如何设置调用strftime前的时区? [英] How can I set the time zone before calling strftime?
问题描述
我重新用秒(和毫秒)自1970年以来present日期以及时区和DST标志。我想打印使用的strftime日期重新presentation,但它使用的是从环境拿起时区的全球价值(的extern长整型时区)。我怎样才能得到的strftime打印我的选择的区域?
I represent dates using seconds (and microseconds) since 1970 as well as a time zone and dst flag. I want to print a representation of the date using strftime, but it uses the global value for timezone (extern long int timezone) that is picked up from the environment. How can I get strftime to print the zone of my choice?
推荐答案
下面的程序设置UNIX环境变量TZ与您所需的时区,然后使用的strftime打印格式的时间。
The following program sets the UNIX environment variable TZ with your required timezone and then prints a formatted time using strftime.
在时区下面的例子中被设定为美国太平洋时区。
In the example below the timezone is set to U.S. Pacific Time Zone .
#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);
}
这篇关于如何设置调用strftime前的时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!