2
功能strptime()
的莫名其妙的行爲:爲什麼strptime c函數會改變結構?
#define _XOPEN_SOURCE
#include <stdio.h>
#include <time.h>
double getPeriod(char * dateStart, char * dateStop) {
struct tm tmStart, tmStop;
time_t timeStampStart, timeStampStop;
strptime(dateStart, "%Y-%m-%d %H:%M:%S", &tmStart);
strptime(dateStop, "%Y-%m-%d %H:%M:%S", &tmStop);
timeStampStart = mktime(&tmStart);
timeStampStop = mktime(&tmStop);
printf("%d\t%d\n", tmStart.tm_hour, tmStop.tm_hour);
}
int main()
{
getPeriod("2016-12-05 18:14:35", "2016-12-05 18:18:34");
return 0;
}
輸出:
17 18
爲什麼會出現這種情況?
編譯器GCC(海灣合作委員會)6.2.1 OS的Linux
您需要將timeStampStart和timeStampStop的'tm_isdst'成員初始化爲-1。 – user4815162342
[似乎對GCC和工作鏘這裏(http://coliru.stacked-crooked.com/a/6b54f234cc33eb6c) – AndyG
@ user4815162342:哦,是夏令時的事情總是這樣的痛苦!我覺得你的意思了'tmStart'和'tmStop' – AndyG