mktime返回错误的时间戳(关闭一整个月) [英] mktime returns wrong timestamp (off by a whole month)
本文介绍了mktime返回错误的时间戳(关闭一整个月)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 mktime
从我当前的当地时间创建一个unix时间戳记:
I use mktime
to create a unix timestamp from my current local time:
#include <time.h>
int _tmain(int argc, _TCHAR* argv[])
{
struct tm info;
// 16.05.2014
info.tm_mday = 16;
info.tm_mon = 5;
info.tm_year = 114; // Years since 1900
// 08:00:00 Uhr
info.tm_hour = 8;
info.tm_min = 0;
info.tm_sec = 0;
// Convert to Unix timestamp
info.tm_isdst = -1;
time_t timestamp = mktime(&info);
printf("Timestamp: %i", timestamp);
}
这给了我:
1402898400
1402898400
当转换回到人类可读的时间(通过某些网站)到:
When converting this back to a human readable time (via some website) this translates to:
16.06.2014 08:00:00
16.06.2014 08:00:00
你可以看到这是从我预期的一个月(放在五月(5),六月(6))。
为什么?
As you can see this is one month off from what I expected (put in May (5), got out June (6)). Why?
推荐答案
C11 7.27.1时间成分
C11 7.27.1 Components of time
int tm_sec; // seconds after the minute — [0, 60]
int tm_min; // minutes after the hour — [0, 59]
int tm_hour; // hours since midnight — [0, 23]
int tm_mday; // day of the month — [1, 31]
int tm_mon; // months since January — [0, 11]
int tm_year; // years since 1900
int tm_wday; // days since Sunday — [0, 6]
int tm_yday; // days since January 1 — [0, 365]
int tm_isdst; // Daylight Saving Time flag
tm_mon
开始于 0
,而不是 1
。所以 5
的值意味着它是六月,而不是五月。
tm_mon
starts with 0
, not 1
. So the value of 5
means it's June, not May.
这篇关于mktime返回错误的时间戳(关闭一整个月)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文