C Time Functions

Source: Internet
Author: User

1. Obtain the time:
Use the time () function to obtain the calendar time. Its prototype is time_t time (time_t * timer );

# Include "stdafx. H"
# Include "time. H"
# Include "stdio. H"
# Include "stdlib. H"

Int main (void)
{
Struct TM t; // defines the TM time structure, used to store data information in time format
Time_t t_of_day; // defines the time_t time structure.
T. tm_year = 2006-1900; // The calculation time is based on January 1, 1900.
T. tm_mon = 6; // assign values to struct members
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 );
// Use the mktime () function to convert the time expressed in the TM structure to the calendar time: time_t variable. The function prototype is as follows: time_t mktime (struct TM * timeptr); The ctime () function (the parameter is in the time_t structure) displays the time in a fixed format, the returned value is a char * string.
Return 0;
}

2. Storage of time is stored through two predefined structures:
1. The calendar time is represented by the time_t data type. The time expressed by time_t (calendar time) is from a time point (for example, January 1, 1970 00:00:00) the number of seconds. 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 repeatedly defining time_t */
# Endif
2. In Standard C/C ++, we can obtain the date and time through the TM structure. The definition of the TM structure in time. H is as follows:

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], where 0 indicates Sunday, 1 indicates Monday, and so on */
Int tm_yday;/* Number of days from January 1, 0,365 each year-value range: [1,]. 0 indicates January 1, and so on */
Int tm_isdst;/* identifier of the sequence. When the sequence is executed, the sequence is positive. When the sequence is not implemented, the tm_isdst value is 0. If the sequence is unknown, the tm_isdst () value is negative. */
};

3. display of time:
Time. the H header file provides the asctime () function (the parameter is a TM structure pointer) and ctime () function (the parameter is a time_t structure) to display the time in a fixed format, both return values are char * strings. The returned time format is: day of the week, month, hour, minute, second, year, N, 0, time. H also provides two different functions to convert the calendar time (an integer expressed in time_t) to the time format that we usually see separately displaying year, month, day, hour, minute, and second. TM:
Struct TM * gmtime (const time_t * timer );
The gmtime () function converts the calendar time to the world standard time (Greenwich Mean Time), and returns a TM struct to save the time.
Struct TM * localtime (const time_t * timer); The localtime () function converts the calendar time to the local time.
# Include <stdafx. h>
# Include <time. h>
# Include <stdio. h>
# Include <stdlib. h>

Int main (void)
{
Struct TM * local, * PTR; // defines the storage time information of the TM structure pointer.
Time_t t; // time structure or object
T = time (null); // obtain the calendar time of the Current System
// Use the time () function to obtain the calendar time ),
// Its prototype is time_t time (time_t * timer );
Local = localtime (& T); // The localtime () function converts the calendar time to the local time.
Printf ("Local hour is: % d/N", local-> tm_hour); // time member of the output TM struct
Printf ("UTC hour is: % d/N", local-> tm_hour );
// Local = gmtime (& T );
// The gmtime () function converts the calendar time to the world standard time (Greenwich Mean Time ),
// Return a TM struct to save the time
PTR = gmtime (& T); // converts the calendar time to the world standard time.
Printf ("the UTC time is % s/n", asctime (PTR); // format the output World Standard Time
Printf ("The local time is % s/n", ctime (& T); // output local time
/* The asctime () function (the parameter is a TM structure pointer) and the ctime () function (the parameter is a time_t structure) display the time in a fixed format, both return values are char * strings. The returned time format is: day of the week, month, hour, minute, second, year, N, 0 */
Return 0;
}

4. Time difference calculation:
Function used: the timing function in C/C ++ is clock (), and the related data type is clock_t. The clock function is defined as follows in msdn:
Clock_t clock (void); the number of CPU clock units (clock tick) returned by the function from "enabling this program process" to "calling the clock () function in the program, clock_t is a data type with a long integer and storage time. In the time. h file, a constant clocks_per_sec is also defined to indicate the number of clock units in one second. Its definition is as follows:
# Define clocks_per_sec (clock_t) 1000)
Every 1‰ seconds (1 millisecond), the value returned by calling the clock () function is increased by 1, and the length of the clock timing unit is 1 millisecond, so the timing accuracy is also 1 millisecond, can we change the definition of clocks_per_sec to a greater value, so that the timing accuracy is higher? This is not acceptable. In standard C/C ++, the minimum unit of time is one millisecond. Double difftime (time_t time1, time_t time0); this function is used to calculate the time difference.

# Include "stdafx. H"
# Include "time. H"
# Include "stdio. H"
# Include "stdlib. H"
Int main (void)
{
Time_t c_start, t_start, c_end, t_end;
C_start = clock ();
T_start = Time (null );
System ("pause ");
C_end = clock ();
T_end = Time (null );
Printf ("the pause used % f ms by time ()./N", difftime (c_end, c_start ));
Printf ("the pause used % f s by clock ()./N", difftime (t_end, t_start ));
System ("pause ");
Return 0;
}
5. Other uses of time
Used as the seed of a random number. Since time is actually a long integer of the double type, it is obtained through the time (null) function as srand (Time (null )) is better.

# Include "stdafx. H"
# Include "time. H"
# Include "stdio. H"
# Include "stdlib. H"
Int main (void)
{
Srand (Time (null ));
// Set the seed. If this function is commented out, the random number 10 is the same for each running program.
For (INT I = 0; I <100; I ++)
{
Printf ("% d/T", Rand ());
}
System ("pause ");
Return 0;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.