相关头文件
#inlucde <time.h>
相关结构体定义
<time.h>
typedef __time_t time_t;
<bits/types.h>
#if __WORDSIZE == 32
# define __STD_TYPE typedef
/* We want __extension__ before typedef's that use nonstandard base types
such as `long long' in C89 mode. */
# define __STD_TYPE __extension__ typedef
#elif __WORDSIZE == 64
__STD_TYPE __TIME_T_TYPE __time_t; /* Seconds since the Epoch. */
#else
<bits/typesizes.h>
/* X32 kernel interface is 64-bit. */
#if defined __x86_64__ && defined __ILP32__
# define __SYSCALL_SLONG_TYPE __SQUAD_TYPE
# define __SYSCALL_ULONG_TYPE __UQUAD_TYPE
#else
# define __SYSCALL_SLONG_TYPE __SLONGWORD_TYPE
# define __SYSCALL_ULONG_TYPE __ULONGWORD_TYPE
#endif
#define __TIME_T_TYPE __SYSCALL_SLONG_TYPE
<types.h>
/* quad_t is also 64 bits. */
#if __WORDSIZE == 64
typedef long int __quad_t;
typedef unsigned long int __u_quad_t;
#else
__extension__ typedef long long int __quad_t;
__extension__ typedef unsigned long long int __u_quad_t;
#endif
#define __SLONGWORD_TYPE long int
#if __WORDSIZE == 32
# define __SQUAD_TYPE __quad_t
#elif __WORDSIZE == 64
# define __SQUAD_TYPE long int
#else
/*通过上面关于time_t的定义追踪后发现,虽然针对__WORDSIZE是32还是64,对time_t的类型做了不同的处理,但最终生成的类型都是一个64bit的有符号的整型。*/
<time.h>
struct tm
{
int tm_sec; /* Seconds. [0-60] (1 leap second) */
int tm_min; /* Minutes. [0-59] */
int tm_hour; /* Hours. [0-23] */
int tm_mday; /* Day. [1-31] */
int tm_mon; /* Month. [0-11] */
int tm_year; /* Year - 1900. */
int tm_wday; /* Day of week. [0-6] */
int tm_yday; /* Days in year.[0-365] */
int tm_isdst; /* DST. [-1/0/1]*/
# ifdef __USE_MISC
long int tm_gmtoff; /* Seconds east of UTC. */
const char *tm_zone; /* Timezone abbreviation. */
# else
long int __tm_gmtoff; /* Seconds east of UTC. */
const char *__tm_zone; /* Timezone abbreviation. */
# endif
};
时间相关的函数
time()、ctime()、localtime()、gmtime()、asctime()、strftime()、
/*返回自纪元 Epoch(1970-01-01 00:00:00 UTC)起经过的时间,以秒为单位。如果 seconds 不为空,则返回值也存储在变量 seconds 中。
*/
time_t time(time_t *seconds);
/*返回一个表示当地时间的字符串,当地时间是基于参数 timer。
返回的字符串格式如下: Www Mmm dd hh:mm:ss yyyy
*/
char *ctime(const time_t *timer);
/*使用 timer 的值来填充 tm 结构。timer 的值被分解为 tm 结构,并用本地时区表示。*/
struct tm *localtime(const time_t *timer);
/* 使用 timer 的值来填充 tm 结构,并用协调世界时(UTC)也被称为格林尼治标准时间(GMT)表示。*/
struct tm *gmtime(const time_t *timer);
/*一个指向字符串的指针,包含了可读格式的日期和时间信息 Www Mmm dd hh:mm:ss yyyy*/
char *asctime(const struct tm *timeptr)
/*根据 format 中定义的格式化规则,格式化结构 timeptr 表示的时间,并把它存储在 str 中。*/
size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr);
编程应用
time() → gmtime → asctime
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <error.h>
#include <stdlib.h>
int main(void)
{
time_t now_time = 0;
char *buf = NULL;
struct tm *utc = NULL;
char outstr[256] = {0};
now_time = time(NULL); //自1970来到现在一共过去了几秒
printf("\n1970到目前经历的秒数(now_time)为:%ld \n", now_time);
utc =gmtime(&now_time);
printf("1970到目前经历的秒数通过gmtime转换成struct tm结构体时间:");
printf("%d年%d月%d日%d时%d分%d秒\n", utc->tm_year+1900, utc->tm_mon+1, utc->tm_mday, utc->tm_hour, utc->tm_min, utc->tm_sec); //格林尼治时间值与北京时间小时相差8小时。
printf("将struct tm结构体时间 utc 转换成字符串格式时间显示:%s", asctime(utc));
strftime(outstr,sizeof(outstr),"%Y-%m-%d %H:%M:%S", utc); //选择输出什么格式 %F 年-月-日 %j 十进制表示的每年的第几天
printf("将struct tm转换成特定格式的时间规则样式显示:");
printf("Format string is | %s |\n ", outstr);
return 0;
}
执行结果:
1970到目前经历的秒数(now_time)为:1614735691
1970到目前经历的秒数通过gmtime转换成struct tm结构体时间:2021年3月3日1时41分31秒
将struct tm结构体时间 utc 转换成字符串格式时间显示:Wed Mar 3 01:41:31 2021
将struct tm转换成特定格式的时间规则样式显示:Format string is | 2021-03-03 01:41:31 |
time() → localtime → asctime
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <error.h>
#include <stdlib.h>
int main(void)
{
time_t now_time = 0;
char *buf = NULL;
struct tm *local = NULL;
char outstr[256] = {0};
now_time = time(NULL); //自1970来到现在一共过去了几秒
printf("\n1970到目前经历的秒数(now_time)为:%ld \n", now_time);
local = localtime(&now_time);
printf("1970到目前经历的秒数通过localtime转换成struct tm结构体时间:");
printf("%d年%d月%d日%d时%d分%d秒\n", local->tm_year+1900, local->tm_mon+1, local->tm_mday, local->tm_hour, local->tm_min, local->tm_sec); //本地时间:UTC+8
printf("将struct tm结构体时间 local 转换成字符串格式时间显示:%s", asctime(local));
strftime(outstr,sizeof(outstr),"%Y-%m-%d %H:%M:%S", local); //选择输出什么格式 %F 年-月-日 %j 十进制表示的每年的第几天
printf("将struct tm转换成特定格式的时间规则样式显示:");
printf("Format string is | %s |\n ", outstr);
return 0;
}
执行结果:
1970到目前经历的秒数(now_time)为:1614736292
1970到目前经历的秒数通过localtime转换成struct tm结构体时间:2021年3月3日9时51分32秒
将struct tm结构体时间 local 转换成字符串格式时间显示:Wed Mar 3 09:51:32 2021
将struct tm转换成特定格式的时间规则样式显示:Format string is | 2021-03-03 09:51:32 |
不经过gmtime、localtime、asctime,由time → ctime
#include<stdio.h>
#include<unistd.h>
#include<time.h>
#include<error.h>
#include <stdlib.h>
int main(void)
{
time_t now_time = 0;
now_time = time(NULL); //自1970来到现在一共过去了几秒
printf("1970到目前经历的秒数(now_time)为:%ld \n", now_time);
printf("1970到目前经历的秒数转成成字符串格式显示:%s\n", ctime(&now_time));
return 0;
}
执行结果:
1970到目前经历的秒数(now_time)为:1614736979
1970到目前经历的秒数转成成字符串格式显示:Wed Mar 3 10:02:59 2021