当我运行以下代码时, strptime似乎忽略了时区值。只需设置本地时区的值(即 +10)。
这是输出,(在 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;
}
}