Linux Time Conversion

Source: Internet
Author: User
Tags time zones string format

Introduction: Write code today to encounter a time conversion of the sudden change of the function is not too sure to open the record before looking at it and then by the way to send here

One: Data structure

Linux storage time is common in two ways, an absolute number of seconds, one is to use a structure to store the date and time of each

Header file #include <time.h>

typedef long time_t; In fact, time_t is a long type of time function can be obtained from 1970 to now how much

struct TM {

int tm_sec; /* seconds – The value interval is [0,59] */

int tm_min; /* min-value interval is [0,59] */

int tm_hour;/* When-the value interval is [0,23] */int tm_mday;/* Date in one months-value range is [1,31] */int Tm_mon;/* Month (starting from January, 0 for January)-value range is [0,11] * /int tm_year;/* year, whose value starts at 1900 * /int tm_wday;/* Week – The value interval is [0,6], where 0 stands for Sunday, 1 for Monday, and so on * /int tm_yday;/* Number of days starting January 1 of each year – the value interval is [0,365], where 0 is January 1, 1 is January 2, and so on .int tm_isdst;/* Daylight Saving time identifier, TM_ISDST is positive when daylight savings is applied. Without daylight saving time, TM_ISDST is 0 and TM_ISDST () is negative when the situation is not known. */a long int tm_gmtoff;/* Specifies a negative number of seconds in the East time zone of the Date Line east of UTC or UTC in the West time zone */const char *tm_zone;/* The name of the current time zone (related to the environment variable TZ) */};
Two: function (collation memory)the number of seconds to return differs in that mktime can be returned by way of the TM structureThe 1:time () function returns the number of seconds from the current time since the Unix era (January 1 1970 00:00:00 GMT) 2:time_t mktime (struct TM * timeptr); Convert time to seconds since January 1, 1970The return string differs in that the incoming parameter is not the same3:char *asctime (const struct tm* timeptr); Converts information in a structure to real-world time, as a string4:char *ctime (const time_t *TIMEP); converts TIMEP to True world time, displayed as a stringThe return TM structure differs in that localtime returns the current time zone5:struct tm* gmtime (const time_t *TIMEP); Converts the time represented by time_t to UTC time without time zone conversion (0 o'clock Zone)6:struct tm* localtime (const time_t *TIMEP); Similar to Gmtime, but it is time-zone-converted to return to the current time zone     

PS:UTC (World coordination Time) equals the average solar time of 0 time zones on the Prime meridian (i.e. longitude 0 degrees)

GMT (Greenwich) Same as UTC Beijing is in the East eight district, Beijing time is ahead of UTC eight hours

DST (Summer saving time) I see a little less niang a bit is in the summer when the sun rises relatively early, the clock is set to one hour faster, to early use of daylight,

In the UK it is called Daylight Saving time (Summer times), or why Boston is 12 hours apart from China in the summer 13

CST (Central Standard Time) focus on this. In general, we use the date command to display the CST format under Linux.
In fact, CST is not just representative of China time actually represents the 4 urban time of the United States Australia Cuba China

CST Central Standard Time (USA) ut-6:00
CST Central Standard Time (Australia) ut+9:30
CST China Standard Time ut+8:00
CST Cuba Standard Time ut-4:00
Linux is the current real time zone by/etc/localtime the time zone file under the/usr/share/zoneinfo that the hard connection points to
For example, when/etc/localtime points to the/usr/share/zoneinfo/asia/shanghai file, CST represents the Chinese standard time.
The CP command copy can also be used for example
[[email protected] ~]# cp/usr/share/zoneinf/asia/shanghai/etc/localtime//Copy the appropriate time zone file
[[email protected] ~]# hwclock//write BIOS to avoid restart failure


Three: examples

1: Display the current time common format Fri Jan 11 17:25:24 2014

#include <stdio.h> #include <stdlib.h> #include <time.h> int main () {       time_t timep;       Time (&TIMEP); /* Get the current time of the time_t type *       /printf ("%s", Asctime (Gmtime (&TIMEP)));/* Use gmtime to convert the time of the time_t type to the time of the struct TM type, */       printf ("%s", CTime (&TIMEP));//This comparison is due to the fact that the time zone is changed to date by       return 0;  }


2: Absolute number of seconds to format time_t seconds structure----&GT;LOCALTIME TM structure------>asctime to String
time_t second number structure----&GT;LOCALTIME TM structure------>strftime to format

#include <stdio.h> #include <stdlib.h> #include <time.h>int main () {    time_t timep;    struct TM *test = NULL;    Char tm[100];    TIMEP = 1378742400;    printf ("%s\n", Asctime (LocalTime (&time));//Turn string format    struct TM *tmtest = NULL;    Test = localtime (&TIMEP);    if (Strftime (tm, sizeof (TM), "%y-%m-&d%h:%m:%s", test))       return-1;    printf ("%s\n", TM);    return 0;}     


3: String to absolute number of seconds the last is used to mktime to absolute number of seconds code no example



Linux Time Conversion

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.