VC/C ++ Time Functions
I. MFC
MFC provides two dates and
TimeClass ctime and ctimespan, respectively representing the relative
TimeAnd absolute
Time. Ctime is based on Greenwich Mean
Time(GMT), local
TimeIt is determined by the Environment Variable tz. Ctimespan represents
TimeInterval.
The ctime class consists of the following members:Function:
Ctime ()
Create a ctime object.
Getcurrenttime ()
By the currentTimeCreate a ctime object.
Gettime ()
A time_t variable is returned by the ctime object.
Getyear ()
Obtain the year represented by the ctime object.
Getmonth ()
Obtain the month represented by the ctime object.
Getday () gets the date represented by the ctime object.
Gethour () obtains the hour that the ctime object represents.
Getminute () gets the score represented by the ctime object.
Getsecond () gets the second represented by the ctime object.
Getdayofweek () obtains the Sunday represented by the ctime object, 1 represents Sunday, and 2 represents weeks.
Format () converts a string into a local time zone-based format string.
Formatgmt () converts a string into a UTC (Universal Time)-based format string.
Operator = newTime.
Operator + adds ctime and ctimespan objects.
Operator-reduces ctime and ctimespan objects.
Operator + = Add a ctimespan object to the ctime object.
Operator-= ctime object minus a ctimespan object.
Operator = compare two absolute valuesTimeEqual or not.
Operator! = Compare two absolute valuesTimeIs not equal.
Operator <compares two absolute valuesTime, Whether the first one is greater than the last one.
Operator> compare two absolute valuesTime, Whether the first one is smaller than the last one.
Operator> = compare two absolute valuesTime, Whether the first one is greater than or equal to the last one.
Operator <= compares two absolute valuesTime, Whether the previous one is less than or equal to the second. Summary first, let's look at the Declaration of the prototype of several functions (in time. h ):
Clock_t clock (void) clock_t is the data type used to save the time. It is a long data type.
Double difftime (time_t time1, time_t time0); time interval function
Time_t time (time_t * timer); Calendar time function
Char * asctime (const struct TM * timeptr); converts the Time Structure of the TM class to a fixed time format
Char * ctime (const time_t * timer); converts the calendar time to a fixed time format
Time_t mktime (struct TM * timeptr); Time Structure saved by years, months, days, hours, minutes, seconds, and other components
Struct TM * gmtime (const time_t * timer); converts the calendar time to Greenwich Mean Time
Struct TM * localtime (const time_t * timer); converts the calendar time to the local time
TM definition:
Struct TM {
Int tm_sec;/* Second-value range: [0, 59] */
Int tm_min;/* minute-value range: [0, 59] */
Int tm_hour;/* hour-value range: [0, 23] */
Int tm_mday;/* date in a month-value range: [] */
Int tm_mon;/* month (from January 1, 0 represents January 1,)-value range: [] */
Int tm_year;/* year, whose value is equal to the actual year minus 1900 */
Int tm_wday;/* day-value range: [0, 6]. 0 indicates Sunday, 1 indicates Monday, and so on.
*/
Int tm_yday;/* Number of days from January 1, January 1 each year-value range: [1, 0,365]. 0 indicates January 1, January 1,
1 represents January 2, and so on */
Int tm_isdst;/* identifier of the sequence. When the sequence is executed, the sequence is positive. Wait time is not implemented
, Tm_isdst is 0. If you do not know the situation, tm_isdst () is negative. */}
1. Get the interval
1. clock_t start, finish;
Long I = 10000000;
Double duration;
Start = clock ();
While (I --);
Finish = clock ();
Duration = (double) (finish-Start)/clocks_per_sec;
Cout <duration;
The output is the time from 10000000 to zero, accurate to milliseconds.
2.
Double pause1;
Time_t start, end;
Start = Time (null );
System ("pause ");
End = Time (null );
Pause1 = difftime (end, start );
Cout <pause1;
The output is: the pause time, accurate to seconds.
2. Get the calendar time
Time_t lt;
Lt = Time (null); // (it is not clear the difference between parameters with and without parameters)
Cout <lt;
The output is the number of seconds from 00:00:00, January 1, January 1, 1970 to the current time.
3. Get the date and time
1. Convert calendar time to local time (Greenwich Mean Time)
Struct TM * local
Time_t T;
T = time (null );
Local = localtime (& T );
// Local = gmtime (& T );
Cout <local-> tm_hour;
2. Get the date and time in a fixed time format: see the parameters and return values of these two functions.
Char * asctime (const struct TM * timeptr );
Char * ctime (const time_t * timer );
1. Convert the calendar time directly to a fixed date and time format
Char * jieguo;
Time_t lt;
Lt = Time (null );
Jieguo = ctime (& lt );
Cout <jieguo;
2. Convert the calendar time to a date and time in a fixed time format after being converted to localtime () and gmtime ().
Struct TM * local;
Char * jieguo;
Time_t T;
T = time (null );
Local = localtime (& T );
// Local = gmtime (& T );
Jieguo = asctime (local );
Cout <jieguo;
4. Convert the decomposition time to the calendar time
The decomposition time here is the time structure stored by the year, month, day, hour, minute, second, and other components. In C/C ++, It is the TM structure. We can
Use the mktime () function to convert the time expressed in the TM structure to the calendar time. The function prototype is as follows:
Time_t mktime (struct TM * timeptr );
The returned value is the converted calendar time. In this way, we can first set a decomposition time and then operate on the time,
The following example shows the day of the week in June:
# Include "time. H"
# Include "stdio. H"
# Include "stdlib. H"
Int main (void)
{
Struct tm t;
Time_t t_of_day;
T. tm_year = 1997-1900;
T. tm_mon = 6;
T. tm_mday = 1;
T. tm_hour = 0;
T. tm_min = 0;
T. tm_sec = 1;
T. tm_isdst = 0;
T_of_day = mktime (& T );
Printf (ctime (& t_of_day ));
Return 0;
}
Running result:
Tue Jul 01 00:00:01 1997
NowNote:Now, with the mktime () function, can we operate any time before now? You can use this method to calculate
What is the day of the week in March? The answer is no. Because this time was before January 1, January 1, 1970
.
5. I also know a system () function that executes the doscommand, system ("A doscommand"); the header file is stdlib. h? Windows. h