c语言中获取当前时间的函数

2025-07-18 03:24:50

设置系统时间参考本文。

1、time()函数

#include

typedef long time_t;//time_t实际是long类型数据。

time_t time( time_t * ) ; //time_t 是long类型

//返回从1970年1月1日0时0分0秒,到现在的的秒数

#include

time_t t;

time_t time(&t); //返回从1970年1月1日0时0分0秒,到现在的的秒数

time(&t);中t的取值 和time(NULL)的返回值相等,即二者等价。

2、struct tm * localtime(const time_t * timer); //将世纪秒转换成对应的年月日时分秒;

其中:

struct tm {

int tm_sec; /* 秒 – 取值区间为[0,59] */

int tm_min; /* 分 - 取值区间为[0,59] */

int tm_hour; /* 时 - 取值区间为[0,23] */

int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */

int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] ,需要+1 */

int tm_year; /* 年份,其值等于实际年份减去1900 */

int tm_wday; /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一 */

int tm_yday; /* 从每年1月1日开始的天数– 取值区间[0,365],其中0代表1月1日 */

int tm_isdst; /* 夏令时标识符,夏令时tm_isdst为正;不实行夏令时tm_isdst为0 */

};

所以在输出年时要加1900,输出月要加1,一般输出前6个

(长年累月的加) 3、char * ctime(const time_t *timer);

将time_t时间转换成 char* 形式日历形式的时间

#include

int main()

{

time_t t;

struct tm *st ;

char *ch ;

time(&t);

printf("time:%d\n", t );

ch = ctime(&t) ;

printf("ctime:%s\n", ch );

st = localtime(&t);

printf("year=%d\n", st->tm_year+1900 ); //年需要加1900

printf("month=%d\n", st->tm_mon+1 ); //月需要加1

printf("day=%d\n", st->tm_mday ); //其他的不用

return 0;

}

4、gettimeofday()//精确度更高,精确到微秒

#include //此函数头文件中需要添加sys才好用。

int gettimeofday(struct timeval*tv,struct timezone *tz ) //第二个参数tz一般都为空,因为不需要第2个参数的值

struct timeval{

long tv_sec;/*秒*/

long tv_usec;/*微秒*/

};

struct timezone{

int tz_minuteswest;/*和greenwich 时间差了多少分钟*/

int tz_dsttime;/*type of DST correction*/

};

#include

#include

int main()

{

time_t t;

time(&t);

printf("time:%d\n", t ); //time(&t);

struct timeval tv;

struct timezone tz;

gettimeofday(&tv,&tz);

printf("tv_sec:%d\n",tv.tv_sec); //gettimeofday(&tv,&tz);

printf("tv_usec:%d\n",tv.tv_usec);

printf("tz_minuteswest:%d\n",tz.tz_minuteswest);

printf("tz_dsttime:%d\n",tz.tz_dsttime);

}

上述程序使用gettimeofday()显示结果表明当前距1970年1月1日0时0分0秒有1587450597.989175s

小结:

1) time(&t);和 //gettimeofday(&tv,&tz);中 tv.tv_sec; 的值一样的。

2)gettimeofday()可以精确到微秒,在测试程序代码执行时间上用的比较多。

3)gettimeofday()函数的头文件为 #include 写成#include 报错。(为Linux系统函数??)

4)gettimeofday()我们一般写成gettimeofday(&tv,NULL);因为我们不想知道2参的情况。

5、clock_gettime()函数,与gettimeofday类似,但精确度到更高,可以精确到纳秒

#include

int clock_gettime(clockid_t clk_id, struct timespec* tp);

clk_id : 检索和设置的clk_id指定的时钟时间。 CLOCK_REALTIME:系统实时时间,随系统实时时间改变而改变,即从UTC1970-1-1 0:0:0开始计时,中间时刻如果系统时间被用户改成其他,则对应的时间相应改变 CLOCK_MONOTONIC:从系统启动这一刻起开始计时,不受系统时间被用户改变的影响 CLOCK_PROCESS_CPUTIME_ID:本进程到当前代码系统CPU花费的时间 CLOCK_THREAD_CPUTIME_ID:本线程到当前代码系统CPU花费的时间

struct timespec { time_t tv_sec; /* 秒*/ long tv_nsec; /* 纳秒*/ };

#define BILLION 1000000000L;

int main( int argc, char** argv )

{

struct timespec start, stop;

double accum;

if( clock_gettime( CLOCK_REALTIME, &start) == -1 ) {

perror( "clock gettime" );

return EXIT_FAILURE;

}

system( argv[1] );

if( clock_gettime( CLOCK_REALTIME, &stop) == -1 ) {

perror( "clock gettime" );

return EXIT_FAILURE;

}

accum = ( stop.tv_sec - start.tv_sec )

+ (double)( stop.tv_nsec - start.tv_nsec )

/ (double)BILLION;

printf( "%lf\n", accum );

return EXIT_SUCCESS;

}