How can I set the time zone before calling strftime?

后端 未结 2 1536
终归单人心
终归单人心 2020-12-18 04:53

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

相关标签:
2条回答
  • 2020-12-18 05:13

    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);
    }
    
    0 讨论(0)
  • 2020-12-18 05:23

    Change timezone via setting timezone global variable and use localtime to get the time you print via strftime.

    0 讨论(0)
提交回复
热议问题