本文主要认识几个time.h
头文件下的简单的时间函数。分为以下几个方面:
1.表示时间的三种数据类型
2.从计算机系统时钟获得时间的方法
3.常见的时间日期数据类型的转换函数
4.时间日期数据的格式化函数
5.对时间数据的操作
(注:在Visual Studio 2015中,使用某些函数会提示出错,如:
‘ctime’: This function or variable may be unsafe. Consider using ctime_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
因此,应该改用更安全的ctime_s
函数。以下讨论的均是基于新的安全的函数。)
1.表示时间的三种数据类型
1).日历时间(calendar time),是从一个标准时间点(epoch)到现在的时间经过的秒数,不包括插入闰秒对时间的调整。开始计时的标准时间点,各种编译器一般使用UTC1970年1月1日0时0秒。日历时间用数据类型time_t表示。time_t类型实际上一般是32位整数类型。因此表示的时间不能晚于UTC 2038年1月18日19时14分07秒。为此,某些编译器引入了64位甚至更长的整形来保存日历时间。如Visual C++支持__time64_t数据类型,通过_time64()函数获取日历时间,可支持到UTC 3001年1月1日0时0分0秒的时间。
2).时钟滴答数(clock tick),从进程启动开始计时,因此这是相对时间。每秒钟包含CLOCKS_PER_SEC(time.h中定义的常量,一般为1000)个时钟滴答。时钟滴答数用数据类型clock_t表示。clock_t类型一般是32位整数类型。
3).分解时间(broken-down time),用结构数据类型tm表示,tm包含下列结构成员:
2.从计算机系统时钟获得时间的方法
1). time_t time(time_t *timer);
该函数的返回值是自 1970 年 1 月 1 日午夜以来经过的时间(单位:秒),如果发生错误则返回 -1。返回值存储在指定的位置timer中,此参数可以为NULL,在这种情况下不存储返回值。
测试:
#include <stdio.h>
#include <time.h>
int main()
{
time_t ltime;
time(<ime);
printf("%d\n", ltime);
return 0;
}
输出结果:1495184971。
这就是从1970 年 1 月 1 日午夜以来经过的秒数。然而,这样的时间显示不符合一般情况下的阅读习惯。
2).clock_t clock(void)
得到从进程启动到此次函数调用的累计的时钟滴答数。
测试:
#include <stdio.h>
#include <time.h>
int main(void)
{
clock_t start, finish;
double duration;
long times = 600000000l;
start = clock();
while (times)
times--;
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf("time to do 600000000 empty loops is %f seconds\n",duration);
return 0;
}
输出结果:
time to do 600000000 empty loops is 1.527000 seconds
3.常见的时间日期数据类型的转换函数
1). errno_t gmtime_s(struct tm* _tm,const time_t* time);
从日历时间time_t到分解时间tm(世界协调时UTC)的转换。存储空间被tm被gmtime_s
, localtime_s
与ctime_s
函数所共用。这些函数的每一次调用会覆盖这块tm结构存储空间的内容。如果成功,该函数返回0。
测试:
#include <stdio.h>
#include <time.h>
#define SIZE 26
int main()
{
time_t nowtime;
time(&nowtime);
struct tm currtime;
char buf[SIZE];
if (ctime_s(buf, SIZE, &nowtime) == 0)
printf("%s", buf);
if (gmtime_s(&currtime, &nowtime) == 0) {
printf("hour (0 – 23):%d\n", currtime.tm_hour);
printf("day of the month (1 – 31):%d\n", currtime.tm_mday);
printf("minutes (0 – 59):%d\n", currtime.tm_min);
printf("month (0 – 11, 0 = January):%d\n", currtime.tm_mon);
printf("seconds (0 – 60, 60 = Leap second):%d\n", currtime.tm_sec);
printf("day of the week (0 – 6, 0 = Sunday):%d\n", currtime.tm_wday);
printf("day of the year (0 – 365):%d\n", currtime.tm_yday);
printf("year since 1900: %d\n", currtime.tm_year);
}
return 0;
}
输出结果:
Fri May 19 21:09:40 2017
hour (0 – 23): 13 //北京时间在东八区,领先UTC八个小时,故UTC的小时数应该是21-8=13
day of the month (1 – 31): 19
minutes (0 – 59): 9
month (0 – 11, 0 = January): 4
seconds (0 – 60, 60 = Leap second): 40
day of the week (0 – 6, 0 = Sunday):5
day of the year (0 – 365): 138
year since 1900: 117
2). errno_t localtime_s(struct tm* _tm,const time_t *time);
从日历时间time_t到分解时间tm(当地时间)的转换,即结果数据已经调整到本地时区与夏令时。不在单独举例子测试。
4.时间日期数据的格式化函数
1). errno_t ctime_s(char* buffer, size_t numberOfElements, const time_t *time);
把日历时间time_t timer
输出到字符串。字符串包含完全 26 个字符,其形式为Wed Jan 02 02:03:55 1980\n\0
,使用 24 小时制。参数buffer存放字符串结构的时间。若该函数调用成功,则返回0。
测试:
#include <stdio.h>
#include <time.h>
#define SIZE 26
int main(void)
{
time_t ltime;
char buf[SIZE];
time(<ime);
printf("%d\n", ltime);
if (ctime_s(buf, SIZE, <ime) == 0)//成功返回0
printf("%s", buf);
return 0;
}
输出结果:
1495186159
Fri May 19 17:29:19 2017
显然,通过调用ctime_s
函数,就能将当前时间显示出来了。
2). errno_t asctime_s(char* buffer,size_t numberOfElements, const struct tm *_tm);
把分解时间tm输出到字符串,结果的格式与ctime_s
函数一样。
5.对时间数据的操作
1). double difftime(time_t timer1, time_t timer0);
返回从 timer0 (开始时间)到 timer1(结束时间) 流逝的时间(以秒为单位)。 返回值为双精度浮点数字。 返回值可能为 0,表示错误。