将字符串时间转换为unix时间,反之亦然

jdo*_*eer 7 c++ unix-timestamp

我一直试图简单地将字符串"1998-04-11"转换为UNIX时间戳,根据在线转换器应该是892245600.

但我一直得到不同的结果.

    struct tm tm;
    time_t ts;

    strptime("1998-04-11", "%Y-%m-%d", &tm);
    tm.tm_mon = tm.tm_mon -1;
    ts = mktime(&tm);

    printf("%d \n", (int)ts); //unix time-stamp
    printf("%s \n", ctime(&ts)); //human readable date 
Run Code Online (Sandbox Code Playgroud)

结果:

893502901
Sat Apr 25 13:15:01 1998
Run Code Online (Sandbox Code Playgroud)

谁能告诉我我做错了什么?

nnn*_*nnn 7

tm调用前将结构归零strptime

memset(&tm, 0, sizeof(struct tm));
Run Code Online (Sandbox Code Playgroud)

来自以下网站的笔记部分:http://man7.org/linux/man-pages/man3/strptime.3.html

原则上,此函数不会初始化tm,只会存储指定的值.这意味着tm应该在调用之前初始化.

并且memset在同一页面的示例中使用如上所述.