gpt4 book ai didi

c - 如何在调用 strftime 之前设置时区?

转载 作者:太空狗 更新时间:2023-10-29 17:22:33 25 4
gpt4 key购买 nike

我使用自 1970 年以来的秒(和微秒)以及时区和夏令时标志来表示日期。我想使用 strftime 打印日期表示,但它使用从环境中获取的时区全局值(extern long int timezone)。如何让 strftime 打印我选择的区域?

最佳答案

以下程序使用您所需的时区设置 UNIX 环境变量 TZ,然后使用 strftime 打印格式化时间。

在下面的示例中,时区设置为美国太平洋时区。

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

int main (int argc, char *argv[])
{
struct tm *mt;
time_t mtt;
char ftime[10];

setenv("TZ", "PST8PDT", 1);
tzset();
mtt = time(NULL);
mt = localtime(&mtt);
strftime(ftime,sizeof(ftime),"%Z %H%M",mt);

printf("%s\n", ftime);
}

关于c - 如何在调用 strftime 之前设置时区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1620188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com