给定一个输入日期和时间(以字符串格式),我试图用给出的time
函数获得它的历元时间,如mktime
。将time_t
纪元时间转换回日期和时间会导致日期和时间比原始时间少一小时。我经历了一些讨论,说夏时制可能会有一小时的调整。下面是代码:应对夏令时 - C++
//sample strptime program.
#include <iostream>
#include <ctime>
#include <string>
using namespace std;
long parseTime(string time) {
cout << "Time entered = " << time << endl;
long timeSinceEpoch;
struct tm t;
if(time.find("/") != string::npos) {
//format of date is mm/dd/yyyy. followed by clock in hh:mm (24 hour clock).
if(strptime(time.c_str(), "%m/%e/%Y %H:%M", &t) == NULL) {
cout << "Error. Check string for formatting." << endl;
}
} else if(time.find("-") != string::npos) {
//format of date is yyyy-mm-dd hh:mm:ss (hh in 24 hour clock format).
cout << "I am here." << endl;
if(strptime(time.c_str(), "%Y-%m-%e %H:%M:%S", &t) == NULL) {
cout << "Error. Check string for formatting of new date." << endl;
}
}
cout << "Details of the time structure:" << endl;
cout << "Years since 1900 = " << t.tm_year << endl;
cout << "Months since January = " << t.tm_mon << endl;
cout << "Day of the month = " << t.tm_mday << endl;
cout << "Hour = " << t.tm_hour << " Minute = " << t.tm_min << " second = " << t.tm_sec << endl;
timeSinceEpoch = mktime(&t);
time_t temp = mktime(&t);
cout << "Time since epoch = " << timeSinceEpoch << endl;
cout << "Reconverting to the time structure:" << endl;
struct tm* t2 = localtime(&temp);
cout << "Details of the time structure:" << endl;
cout << "Years since 1900 = " << t2->tm_year << endl;
cout << "Months since January = " << t2->tm_mon << endl;
cout << "Day of the month = " << t2->tm_mday << endl;
cout << "Hour = " << t2->tm_hour << " Minute = " << t2->tm_min << " second = " << t2->tm_sec << endl;
return timeSinceEpoch;
}
int main(int argc, char *argv[]) {
string date, t;
cout << "Enter date: " << endl;
cin >> date;
cout << "Enter time: " << endl;
cin >> t;
struct tm time;
string overall = date + " " + t;
long result = parseTime(overall);
cout << "Time in date + time = " << overall << " and since epoch = " << result << endl;
return 0;
}
的麻烦的输入是: 日期:2013年3月11日 时间:4时41分53秒
我的问题(S):
1.检查tm_idst
标志返回一个非零值,表示DST生效。但是,我怎么知道正在讨论什么时区?
2.上述时间戳可能没有被记录在同一时区,我在,有没有这样的tm_idst
标志设置是否正确,指定时区的方法吗?
3.当我不确定时间戳记录在哪个时区时,我该如何处理DST?
获取时区取决于您的操作系统和C语言库上,没有标准或平台无关的方式得到它。例如,在使用glibc的Linux上,'tm'结构中有一个名为'tm_zone'的额外成员字段,它是一个包含时区缩写的C风格字符串。 – 2013-03-27 19:09:36
@Joachim:我试过'tm_zone'选项,但它似乎不在那里。所以基本上,我只是从时代开始就有时间,重新转换为'struct tm',并且如果有一个小时的差异,则增加3600秒?这是我处理DST的唯一选择吗? – Sriram 2013-03-27 19:22:26
评论 - 如果你接受'ISO'类型的日期,那么正确的输入格式是YYYY-MM-DD(即4 + 2 + 2 **总是***) - 所以解析为'% Y-%m-%d',%e格式在理论上接受单位数字日期的前导空间,而[ISO总是期望两位数的月份和年份](http://en.wikipedia.org/wiki/ ISO_8601)。 – Petesh 2013-03-27 19:47:19