1. 先看一段程序
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t cur = time(NULL);
printf ("cur = %ld \n", cur);
printf("ctime(&cur) = %s", ctime(&cur));
printf("asctime(gmtime(&cur)) = %s", asctime(gmtime(&cur)));
printf("asctime(localtime(&cur)) = %s", asctime(localtime(&cur)));
return 0;
}
其输出如下:
cur = 1658548680
ctime(&cur) = Sat Jul 23 11:58:00 2022
asctime(gmtime(&cur)) = Sat Jul 23 03:58:00 2022
asctime(localtime(&cur)) = Sat Jul 23 11:58:00 2022
2. 参数介绍
#ifndef _TIME32_T_DEFINED
#define _TIME32_T_DEFINED
typedef long __time32_t;
#endif
#ifndef _TIME64_T_DEFINED
#define _TIME64_T_DEFINED
__MINGW_EXTENSION typedef __int64 __time64_t;
#endif
#ifndef _TIME_T_DEFINED
#define _TIME_T_DEFINED
#ifdef _USE_32BIT_TIME_T
typedef __time32_t time_t;
#else
typedef __time64_t time_t;
#endif
#endif
由于是64位系统,所有time_t的类型是__int64,上面打印的还是有点问题,应该这样
printf ("cur = %lld \n", cur);
time_t time (time_t* timer);
Get the current calendar time as a value of type time_t.
The function returns this value, and if the argument is not a null pointer, it also sets this value to the object pointed by timer.
The value returned generally represents the number of seconds since 00:00 hours, Jan 1, 1970 UTC.
char* ctime (const time_t * timer);
Convert time_t value to string
Interprets the value pointed by timer as a calendar time and converts it to a C-string containing a human-readable version of the corresponding time and date, in terms of local time.
The returned string has the following format:
Www Mmm dd hh:mm:ss yyyy
struct tm * gmtime (const time_t * timer);
Convert time_t to tm as UTC time
Uses the value pointed by timer to fill a tm structure with the values that represent the corresponding time, expressed as a UTC time.
struct tm * localtime (const time_t * timer);
Convert time_t to tm as local time
Uses the value pointed by timer to fill a tm structure with the values that represent the corresponding time, expressed for the local timezone.
char* asctime (const struct tm * timeptr);
Convert tm structure to string
Interprets the contents of the tm structure pointed by timeptr as a calendar time and converts it to a C-string containing a human-readable version of the corresponding date and time.
The returned string has the following format:
Www Mmm dd hh:mm:ss yyyy
struct tm
Time structure
Structure containing a calendar date and time broken down into its components.
Member Type Meaning Range
tm_sec int seconds after the minute 0-60*
tm_min int minutes after the hour 0-59
tm_hour int hours since midnight 0-23
tm_mday int day of the month 1-31
tm_mon int months since January 0-11
tm_year int years since 1900
tm_wday int days since Sunday 0-6
tm_yday int days since January 1 0-365
tm_isdst int Daylight Saving Time flag
3. 程序释义
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t cur = time(NULL);
/* cur 是1970年1月1日0时0分0秒到此时的秒数 */
printf ("cur = %lld \n", cur);
/* ctime(&cur)是将此时的时间转换成字符串输出,格式是星期几 几月 几号 某时:某分:某秒 某年 */
printf("ctime(&cur) = %s", ctime(&cur));
/*
* gmtime(&cur)是将此时的时间转换成UTC时间,即零时区时间
* localtime(&cur)是将此时的时间转换成本时区时间,即北京时间
* asctime()是将上述两个函数获取的时间转换成字符串输出
*/
printf("asctime(gmtime(&cur)) = %s", asctime(gmtime(&cur)));
printf("asctime(localtime(&cur)) = %s", asctime(localtime(&cur)));
return 0;
}
4. 附录
想了解C语言库函数了,可以到这里 https://cplusplus.com/reference/clibrary/