2025年3月29日 星期六 甲辰(龙)年 月廿八 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > C语言

【C语言】时间日期函数

时间:07-26来源:作者:点击数:31

一、前言

time.h是C/C++中的日期和时间头文件。用于需要时间方面的函数。下面分享time.h头文件中几个常用函数的用法:

二、time()函数

1、函数原型

  • time_t time(time_t *t);

2、函数说明

time_t 是long int 类型。此函数会返回从公元1970年1月1日的UTC时间从0时0 分0秒算起到现在所经过的秒数。如果t是空指针,直接返回当前时间。如果t不是空指针,返回当前时间的同时,将返回值赋予t指向的内存空间。

3、函数返回值

成功则返回秒数,失败则返回((time_t)-1)值,错误原因存于errno中。

4、示例程序

  • #include <stdio.h>
  • #include <time.h>
  • int main(void)
  • {
  • time_t sec = time((time_t*)NULL);
  • printf("1970-01-01至现在的秒数为:%d\n", sec);
  • return 0;
  • }

输出结果

none

  • 1970-01-01至现在的秒数为:1542377079

三、gmtime()函数

1、函数原型

  • struct tm *gmtime(const time_t *timep);

2、函数说明

gmtime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。

结构tm的定义为:

  • struct tm{
  • int tm_sec; //代表目前秒数, 正常范围为0-59, 但允许至61 秒
  • int tm_min; //代表目前分数, 范围0-59
  • int tm_hour; //从午夜算起的时数, 范围为0-23
  • int tm_mday; //目前月份的日数, 范围01-31
  • int tm_mon; //代表目前月份, 从一月算起, 范围从0-11
  • int tm_year; //从1900 年算起至今的年数
  • int tm_wday; //一星期的日数, 从星期一算起, 范围为0-6
  • int tm_yday; //从今年1 月1 日算起至今的天数, 范围为0-365
  • int tm_isdst; //日光节约时间的旗标
  • };

3、函数返回值

返回结构tm代表目前UTC时间。

4、示例程序

  • #include <stdio.h>
  • #include <time.h>
  • int main(void)
  • {
  • char *wday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
  • time_t t;
  • struct tm *p;
  • int year = 0, mon = 0, mday = 0;
  • int hour = 0, min = 0, sec = 0;
  • char *week = NULL;
  • time(&t);
  • p = gmtime(&t);
  • year = 1900 + p->tm_year;
  • mon = 1 + p->tm_mon;
  • mday = p->tm_mday;
  • week = wday[p->tm_wday];
  • hour = 8 + p->tm_hour; //获取当地时间,与UTC时间相差8小时
  • min = p->tm_min;
  • sec = p->tm_sec;
  • printf("%d-%d-%d %s", year, mon, mday, week);
  • printf(" %.2d:%.2d:%.2d\n", hour, min, sec);
  • return 0;
  • }

输出结果

  • 2018-11-16 Fri 22:23:25

四、localtime()函数

1、函数原型

  • struct tm *localtime(const time_t * timep);

2、函数说明

localtime()将参数timep所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。

3、函数返回值

返回结构tm代表目前的当地时间。

4、示例程序

  • #include <stdio.h>
  • #include <time.h>
  • int main(void)
  • {
  • char *wday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
  • time_t t;
  • struct tm *p;
  • int year = 0, mon = 0, mday = 0;
  • int hour = 0, min = 0, sec = 0;
  • char *week = NULL;
  • time(&t);
  • p = localtime(&t); //取得当地时间
  • year = 1900 + p->tm_year;
  • mon = 1 + p->tm_mon;
  • mday = p->tm_mday;
  • week = wday[p->tm_wday];
  • hour = p->tm_hour;
  • min = p->tm_min;
  • sec = p->tm_sec;
  • printf("%d-%d-%d %s", year, mon, mday, week);
  • printf(" %.2d:%.2d:%.2d\n", hour, min, sec);
  • return 0;
  • }

输出结果

  • 2018-11-16 Fri 22:32:27

五、ctime()函数

1、函数原型

  • char *ctime(const time_t *timep);

2、函数说明

ctime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时间。注意:若再调用相关的时间日期函数,此字符串可能会被破坏。

3、函数返回值

返回一字符串表示目前当地的时间日期。格式:星期,月,日,小时:分:秒,年。

4、示例程序

  • #include <stdio.h>
  • #include <time.h>
  • int main(void)
  • {
  • time_t t;
  • time(&t);
  • printf("%s\n", ctime(&t));
  • return 0;
  • }

输出结果

  • Fri Nov 16 22:38:51 2018

六、asctime()函数

1、函数原型

  • char *asctime(const struct tm * timeptr);

2、函数说明

asctime()将参数timeptr所指的tm结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时间。注意:若再调用相关的时间日期函数,此字符串可能会被破坏。与ctime()函数的不同在于传入参数结构不同。

3、函数返回值

返回一字符串表示目前当地的时间日期。格式:星期,月,日,小时:分:秒,年。

4、示例程序

  • #include <stdio.h>
  • #include <time.h>
  • int main(void)
  • {
  • time_t t;
  • struct tm *p;
  • char *date = NULL;
  • time(&t);
  • p = gmtime(&t);
  • date = asctime(p);
  • printf("%s\n", date);
  • return 0;
  • }

输出结果

  • Fri Nov 16 14:48:29 2018

以上就是关于time.h头文件里一些时间函数的介绍。其中gmtime()函数与localtime()函数类似,ctime()函数与asctime()函数类似,应把这两对函数对比来看。

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐