我使用自1970年以来的秒(和微秒)以及时区和dst标志来表示date。 我想使用strftime打印date的表示,但是它使用从环境中拾取的时区(extern long int timezone)的全局值。 如何获得strftime来打印我select的区域?
以下程序使用所需的时区设置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); }
通过设置timezone
全局变量来更改时区,并使用localtime
时间获取通过strftime
打印的时间。