Time with time-zone programming on Linux

Source: Internet
Author: User
1. Time Format and Examples

String-time: "04:05:06 + 0700"

Struct-time: struct TM {
Tm_sec = 6,
Tm_min = 5,
Tm_hour = 4,
Tm_mday = 3,
Tm_mon = 1,
Tm_year = 101,
Tm_wday = ?,
Tm_yday = ?,
Tm_isdst = ?,
_ Tm_gmtoff = 25200,/* = 7*3600 */
_ Tm_zone = 0x ??? "??? "
}

Long-time: 12345678/* seconds elapsed since 1970.1.1 0: 0 */

2. For communication: String-time is best, as RFC 822 defined.
For time comparing and save into DB, long-time is best.

Struct-time, used between string-time and long-time, and for time changing.

3. Long-time lost the time zone info.

4. Standard-time, remote-time and local-time

Standard-time: UTC time (former GMT time), offset = 0

Local-time: local Linux time and time zone, offset = +/-xxxx

Reomote-time: Time and time-zone on remote site in communication, offset = +/-yyyy

5. glibc (Library) is weak on time functions.

Strptime (): String-Time to struct-time, with user time zone (only for 2.6.x ).
Strftime (): struct-Time to string-time, with user time zone.

Timegm (): struct-Time to long-time, lost time zone.
Mktime (): struct-Time to long-time, with local time zone.

Gmtime_r (): long-time to struct-time, no time zone.
Localtime_r (): long-time to struct-time, with local time zone.

Time (): long-time, lost local time zone.
Timezone: Local Time Zone offset

6. Principle of time programming.

6.1 save long-time into DB. This long-time is Standard Time (UTC time ).
For the parameters of function, use UTC long-time as the time format.

6.2 for incoming data, containing string-time, call strptime () to convert
Struct-time, and keep time zone in a independent variable (for Linux 2.4.x, get
The time zone from the string by yourself), then call timegm () to convert
Long-time, then Minus time-zone in the variable, to get UTC long-time.

6.3 For outgoing data, as a UTC long-time, if the time zone is local, call
Localtime_r () to convert to a struct-time, with time zone (local), then
Call strftime () to get a string-time, with time zone (local ).

6.4 for outgoing data, as a UTC long-time, if the time zone is user, before this
Shocould somewhere a variable saved the offset, add the time zone offset to
Long-time, then call gmtime_r () to get a struct-time, no time zone, then set
The tm_gmtoff of struct time with the offset in the variable, then call
Strftime () to get a string-time, with time zone (user ).

6.5 for local config file, containing string-time, shocould save the time
Time zone, for getting correct time after time zone changed. Otherwise,
Always save UTC long-time or UTC string-time in config file.

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.