C++ Date & Time
The C++ standard library does not provide a so-called date type. C++ inherits the C language's structures and functions for date and time operations. In order to use date and time related functions and structures, the <ctime> header file needs to be referenced in the C++ program.
There are four time-related types: clock_t, time_t, size_t and tm. The types clock_t, size_t, and time_t represent the system time and date as certain integers.
Structure typetm Save date and time in the form of C structure. The definition of tm 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 int tm_year; // 自 1900 年起的年数 int tm_wday; // 一周中的第几天,范围从 0 到 6,从星期日算起 int tm_yday; // 一年中的第几天,范围从 0 到 365,从 1 月 1 日算起 int tm_isdst; // 夏令时 }
The following is about date and time in C/C++ important functions. All these functions are part of the C/C++ standard library. You can check the details of each function in the C++ standard library.
Serial number | Function & Description |
---|---|
1 | time_t time( time_t *time); This function returns the current calendar time of the system, the number of seconds that have passed since January 1, 1970. If the system has no time, .1 is returned. |
2 | char *ctime(const time_t *time); This returns a string pointer representing the local time, character String format day month year hours:minutes:seconds year\n\0. |
3 | struct tm *localtime(const time_t *time); This function returns a pointer to ## representing the local time #tm Pointer to the structure. |
clock_t clock(void);This function returns the processor when the program is executed (usually the beginning of the program) The time used by the clock. If the time is not available, .1 is returned. | |
char * asctime ( const struct tm * time );This function returns a pointer to a string, character The string contains the information stored in the structure pointed to by time, and the return format is: day month date hours:minutes:seconds year\n\0. | |
struct tm *gmtime(const time_t *time);This function returns a pointer to time, time is tm structure, expressed in Coordinated Universal Time (UTC) also known as Greenwich Mean Time (GMT). | |
time_t mktime(struct tm *time);This function returns the calendar time, which is equivalent to the structure pointed to by time Storage time. | |
double difftime ( time_t time2, time_t time1 );This function returns the number of seconds difference between time1 and time2 . | |
size_t strftime();This function can be used to format the date and time into the specified format. |