【问题标题】:struct tm becomes PST when passing through a functionstruct tm 在通过函数时变为 PST
【发布时间】:2020-12-30 00:59:27
【问题描述】:

美好的一天!我在 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;
}

【问题讨论】:

  • 无法复制。也许不相关的东西相关的。

标签: c windows-subsystem-for-linux


【解决方案1】:

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

我还是不知道为什么 struct tm 会改变时区。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-16
    • 2015-09-09
    • 2015-05-06
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    • 2014-07-24
    • 2012-08-28
    相关资源
    最近更新 更多