收藏 分享(赏)

获取Linux系统时间.docx

上传人:hyngb9260 文档编号:7834857 上传时间:2019-05-27 格式:DOCX 页数:3 大小:15.25KB
下载 相关 举报
获取Linux系统时间.docx_第1页
第1页 / 共3页
获取Linux系统时间.docx_第2页
第2页 / 共3页
获取Linux系统时间.docx_第3页
第3页 / 共3页
亲,该文档总共3页,全部预览完了,如果喜欢就下载吧!
资源描述

1、 可以用 localtime 函数分别获取年月日时分秒的数值。Linux 下获得系统时间的 C语言的实现方法:1. 可以用 localtime 函数分别获取年月日时分秒的数值。#include /C 语言的头文件#include /C 语言的 I/Ovoid main()time_t now; /实例化 time_t结构struct tm *timenow; /实例化 tm结构指针time(/time 函数读取现在的时间(国际标准时间非北京时间),然后传值给 nowtimenow = localtime(/localtime 函数把从 time取得的时间 now换算成你电脑中的时间(就是你设置

2、的地区)printf(“Local time is %sn“,asctime(timenow);/上句中 asctime函数把时间转换成字符,通过 printf()函数输出注释:time_t 是一个在 time.h中定义好的结构体。而 tm结构体的原形如下:struct tmint tm_sec;/seconds 0-61int tm_min;/minutes 1-59int tm_hour;/hours 0-23int tm_mday;/day of the month 1-31int tm_mon;/months since jan 0-11int tm_year;/years from

3、1900int tm_wday;/days since Sunday, 0-6int tm_yday;/days since Jan 1, 0-365int tm_isdst;/Daylight Saving time indicator;2. 对某些需要较高精准度的需求,Linux 提供了 gettimeofday()。#include #include #include int main(int argc, char *argv)struct tim start,stop,diff;gettimeofday(/做你要做的事.gettimeofday(tim_subtract(printf(“总计用时:%d 毫秒n“,diff.tv_usec);int tim_subtract(struct tim *result, struct tim *x, struct tim *y)int nsec;if ( x-tv_sec y-tv_sec )return -1;if (x-tv_sec=y-tv_sec) result-tv_sec = ( y-tv_sec-x-tv_sec );result-tv_usec = ( y-tv_usec-x-tv_usec );if (result-tv_usectv_sec-;result-tv_usec+=1000000;return 0;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报