C + + timestamp conversion (related to GMT CST time zone conversions)

Source: Internet
Author: User
Tags cst time time zones



Origin of the problem



Timestamp conversion (timestamp: The total number of seconds from January 1, 1970 (00:00:00) to the current time. )


#include <stdio.h>
#include <time.h>

int main(int argc, const char * argv[])
{    
    time_t t;
    struct tm *p;
    t=1408413451;
    p=gmtime(&t);
    char s[80];
    strftime(s, 80, "%Y-%m-%d %H:%M:%S", p);
    printf("%d: %s\n", (int)t, s);
}


Results


1408413451      2014-08-19 01:57:1408384651


However, the results of using the command on the Linux terminal differ


 
[###t]$ date -d @1408413451
Tue Aug 19 09:57:31 CST 2014


By comparison, the two are just 8 hours, CST means Greenwich Mean time, through the strftime () function can output timezone, correct the following


#include <stdio.h>
#include <time.h>

int main(int argc, const char * argv[])
{    
    time_t t;
    struct tm *p;
    t=1408413451;
    p=gmtime(&t);
    char s[80];
    strftime(s, 80, "%Y-%m-%d %H:%M:%S::%Z", p);
    printf("%d: %s\n", (int)t, s);
}


Results


1408413451:2014-08-19 01:57:31::GMT





Bottom



GMT (Greenwich Mean time) represents GMT. In 17th century, the Royal Observatory of Greenwich was observing astronomical objects for the expansion of its naval hegemony. The old Royal Observatory was formally established in 1675, using the Meridian of Greenwich to divide the Earth's two hemispheres by longitude 0 degrees. There is a sign on the wall of the observatory 24 Hour clock, showing the current time, for the world, the time set here is the world time reference point, the whole world in Greenwich time as standard to set the time, this is our familiar "Greenwich Standard Time" (Greenwich Mean Times , referred to as g.m.t.) The origin.



CST can also represent the following 4 different time zones:


Central Standard Time (USA) UT-6:00
Central Standard Time (Australia) UT+9:30
China Standard Time UT+8:00
Cuba Standard Time UT-4:00


It can be seen that CST could also represent the standard time in four countries of the United States, Australia, China, and Cuba.



Okay, the difference between the two is 8 hours (CST than GMT late/large 8 hours), GMT+8*3600=CST, the code is as follows


#include <stdio.h>
#include <time.h>

int main(int argc, const char * argv[])
{    
    time_t t;
    struct tm *p;
    t=1408413451;
    p=gmtime(&t);
    char s[80];
    strftime(s, 80, "%Y-%m-%d %H:%M:%S::%Z", p);
    printf("%d: %s\n", (int)t, s);

    t=1408413451 + 28800;
    p=gmtime(&t);
    strftime(s, 80, "%Y-%m-%d %H:%M:%S", p);
    printf("%d: %s\n", (int)t, s);
    return 0;
}


Results


1408413451: 2014-08-19 01:57:31::GMT
1408442251: 2014-08-19 09:57:31


Linux Platforms


Tue 09:57:31 CST 2014







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.