Introduction to C ++ Date and Time
The C ++ standard library does not provide the so-called date type. C ++ inherits the structures and functions used by C for date and time operations.
To use date-and time-related functions and structures, you must reference the header file in the C ++ program.
There are four time-related types: clock_t, time_t, size_t, and tm. The clock_t, size_t, and time_t types can represent the system time and date as some integer.
The structure type tm stores the date and time in the form of a C structure. The tm structure is defined as follows:
Struct tm {
Int tm_sec; // second. The normal range is from 0 to 59, but the value can be 61.
Int tm_min; // The value ranges from 0 to 59.
Int tm_hour; // hour, ranging from 0 to 23
Int tm_mday; // The day of January 1, January, ranging from 1 to 31.
Int tm_mon; // month, ranging from 0 to 11
Int tm_year; // number of years since January 1, 1900
Int tm_wday; // the day of the week, ranging from 0 to 6, counted from Sunday.
Int tm_yday; // The day of the year, ranging from 0 to 365, counted from January 1, January 1.
Int tm_isdst; // timeout
}
Current date and time
The following instance obtains the date and time of the current system.
# Include <iostream>
# Include <ctime>
Using namespace std;
Int main ()
{
/* Based on the current date/time of the current system */
Time_t now = time (0 );
/* Convert now to the string format */
Char * dt = ctime (& now );
Cout <"local date and time:" <dt <endl;
Return 0;
}
Format time using structure tm
The tm structure is particularly important when processing date and time-related operations in C/C ++. The tm structure saves the date and time in the form of a C structure. Most time-related functions use the tm structure. The following example uses the tm structure and various functions related to date and time.
# Include <iostream>
# Include <ctime>
Using namespace std;
Int main ()
{
/* Based on the current date/time of the current system */
Time_t now = time (0 );
Cout <"1970 to the current elapsed seconds:" <now <endl;
Tm * ltm = localtime (& now );
/* Components of the output tm structure */
Cout <"year:" <1900 + ltm-> tm_year <endl;
Cout <"month:" <1 + ltm-> tm_mon <endl;
Cout <"day:" <ltm-> tm_mday <endl;
Cout <"time:" <ltm-> tm_hour <":";
Cout <ltm-> tm_min <":";
Cout <ltm-> tm_sec <endl;
Return 0;
}
Supplement the important functions related to date and time in C/C ++:
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 since January 1, January 1, 1970. If the system does not have time, return. 1. |
2 |
CharCtime (const time_tTime); returns a string pointer that represents the local time, in the form of day month year hours: minutes: seconds year \ n \ 0. |
3 |
Struct tmLocaltime (const time_tTime); this function returns a pointer to the tm structure that represents the local time. |
4 |
Clock_t clock (void); the time used by the processor clock to return the execution start of the Program (generally the beginning of the program. If the time is unavailable, return. 1. |
5 |
Char * asctime (const struct tm * time); this function returns a pointer to a string. The string contains the information stored in the structure pointed to by time. The return form is: day month date hours: minutes: seconds year \ n \ 0. |
6 |
Struct tmGmtime (const time_tTime); this function returns a pointer to time. The time is in the tm structure and expressed in Coordinated Universal time (UTC), also known as Greenwich Mean time (GMT. |
7 |
Time_t mktime (struct tm * time); this function returns the calendar time, which is equivalent to the time stored in the structure pointed by time. |
8 |
Double difftime (time_t time2, time_t time1); this function returns the number of seconds between time1 and time2. |
9 |
Size_t strftime (); this function can be used to format the date and time in the specified format. |
This article permanently updates link: https://www.bkjia.com/Linux/2018-02/151106.htm