Linux system Time function

Source: Internet
Author: User
Tags local time

First of all, I have some understanding of the engineering process:

1, input time_t, Output TM Format function loctaltime (time_t)/gmtime (time_t)

Where localtime is affected by time zone and daylight saving time, that is, the system takes the value of the time_t format of the function input as UTC time, and then offsets the hour by the local TZ environment variable to get a TM format;

  gmtime does not do the environment variable related processing, directly obtains the TM format value.

2, Input tm, Output time_t Format function mktime()

This function is also affected by the TZ environment variable, which is precisely affected by the daylight saving time flag. If the TM_ISDST member in the TM is 1, when the conversion is made, MK automatically obtains the daylight saving time information for the program's operating environment, so the results are affected by the TM member and the Daylight savings rate offset.

3, string Format function CTime (time_t) Asctime (tm)

The corresponding value is printed according to the local time environment variable.

The following excerpt from The good post of netizens

The conversion relationships of system functions are as follows:

Conversion between the time_t and the struct TM

struct TM {

int tm_sec; /*seconds (0-60) */

int tm_min; /*minites (0-59) */

int tm_hour; /*hours (0-23) */

int tm_mday; /*day of the Month (1-31) */

int Tm_mon; /*month (1-12) */

int tm_year; /*year since 1900*/

int tm_wday; /*day of the week (Sunday = 0) */

int tm_yday; /*day in the year (0-365; 1 Jan = 0) */

int tm_isdst; /*daylight saving Time flag > 0:DST are in effect; = 0:DST is not effect; < 0:DST information not available*/
};

The Structure TM decomposes the date and time into separate fields, which makes it easy for the program to get different field values to handle. The upper limit for field tm_sec is 60 instead of 59, which is designed to consider leap seconds and occasionally use it to adjust the human calendar to a precise astronomical year (the so-called regression year). If the _bsd_source test macro is defined in the program, then the TM structure with the GLIBC definition also includes two fields, a long int tm_gmtoff, which represents the number of seconds east of UTC, and one for the const char* Tm_zone, The abbreviation used to represent the time zone (for example, CEST for Central European Daylight savings).

The Gmtime () and localtime () two functions convert time_t to a struct TM. Gmtime () the tm,localtime () that breaks the time_t directly into UTC time will need to consider the time zone and daylight saving settings, as stated below:

#include <time.h>

Both return a pointer to a statically allocated broker-down time structure on success, or NULL on error

struct tm* gmtime (const time_t *TIMEP);

struct tm* localtime (const time_t *TIMEP);

The above two functions are non-thread safe, the thread-safe version is Gmtime_r () and Localtime_r ()

The Mktime () function converts a struct TM to time_t, which is declared as follows:

time_t mktime (struct TM *timeptr);

The function may modify the value of the timeptr, at least to ensure that the Tm_wday and Tm_yday fields are set up, ensuring that the fields correspond to each other. Also, Mktime () sets the time zone when the conversion occurs. In addition, the use of DST settings depends on the value of the input field tm_isdst.

    • If TM_ISDST is 0, this is considered a standard time (that is, ignoring daylight savings)
    • If TM_ISDST is greater than 0, consider this time as daylight saving time
    • If TM_ISDST is less than 0, an attempt is made to determine whether DST takes effect at this time of year. This is often a desirable setting

defined in the/usr/share/zoneinfo. The settings of the time zone affect functions such as CTime (), localtime (), Mktime (), strftime (), and in order to get the time zone setting, these functions call Tzset (3) to set the following global variables:

Char *tzname[2]; /*name of TimeZone and alternate (DST) timezone*/

int daylight; /*nonzero If there is an alternate (DST) timezone*/

Long timezone; /*seconds difference between UTC and local standard time*/

Linux system Time function

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.