1
當我運行下面的代碼strptime似乎忽略了時區值。只需設置當地時區的值(即+10)。C++ strptime忽略解析時區
這是輸出,(在Linux上運行,用gcc 4.6.3編譯):
-----------2013-04-24T9:47:06+400 - %Y-%m-%dT%H:%M:%S%z
TM Break H:9 is DST:0 GMT Off:0
The epoch value: 1366760826
DateTime in String: 04/24/13 - 09:47AM +1000
-----------2013-04-24T11:47:06+800 - %Y-%m-%dT%H:%M:%S%z
TM Break H:11 is DST:0 GMT Off:36000
The epoch value: 1366768026
DateTime in String: 04/24/13 - 11:47AM +1000
-----------2013-04-24T9:47:06+0 - %Y-%m-%dT%H:%M:%S%z
TM Break H:9 is DST:0 GMT Off:36000
The epoch value: 1366760826
DateTime in String: 04/24/13 - 09:47AM +1000
-----------2013-04-24T9:47:06+4 - %Y-%m-%dT%H:%M:%S%z
TM Break H:9 is DST:0 GMT Off:36000
The epoch value: 1366760826
DateTime in String: 04/24/13 - 09:47AM +1000
這是代碼:
void date_Test(){
string dateStrings[] = {"2013-04-24T9:47:06+400"
, "2013-04-24T11:47:06+800"
, "2013-04-24T9:47:06+0"
, "2013-04-24T9:47:06+4"};
string formatStrings[] = {"%Y-%m-%dT%H:%M:%S%z"
, "%Y-%m-%dT%H:%M:%S%z"
, "%Y-%m-%dT%H:%M:%S%z"
, "%Y-%m-%dT%H:%M:%S%z"};
process_Timezone(dateStrings, formatStrings);
}
void process_Timezone(string dateStrings[], string formatStrings[]){
int num = 4;
for (int i = 0; i < num; i++) {
cout << endl << "-----------" << dateStrings[i] << " - " << formatStrings[i] << endl;
tm *dtm = new tm;
strptime(dateStrings[i].c_str(), formatStrings[i].c_str(), dtm);
cout << "TM Break \tH:" << dtm->tm_hour << " is DST:" << dtm->tm_isdst << " GMT Off:" << dtm->tm_gmtoff << endl;
time_t ep_dt = mktime(dtm);
cout << "The epoch value: \t" << ep_dt << endl;
char buffer[40];
strftime(buffer, 40,"%x - %I:%M%p %z", dtm);
cout << "DateTime in String: \t" << buffer << endl;
delete dtm;
}
}
我已經嘗試過像0400,04:00這樣的變體,他們都沒有工作,甚至找不到一個使用它的人的在線例子 – pt123
嘿在這方面有什麼好運,我也有同樣的問題..如何從字符串創建time_t時區 – N3Xg3N