【问题标题】:C++ time() gives me almost random resultC++ time() 给了我几乎随机的结果
【发布时间】:2016-08-31 05:54:36
【问题描述】:

我写了以下简单的代码:

time_t makeUnixTimeStamp( int year, int month, int day, int hour, int min, int sec ) {
    tm uts_time;
    uts_time.tm_year = year - 1900;
    uts_time.tm_mon = month - 1;
    uts_time.tm_mday = day;
    uts_time.tm_sec = sec;
    uts_time.tm_min = min;
    uts_time.tm_hour = hour;
    return mktime( &uts_time );
}

std::string getReadableDateTime( unsigned int unixTimeStamp ) {
    char dateTime[ 40 ];
    time_t someTime = unixTimeStamp;
    struct tm *mTime;
    mTime = localtime( &someTime );
    strftime( dateTime, sizeof( dateTime ), "%Y-%m-%d %H:%M:%S", mTime );
    return std::string( dateTime ); 
}


unsigned int startLogTime = makeUnixTimeStamp( 2016, 05, 04, 00, 00, 00 );
time_t nowTime;
time( &nowTime );
std::cout << "readable Time = " << getReadableDateTime( startLogTime ) << '\n';

运行几次后,我得到了奇怪的输出。我用php -r 'echo time();' 显示当前秒数。 如果我不更改代码中的任何内容,为什么会有不同的“可读时间”?

输出:

15:20:58 ~ $ rm a.out && g++ analyze.cpp && ./a.out && php -r 'echo time();'
readable Time = 2016-05-04 00:00:00
1462450865

15:21:05 ~ $ rm a.out && g++ analyze.cpp && ./a.out && php -r 'echo time();'
readable Time = 2016-05-04 00:00:00
1462450866

15:21:06 ~ $ rm a.out && g++ analyze.cpp && ./a.out && php -r 'echo time();'
readable Time = 2016-05-04 00:00:00
1462450867

15:21:07 ~ $ rm a.out && g++ analyze.cpp && ./a.out && php -r 'echo time();'
readable Time = 2016-05-03 23:00:00
1462450868

15:21:08 ~ $ rm a.out && g++ analyze.cpp && ./a.out && php -r 'echo time();'
readable Time = 2016-05-03 23:00:00
1462450869

15:21:09 ~ $ rm a.out && g++ analyze.cpp && ./a.out && php -r 'echo time();'
readable Time = 2016-05-04 00:00:00
1462450871

15:21:11 ~ $ rm a.out && g++ analyze.cpp && ./a.out && php -r 'echo time();'
readable Time = 2016-05-03 23:00:00
1462450872

15:21:12 ~ $ rm a.out && g++ analyze.cpp && ./a.out && php -r 'echo time();'
readable Time = 2016-05-04 00:00:00
1462450877

15:21:17 ~ $ rm a.out && g++ analyze.cpp && ./a.out && php -r 'echo time();'
readable Time = 2016-05-04 00:00:00
1462450882

15:21:22 ~ $ rm a.out && g++ analyze.cpp && ./a.out && php -r 'echo time();'
readable Time = 2016-05-03 23:00:00
1462450883

似乎如果我删除 time() 函数 - 效果会更好,但在代码之后我需要它。

【问题讨论】:

    标签: c++ date time


    【解决方案1】:

    您应该设置 DST 标志。可能是随机初始化的

    如果夏令时有效,夏令时标志 (tm_isdst) 大于零,如果夏令时无效,则为零,如果信息不可用,则小于零。

    http://www.cplusplus.com/reference/ctime/tm/

    一个有用的方法是先用当前本地时间初始化 tm 结构,这样它的设置方式就与你机器上的其他所有时间一样。

    time_t now = time(0);
    uts_time = * localtime( &now );
    // initialise with the time you really want
    

    【讨论】:

    • 如何正确操作? :)
    • 你能在我的代码中展示它的样子吗?因为我得到:候选函数(隐式复制赋值运算符)不可行:第一个参数没有从 'struct tm *' 到 'const tm' 的已知转换;用 * 取消引用参数
    • 抱歉,忘记从本地时间取消引用返回。答案已编辑
    【解决方案2】:

    您的tm 结构中有一些未初始化的部分:读回任何未初始化部分的行为是未定义

    改用tm foo{}; 之类的代码,这会导致所有结构元素初始化为零值(以及指向空指针值的指针)。

    【讨论】:

    • tm uts_time{} 给我:error: expected ';'在声明结束时
    • 你忘记;了吗?
    • 如果他目前在夏令时,这会引起混乱
    • 但至少它会是明确定义的混乱,至少在我看来,这不是那么混乱。
    • @Bathsheba,当然:error: expected ';' at end of declaration tm uts_time{};
    猜你喜欢
    • 2014-04-08
    • 2019-07-25
    • 1970-01-01
    • 2011-01-09
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    • 1970-01-01
    • 2013-04-30
    相关资源
    最近更新 更多