Linux uses time functions with timezone conversion considerations (negative negative positive) __oracle

Source: Internet
Author: User
Tags local time
Linux time structure definition:


struct TM
{
int tm_sec; Represents the current number of seconds, with a normal range of 0-59, but allowed to 61 seconds
int tm_min; Represents the current score, range 0-59
int tm_hour; Number of hours from midnight, range 0-23
int tm_mday; Number of days in the current month, range 01-31
int Tm_mon; Represents the current month, from January onwards, ranging from 0-11
int tm_year; Number of years since 1900
int tm_wday; The number of days in a week, from Monday, the range is 0-6
int tm_yday; From January 1 This year, the number of days, the range is 0-365
int tm_isdst; The flag of daylight saving time

};

1, time (get the current UTC seconds, you need to note that the function has no timezone conversion)


#include <time.h>


time_t time (time_t *t);


This function should be considered one of the most commonly used functions, return the UTC standard seconds, no time zone conversion, and call this function return the same value in London and Shanghai.


2, Gmtime (get the current time structure, UTC time, you need to note that the function has no time zone conversion)



#include <time.h>
struct TM *gmtime (const time_t *TIMEP);


struct TM *gmtime_r (const time_t *TIMEP, struct TM *result);


The two functions mean the same, converting TIMEP this number of seconds to the UTC time zone, which is the standard date of day.
The Gmtime_r is thread-safe and is recommended for use. Gmtime returned with a struct tm*,
This pointer points to a static memory, and the area is often altered. You just called gmtime (),
Execute a few other commands, and then want to use just gmtime () to get struct TM, you will find that the content is wrong,
So it's dangerous, after using Gmtime_r, there's no problem, Gmtime_r will save the results to your incoming memory.


Obtain the struct TM structure, note the definition of the structure, if you want to print the year +1900, month +1.


3, localtime (get the current time structure, local time, but sometimes the area conversion)


#include <time.h>


struct TM *localtime (const time_t * TIMEP);


struct TM *localtime_r (const time_t *TIMEP, struct TM *result);




The two functions also mean the same, the local time based on time zone information, and the Localtime_r version is also recommended.


4, Mktime (converts the time structure to UTC seconds, but sometimes the area conversion)


#include <time.h>
time_t mktime (struct TM *tm);


Converts a struct TM that has already been calculated based on time zone information to a time_t number of seconds. The number of seconds calculated is the same as the UTC time, which is the same concept as the number of seconds that are obtained by the call Times ().


Return to the topic of the article, why say negative negative positive,
Like the UTC time of 1519741060, when I use

Gmtime_r

When converting to a struct TM T structure, and then converting the mktime function to a number of seconds, the discovery time is reduced by 8 hours, which is 8 time zone, because Mktime thinks your structure has a time zone.


Therefore, if you want to convert the number of seconds to a TM structure, the correct way to convert the number of seconds from the TM structure is to use Localtime_r to the tm,mktime of the sometimes zone and then remove the seconds before the time zone returns. This is the negative negative positive.


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.