将字符串时间转换为 unix 时间,反之亦然
Convert string time to unix time and vice versa
我一直在尝试将字符串“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
结果:
893502901
Sat Apr 25 13:15:01 1998
谁能告诉我我做错了什么?
在调用 strptime
之前将 tm
结构清零
memset(&tm, 0, sizeof(struct tm));
来自注释部分:http://man7.org/linux/man-pages/man3/strptime.3.html
In principle, this function does not initialize tm
but stores only
the values specified. This means that tm
should be initialized
before the call.
并且memset
在同一页面的示例中如上所示使用。
这是未初始化内存的问题。
(gdb) p tm
= {tm_sec = 1, tm_min = 0, tm_hour = 4196061, tm_mday = 0, tm_mon = -5984, tm_year = 32767,
tm_wday = 0, tm_yday = 0, tm_isdst = 4195984, tm_gmtoff = 4195616,
tm_zone = 0x7fffffffe980 "[=10=]1"}
如您在调试器中所见,struct tm
分配了随机内存。制作 time_zone 偏移量垃圾。
strptime
运行后:
(gdb) p tm
= {tm_sec = 1, tm_min = 0, tm_hour = 4196061, tm_mday = 11, tm_mon = 3, tm_year = 98,
tm_wday = 6, tm_yday = 100, tm_isdst = 4195984, tm_gmtoff = 4195616,
tm_zone = 0x7fffffffe980 "[=11=]1"}
此外:
tm.tm_mon = tm.tm_mon -1;
不需要。更正后的代码:
#include <time.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
struct tm tm;
time_t ts = 0;
memset(&tm, 0, sizeof(tm));
strptime("1998-04-11", "%Y-%m-%d", &tm);
ts = mktime(&tm);
printf("%d \n", (int)ts); //unix time-stamp
printf("%s \n", ctime(&ts)); //human readable date
}
输出:
892252800
Sat Apr 11 00:00:00 1998
我一直在尝试将字符串“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
结果:
893502901
Sat Apr 25 13:15:01 1998
谁能告诉我我做错了什么?
在调用 strptime
tm
结构清零
memset(&tm, 0, sizeof(struct tm));
来自注释部分:http://man7.org/linux/man-pages/man3/strptime.3.html
In principle, this function does not initialize
tm
but stores only the values specified. This means thattm
should be initialized before the call.
并且memset
在同一页面的示例中如上所示使用。
这是未初始化内存的问题。
(gdb) p tm
= {tm_sec = 1, tm_min = 0, tm_hour = 4196061, tm_mday = 0, tm_mon = -5984, tm_year = 32767,
tm_wday = 0, tm_yday = 0, tm_isdst = 4195984, tm_gmtoff = 4195616,
tm_zone = 0x7fffffffe980 "[=10=]1"}
如您在调试器中所见,struct tm
分配了随机内存。制作 time_zone 偏移量垃圾。
strptime
运行后:
(gdb) p tm
= {tm_sec = 1, tm_min = 0, tm_hour = 4196061, tm_mday = 11, tm_mon = 3, tm_year = 98,
tm_wday = 6, tm_yday = 100, tm_isdst = 4195984, tm_gmtoff = 4195616,
tm_zone = 0x7fffffffe980 "[=11=]1"}
此外:
tm.tm_mon = tm.tm_mon -1;
不需要。更正后的代码:
#include <time.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
struct tm tm;
time_t ts = 0;
memset(&tm, 0, sizeof(tm));
strptime("1998-04-11", "%Y-%m-%d", &tm);
ts = mktime(&tm);
printf("%d \n", (int)ts); //unix time-stamp
printf("%s \n", ctime(&ts)); //human readable date
}
输出:
892252800
Sat Apr 11 00:00:00 1998