简体   繁体   English

更改时区不更改时区的日期格式

[英]Changing time zone not changing the date format according to time zone

In the below code I try to display the pdt and gmt format of epoch time value "1411636989", but it changes only time zone not the date, following is the sample output. 在下面的代码中,我尝试显示纪元时间值“ 1411636989”的pdt和gmt格式,但它仅更改时区而不更改日期,以下是示例输出。

int main()
{
    time_t my_time = 1411636989;

    if (putenv("TZ=PDT"))
        printf("Current time zone = %s###########\n", getenv("TZ"));
    else {
        printf("Time zone = %s###########\n", getenv("TZ"));
        printf("PDT 1411636989 = %s$$$$$$$$$$$$$$$$$$\n", asctime(localtime(&my_time)));
    }

    if (putenv("TZ=GMT"))
        printf("putenv failed errno = %d##########\n",errno);
    else {
        printf("New time zone = %s###########\n", getenv("TZ"));
        printf("GMT 1411636989 = %s$$$$$$$$$$$$$$$$$$\n", asctime(localtime(&my_time)));
    }

    return 0;
}

Sample O/P:
Time zone = PDT###########
PDT 1411636989 = Thu Sep 25 09:23:09 2014
$$$$$$$$$$$$$$$$$$
New time zone = GMT###########
GMT 1411636989 = Thu Sep 25 09:23:09 2014
$$$$$$$$$$$$$$$$$$

You'll have to use one of the timezone matching a file name below the /usr/share/zoneinfo/ directory. 您必须使用与/ usr / share / zoneinfo /目录下的文件名匹配的时区之一。 A TZ of PDT isn't going to be recognized. PDT的TZ不会被识别。 The system will fall back to GMT if your TZ variable isn't valid. 如果您的TZ变量无效,系统将退回到GMT。

Set it to "US/Pacific". 将其设置为“美国/太平洋”。

putenv("TZ=US/Pacific")

Since time_t is usually represented in seconds use normal arithmetic to add value to my_time . 由于time_t通常以秒表示,因此请使用常规算术将值添加到my_time

Before if(putenv("TZ" = "GMT")) add this line to increase the time to GMT: if(putenv("TZ" = "GMT"))添加以下行以增加到达GMT的时间:

my_time += (7 * 60 * 60);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM