Summary of frequent use of time functions

Source: Internet
Author: User
Tags month name ranges
In a computer, time generally has three functions: time, timing, and timing. This article will mainly introduce some frequently used time-expressing functions and their conversions. Other usage methods will be added in the hope. I. Basic Concepts 1. the World Standard Time (UTC, coorainated Universal Time) is the most basic world time standard. It is based on atomic time, and is as close as possible to the year (GMT, greenwich Mean Time). In most cases, UTC is equivalent to GMT, but GMT is no longer precisely defined by the scientific community. 2. Local time refers to the local time relative to UTC/GMT (time zone conversion ). For example, the time difference between mainland China and UTC is + 8, that is, UTC + 8. America is UTC-5. 3. The calendar time is the number of seconds that have elapsed since a standard time point (EPOCH). It does not include the adjustment of the time when a leap second is inserted. The standard start time of the timer. Various compilers generally use, January 1, January 1, 1970. The calendar time is represented by the Data Type time_t. The time_t type is usually a 32-bit or 64-bit integer type. Note: is a leap second added in Coordinated Universal Time (UTC? Or reduce it by one second, so that it is close to the adjustments made during normal hours. 4. The clock tick count (clock tick) starts from the start of the process, so this is the relative time. Every second includes clocks_per_sec (a constant defined in time. H, usually 1000. The number of clock tick messages is represented by the Data Type clock_t. Clock_t is usually a 32-bit integer. 5. The broken-down time is represented by the structure data type TM. The TM includes the following structure members:
# Ifndef _ tm_definedstruct TM {int tm_sec;/* Second-value range: [] */INT tm_min;/* minute-value range: [] */INT tm_hour; /* Time-value range: [] */INT tm_mday;/* date in a month-value range: [] */INT tm_mon; /* month (starting from January 1, with 0 representing January 1,)-value range: [] */INT tm_year ;/* Year, whose value is equal to the actual year minus 1900*/INT tm_wday;/* the week-value range is [0, 6], where 0 represents Sunday, 1 represents Monday, and so on */INT tm_yday; /* The number of days starting from January 1, 0,365-the value range is []. 0 indicates January 1, and so on. */INT tm_isdst;/* indicates the identifier of the hour, when the sequence is executed, tm_isdst 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. */}; # DEFINE _ tm_defined # endif
Ii. conversion time overview
The asctime, ctime, gmtime, and localtime functions are non-thread-safe. The corresponding thread-safe versions include asctime_r, ctime_r, gmtime_r, and localtime_r. iii. specific descriptions of various functions 1. mktime time_t Mktime(Struct TM * PTM)
The conversion from the decomposition time TM to the calendar time time_t. 2. timegm time_t Timegm(Struct TM * brokentime)
The conversion from the decomposition time TM (treated as UTC time, regardless of the local time zone settings) to the calendar time time_t. This function is rarely used.
3. gmtime struct TM * Gmtime(Const time_t * timer)
The conversion from the calendar time time_t to the decomposition time TM. The function returns a static TM-structured bucket, which is shared by the gmtime, localtime, and ctime functions. each call to these functions overwrites the content of the TM structure bucket.
4. localtime struct TM * Localtime(Const time_t * timer)
The conversion from the calendar time time_t to the decomposition time TM, that is, the result data has been adjusted to the local time zone and hour. 5. asctime char * Asctime(Const struct TM * tmptr)
Output the decomposition time TM to the string in the format of "www Mmm dd hh: mm: ss yyyy", that is, "number of months in a week, number of days, hours: minutes: in seconds ". The string returned by the function is statically allocated and cannot exceed 26 characters in length. It is shared with the ctime function. Each function call overwrites the string content.
6. ctime char * Ctime(Const time_t * timer)
Output the calendar time time_t timer to a string in the same format as the asctime function.
7. strftime size_t Strftime(Char * s, size_t N, const char * format, const struct TM * tptr) converts the decomposition time TM to a custom string, similar to the common string format output function sprintf. 8. strptime char * strptime (const char * Buf, const char * format, struct TM * tptr) strftime inverse operation converts a string to the decomposition time TM according to its own defined format.
Appendix: formatting commands used in strptime and strftime:
% A abbreviation of the day of the week
% A full name of the day of the week
Abbreviation of % B month
% B full name
% C standard date time string
The last two digits of the Year % C
% D indicates the day of the month in decimal format.
% D month/day/year
% E indicates the day of the month in decimal format in the two-character field
% F-month-day
The last two digits of the Year % G. The year is based on the week.
% G year, based on the week Year
% H abbreviated month name
% H in 24-hour format
% I 12-hour
% J indicates the day of the year in decimal format.
Month in % m decimal format
% M decimal number of minutes
% N new line operator
% P equivalent display of local am or PM
% R 12 hours
% R display hour and minute: hh: mm
% S decimal seconds
% T horizontal Tab
% T display time in seconds: hh: mm: SS
% U the day of the week, Monday is the first day (value ranges from 0 to 6, Monday is 0)
% U indicates the week of the year. Sunday is regarded as the first day (the value ranges from 0 to 53)
% V indicates the week of the year, which is based on the year of the week.
% W decimal indicates the day of the week (the value ranges from 0 to 6, and Sunday is 0)
% W indicates the week of the year. Monday is the first day (from 0 to 53)
% X standard date string
% X standard time string
% Y does not contain the century's decimal year (the value ranges from 0 to 99)
% Y indicates the decimal Year of the century.
% Z, % Z Time Zone name. If the time zone name cannot be obtained, an empty character is returned.
% Percent sign


1. Conversion of Date and Time time_t and struct TM in time. H2. C/C ++ in Wikipedia 3. C, C ++ time and date functions

Summary of frequent use of time functions

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.