Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - C++ current time and date collection
C++ current time and date collection
3. Data structure related to date and time

In the standard C/C++, we can get the date and time through the tm structure, which is defined in time.h as follows:

#ifndef _TM_DEFINED

Structure tm {

Int tm _ sec/* sec–value interval is */

Int tm _ min/* minutes-The interval between values is */

Int tm _ hour/* when-value interval is */

Int tm _ mday/* Date in a month-the range is, where 0 means Sunday, 1 means Monday, and so on */

Int tm _ yday/* Number of days per year starting from 65438+1 October 1–the value range is, where 0 means 65438+1 October1and1means 65438+/kloc-0.

Int tm _ isdst/* daylight saving time identifier. When daylight saving time is implemented, tm_isdst is positive. Without daylight saving time, tm_isdst is 0; When the situation is unknown, Tm_isdst () is negative. */

};

#define _TM_DEFINED

#endif

The ANSI C standard says that the time of using tm structure is expressed as breakdown time.

Calendar time is represented by the data type of time_t, and the time represented by time_t is the number of seconds from a certain point in time (for example: 1970 65438+ 10 month 1: 00: 00). In time.h, we can also see that time_t is a long integer:

#ifndef _TIME_T_DEFINED

typedef long time _ t; /* Time value */

#define _TIME_T_DEFINED /* Avoid defining time_t */

#endif

You may wonder: Since time_t is actually a long integer, what should I do if the number of seconds from a certain point in time (generally 1970 65438+65438 10: 00: 00 on October) to that point in time (that is, calendar time) is beyond the range that a long integer can represent? For the value of time_t data type, the time it represents cannot be later than 65438+ 18, 19: 08, 2038+ 14: 07. In order to express a longer time, some compiler manufacturers have introduced 64-bit or even longer integers to save calendar time. For example, Microsoft uses __time64_t data type to save the calendar time in Visual C++, and obtains the calendar time through _time64 () function (instead of using 32-bit time () function), which can save 300 1 year 1 month 1 day at 0: 00: 00 (.

In the header file of time.h, we can also see some functions with time_t as parameter type or return value type:

double difftime(time _ t time 1,time _ t time 0);

time _ t mktime(struct TM * time ptr);

time _ t time(time _ t * timer);

char * ASC time(const struct TM * time ptr);

char * ctime(const time _ t * timer);

In addition, time.h also provides two different functions to convert the calendar time (an integer represented by time_t) into the time format tm that we usually see to display the year, month, day, minute and second respectively:

struct TM * GM time(const time _ t * timer);

struct TM * local time(const time _ t * timer);

By consulting MSDN, we can know that the value of time point in Microsoft C/C++ 7.0 (the value of time_t object) is the number of seconds from189965438+February 3 1 to this time point, while other versions of Microsoft C/C++ and all different versions of Visual C++ are calculated from 60.

4. Functions related to date and time and their applications

In this section, I will show you how to use the functions declared in time.h to manipulate time. These operations include obtaining the current time, calculating the time interval, displaying the time in different forms and so on.

4. 1 Get the calendar time

We can get the calendar Time through the time () function, and its prototype is:

time _ t time(time _ t * timer);

If the parameter timer has been declared, you can return the current calendar time from the parameter timer, or you can return the current calendar time by returning the value, that is, the number of seconds from a certain time point (for example: 1970 1.00+0.00) to the current time. If this parameter is null, the function will return the current calendar time only by the return value, as shown in the following example:

# contains "time.h"

# contains "stdio.h"

int main(void)

{

struct tm * ptr

time _ t lt

Lt = time (nul);

Printf ("The current calendar time is %d\n", lt);

Returns 0;

}

The result of the operation is related to the time at that time. The result of my operation at that time was:

The current calendar time is1122707619.

Where 1 122707 19 is the calendar time when I run the program. That is, seconds from 1970 65438+ 10 month 1, 0: 00, 0: 00.

4.2 Purchase Date and Time

The date and time mentioned here are the information we usually say such as year, month, day, hour, minute and second. From section 2, we already know that all this information is saved in a structure called tm, so how to save a calendar time as an object with tm structure?

The functions that can be used are gmtime () and localtime (), and the prototypes of these two functions are:

struct TM * GM time(const time _ t * timer);

struct TM * local time(const time _ t * timer);

The gmtime () function converts the calendar time into universal standard time (GMT) and returns a tm structure to save the time, while the localtime () function converts the calendar time into local time. For example, the universal time obtained by using the gmtime () function is 7: 00 on July 30th, 2005, so the local time obtained by using the localtime () function in China will be 8 hours later than the universal time, that is, July 30th, 2005 is 65,438+08: 20. Here's an example:

# contains "time.h"

# contains "stdio.h"

int main(void)

{

struct tm * local

time _ t t

T = time (nul);

local=localtime。 t);

Printf ("local time is: %d\n", Local-& gt;; TM _ hour);

local=gmtime。 t);

Printf("UTC hour is: %d\n ",local-& gt;; TM _ hour);

Returns 0;

}

The running result is:

The local time is: 15

UTC time is: 7.