[STL] C ++ language ctime Library

Source: Internet
Author: User

1. Type
Clock_t: it is a long type, used to record the number of clock timing units in a period of time, that is, the CPU running unit time.
Size_t: defined in the Standard C library. It should be an unsigned int and a long unsigned int in a 64-bit system.
Time_t: the number of seconds that have elapsed since 00:00:00, January 1, January 1, 1970 to this time point.
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. */
};
2. Time operations
Clock: the number of clock units returned. Program Start running.
Time: returns the current time_t.
Difftime: calculate the time difference between time_t.
3. Conversion
Mktime: Convert TM structure to time_t
Asctime: Convert TM structure to string
Ctime: converts time_t to a string.
Gmtime: Convert time_t to TM as UTC time
Localtime: Convert time_t to TM as local time
Strftime: formatted as a string
Several Functions converted into strings: asctime, ctime, strftime
4. Macro
Clocks_per_sec: indicates the number of clock units in a second.

// Measurement event duration void test_clock_t () {long I = 1_l; clock_t start, finish; double duration; Start = clock (); /* measure the duration of an event */while (I --) {}; finish = clock (); duration = (double) (finish-Start)/clocks_per_sec; printf ("time to do 100000000 empty loops is % F seconds \ n", duration);} void test_time_t () {time_t t = time (null ); printf ("the calendar time now is % d \ n", T);} void test_difftime () {time_t start, end; Start = Time (null ); system ("pause"); End = Time (null); printf ("the pause used % 5.4f seconds. \ n ", difftime (end, start) ;}// the following are some Conversion Function applications // mktime: TM --> time_cvoid test_mktime () {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_wday = 4;/* day of the week */t. tm_yday = 0;/* does not show in asctime */t. tm_isdst = 0; t_of_day = mktime (& T); printf (ctime (& t_of_day);} // localtime: time_c --> tmvoid test_localtime () {time_t rawtime; struct TM * timeinfo; time (& rawtime); timeinfo = localtime (& rawtime); printf ("current local time and date: % s", asctime (timeinfo ));} // gmtime: time_c --> tmvoid test_gmtime () {time_t rawtime; struct TM * timeinfo; time (& rawtime); timeinfo = gmtime (& rawtime ); printf ("UTC time and date: % s", asctime (timeinfo);} // ctime: time_t --> stringvoid test_ctime () {time_t t = time (null ); STD: String STR = ctime (& T); STD: cout <STR <STD: Endl ;}
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.