C + + time & Date

Time:2021-8-2

The C + + standard library does not provide the so-called date type. C + + inherits the structures and functions of C language for date and time operations. In order to use date and time related functions and structures, they need to be referenced in C + + programsHeader file.

 

 

There are four time related types: clock_ t、time_ t、size_ t   and   tm。 Type clock_ t、size_ T and time_ T can express the system time and date as some integer.

structure type   tm   Save the date and time in the form of C structure. The definition of TM structure is as follows:

If you encounter problems in learning C / C + +, you can join Xiaobian’s Penguin circle and ask Xiaobian. Xiaobian is very enthusiastic (● ‘◡’ ●)

struct tm { int tm_ sec; // Seconds, normal range from 0 to 59, but allowed to 61

int tm_ min; // Minutes, ranging from 0 to 59

int tm_ hour; // Hours, ranging from 0 to 23

int tm_ mday; // The day of the month, ranging from 1 to 31

int tm_ mon; // Month, ranging from 0 to 11

int tm_ year; // Years since 1900

int tm_ wday; // The day of the week, ranging from 0 to 6, from Sunday

int tm_ yday; // The day of the year, ranging from 0 to 365, from January 1

int tm_ isdst; // Daylight saving time}

The following are important functions about date and time in C / C + +. All these functions are part of the C / C + + standard library. You can view the details of each function in the C + + standard library.

Serial number function & Description

 

 

time_t time(time_t *time);

This function returns the current calendar time of the system and the number of seconds elapsed since January 1, 1970. If the system has no time, return. 1.

char *ctime(const time_t *time);

The returns a string pointer representing the local time in the form of a string  day month year hours:minutes:seconds year/n/0

struct tm *localtime(const time_t *time);

This function returns a pointer to the local time   tm   Pointer to the structure.

clock_t clock(void);

This function returns the time used by the processor clock since the execution of the program (usually the beginning of the program). If time is not available,. 1 is returned.

char * asctime ( const struct tm * time );

The function returns a string in the form of 0 / days: days / days, which points to a string of information stored in the string.

struct tm *gmtime(const time_t *time);

This function returns a pointer to time. Time is a TM structure, which is represented by 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 time stored in the structure pointed to by time.

double difftime ( time_t time2, time_t time1 );

This function returns the number of seconds between time1 and time2.

size_t strftime();

This function can be used to format the date and time in the specified format.

Current date and time

The following example obtains the date and time of the current system, including local time and coordinated universal time (UTC).

 

 

#include  

#include  

using namespace std; int main( )

{/ / based on the current date / time of the current system

time_ t now = time(0); // Convert now to string form

char* dt = ctime(&now); Cout < < local date and time: < < DT < < endl;

//Convert now to TM structure

 tm *gmtm = gmtime(&now); dt = asctime(gmtm);

Cout < < UTC date and time: < < DT < < endl;}

When the above code is compiled and executed, it will produce the following results:

Local date and time: SAT Jan   8 20:07:41 2011   UTC date and time: Sun Jan   9 03:07:41 2011

Format time using structure TM

tm   Structure is particularly important when dealing with date and time related operations in C / C + +. TM structure saves the date and time in the form of C structure. Most time-dependent functions use the TM structure. The following example uses the TM structure and various date and time related functions.

Before practicing using structures, you need to have a basic understanding of C structures and how to use arrow – > operator to access structure members.

#include

 #include  

using namespace std; int main( ) { 

//Based on the current date / time of the current system

time_ t now = time(0); Cout < < seconds since 1970: < < now < < endl; tm *ltm = localtime(&now);

//Each component 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; }

When the above code is compiled and executed, it will produce the following results:

1970 to present time: 1503564157: 2017: August 8: 24 time: 16:42:37