获取linux系统时间

上传人:wt****50 文档编号:37982525 上传时间:2018-04-25 格式:DOCX 页数:3 大小:15.25KB
返回 下载 相关 举报
获取linux系统时间_第1页
第1页 / 共3页
获取linux系统时间_第2页
第2页 / 共3页
获取linux系统时间_第3页
第3页 / 共3页
亲,该文档总共3页,全部预览完了,如果喜欢就下载吧!
资源描述

《获取linux系统时间》由会员分享,可在线阅读,更多相关《获取linux系统时间(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;/y

3、ears from 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_subtra

4、ct(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;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号