首页 > 解决方案 > struct tm 在通过函数时变为 PST

问题描述

再会!我在 GMT 中遇到 struct tm 的问题,但是当通过一个函数时,它会减去 8 小时并转到我的当地时间 (PST)

void TestFunction(glob_t* globbuf, struct tm *tm)
{
    char buffx[300];
    time_t timeif = timegm(tm);


    strftime(buffx, 100, "%Y-%m-%d %H:%M:%S.000", gmtime(&timeif));
    //print the buffx
    // the print shows the time in PST, -8 hours from what it was because at this point tm becomes
    // PST
}

int main()
{
    time_t t = time(NULL);
    struct tm *tm = gmtime(&t);
    tm->tm_sec = 0;

    char buffx[300];
    time_t timeif = timegm(tm);


    strftime(buffx, 100, "%Y-%m-%d %H:%M:%S.000", gmtime(&timeif));
    //print the buffx
    // Prints the time in GMT.
    // Do some other unrelated stuff

    TestFunction(&unRelatedParameterOne, tm); // Variable tm is in GMT

    return 0;
}

标签: cwindows-subsystem-for-linux

解决方案


我现在将 time_t timeif 传递给函数。如果在通过函数时不改变,这将作为时间起作用。

我仍然不知道为什么 struct tm 会改变时区。


推荐阅读