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;