网站首页 > 基础教程 正文
前言
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;
}
输出结果
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()函数类似,应把这两对函数对比来看。
往期好文:
我整理了一个嵌入式资料库,大家有什么好资料分享?可以给我留言,我把它加进去,资源共享,一起来完善这个资料库!
(资料库链接:https://gitee.com/zhengnianli/EmbedSummary)
猜你喜欢
- 2024-10-19 Python3 datetime模块指南:日期时间操作、时区管理与实战案例
- 2024-10-19 C++及数据结构复习笔记,类和对象很简单?为啥好多程序员还不会
- 2024-10-19 Linux时间和日期 linux 时间 表示方法
- 2024-10-19 C/C++的8种时间度量方式以及代码片段
- 2024-10-19 Python实战:使用 datetime模块处理时间日期的全方位指南
- 2024-10-19 C++编程的 42 条建议(四) c++编程100例
- 2024-10-19 mount with noatime - 合理关闭atime提高服务器性能
- 2024-10-19 linux下连续三次fork()——深度理解进程创建函数
- 2024-10-19 C函数time和clock的计时区别 c计时器函数
- 2024-10-19 win进程弹出Microsoft Visual C++ Runtime Library的解决办法
- 最近发表
- 标签列表
-
- gitpush (61)
- pythonif (68)
- location.href (57)
- tail-f (57)
- pythonifelse (59)
- deletesql (62)
- c++模板 (62)
- css3动画 (57)
- c#event (59)
- linuxgzip (68)
- 字符串连接 (73)
- nginx配置文件详解 (61)
- html标签 (69)
- c++初始化列表 (64)
- exec命令 (59)
- canvasfilltext (58)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- node教程 (59)
- console.table (62)
- c++time_t (58)
- phpcookie (58)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)