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

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

设置系统时间参考本文。

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;

}

相关推荐

2010年世界杯西班牙的传奇阵容,现状都怎么样了?
365bet官网的微博

2010年世界杯西班牙的传奇阵容,现状都怎么样了?

📅 07-02 👁️ 631
土巴兔的運營模式是什麼?為什麼能獲得億元投資?
365bet官网的微博

土巴兔的運營模式是什麼?為什麼能獲得億元投資?

📅 07-09 👁️ 2750
迫击炮这种技术含量低的武器为什么还没有被淘汰?
365bet体育在线投注官网

迫击炮这种技术含量低的武器为什么还没有被淘汰?

📅 07-09 👁️ 5099
揭秘Windows系统分区激活全攻略:轻松解锁,告别黑屏困扰!
365bet体育在线投注官网

揭秘Windows系统分区激活全攻略:轻松解锁,告别黑屏困扰!

📅 07-02 👁️ 1773
win11如何删除多余的账户?如何彻底移除账户?
365bet官网的微博

win11如何删除多余的账户?如何彻底移除账户?

📅 06-28 👁️ 8331
徕卡镜头怎么样?徕卡镜头各系列型号介绍
365bet官网的微博

徕卡镜头怎么样?徕卡镜头各系列型号介绍

📅 07-10 👁️ 2623
拍拍贷怎么充值?
365禁用取消提款什么意思

拍拍贷怎么充值?

📅 07-05 👁️ 4716
win11如何删除多余的账户?如何彻底移除账户?
365bet官网的微博

win11如何删除多余的账户?如何彻底移除账户?

📅 06-28 👁️ 8331
巴西队难破“魔咒” 遭淘汰 主教练蒂特宣布辞职
365bet官网的微博

巴西队难破“魔咒” 遭淘汰 主教练蒂特宣布辞职

📅 06-27 👁️ 7262