Article directory
Summary: This issue mainly introduces the common time expression structures in C/C++ and the conversion relationship between them.
一、FILETIME
The FILETIME structure preserves the file'sunsigned 64-bit date and time value. This value represents fromThe number of 100-nanosecond units since the beginning of January 1, 1601.
The specific structure is as follows:
typedef struct _FILETIME
{
DWORD dwLowDateTime;
DWORD dwHighDateTime;
} FILETIME, FAR *LPFILETIME;
The DWORD type is unsigned long , and dwLowDateTime represents the low-order 32 bits of the file time value.
dwHighDateTime represents the high-order 32 bits of the file time value.
Two, time_t
The utc time seen in the computer isCounting seconds from (January 01, 1970 0:00:00). The UTC time you see is the total number of seconds from the time point in 1970 to the specific time. time_t is actually a long long integer.
The specific structure is as follows:
typedef long time_t;
Three, timeval
timeval is also used to represent UTC time, that is, the time starting from 0:00:00 on January 1, 1970. But the timeval structure can be accurate to microseconds.
The specific structure is as follows:
struct timeval
{
long tv_sec; /*秒*/
long tv_usec; /*微秒*/
};
four, tm
The tm structure is used to store UTC time, and is structured according to the year, month, day, hour, minute, and second.
The specific structure is as follows:
struct tm
{
int tm_sec; /*秒,正常范围0-59, 但允许至61*/
int tm_min; /*分钟,0-59*/
int tm_hour; /*小时, 0-23*/
int tm_mday; /*日,即一个月中的第几天,1-31*/
int tm_mon; /*月, 从一月算起,0-11 1+p->tm_mon;*/
int tm_year; /*年, 从1900至今已经多少年 1900+ p->tm_year;*/
int tm_wday; /*星期,一周中的第几天, 从星期日算起,0-6*/
int tm_yday; /*从今年1月1日到目前的天数,范围0-365*/
int tm_isdst; /*日光节约时间的旗标*/
};
5. Conversion between time structures
Introduce the header file #include<time.h>
1.FILETIME --> time_t
time_t FILETIMEToTime_t(FILETIME *pTime)
{
ULARGE_INTEGER ull;//ULARGE_INTEGER 是64位无符号整型结构
ull.LowPart = pTime->dwLowDateTime;
ull.HighPart = pTime->dwHighDateTime;
return ull.QuadPart / 10000000ULL - 11644473600ULL;
}
2.time_t --> tm
struct tm* gmtime(const time_t *timep);
/*将time_t表示的时间转换为没有经过时区转换的UTC时间,是一个struct tm结构指针*/
stuct tm* localtime(const time_t *timep);
/*和gmtime类似,但是它是经过时区转换的时间。*/
3.tm --> time_t
time_t mktime(struct tm* timeptr);
/*将struct tm 结构的时间转换为从1970年至今的秒数*/
6. The time structure is displayed as the current common time
1.tm structure
char *asctime(const struct tm* timeptr);
/*将结构中的信息转换为真实世界的时间,以字符串的形式显示*/
/*或者通过自定义输出*/
printf("time: %04d-%02d-%02d %02d:%02d:%02d\n", 1900 + p->tm_year,1+p->tm_mon,p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec );
2. time_t structure
char *ctime(const time_t *timep);
/*将timep转换为真是世界的时间,以字符串显示,它和asctime不同就在于传入的参数形式不一样*/
end
This is the end of this issue for the analysis of the time structure in C/C++, see you next time:)