时间编程

1.核心理论

 * Coordinated Universal Time (UTC):世界标准时间,也就是格林威治时间 (Greenwich Mean Time,GMT)

 * Calendar Time:日历时间,从1970年1月1日0点到此时经历的秒数来表示时间

2.函数学习

获取日历时间

函数名:time

函数原型:time_t time(time_t *t);

头文件:<time.h>

函数功能:返回日历时间

返回值: 成功:返回日历时间  失败:-1

参数说明:t:不为空保存返回值

获取格林威治时间

函数名:gmtime

函数原型:struct tm *gmtime(const time_t *timep);

头文件:<time.h>

功能:将日历时间转化为标准时间

返回值:成功:以struct tm形式存储的标准时间  

参数说明:待转化的日历时间

获取本地时间

函数名:localtime

函数原型:struct tm*localtime(const time_t *timep);

函数功能:将参数timep所指向的日历时间转化为本地时间

头文件: <time.h>

返回值:成功返回以struct tm格式存储的本地时间    失败:返回NULL

参数说明:指向待转化的日历时间

字符串方式显示时间

函数名:asctime

函数原型:char *asctime(const struct tm *tm);

函数功能:将struct tm格式的时间转化为字符串

头文件:<time.h>

返回值:字符串方式显示的时间

参数说明:待转化的tm格式的时间

获取高精度时间

函数名:gettimeofday

函数原型:int gettimeofday(struct timeval *tv,struct timezone *tzone);

函数功能:获取高精度时间

头文件:<sys/time.h>

返回值:成功:0  失败:-1

参数说明:tv:保存从1970.1.1日0时到现在经历的秒数和微秒数,tzone通常为NULL

猜你喜欢

转载自blog.csdn.net/qq_39541098/article/details/81530569