c++ - 处理夏令时 - C++

标签 c++ time timezone dst ctime

给定输入日期和时间(字符串格式),我尝试使用 ctime 中给出的 time 函数(例如 mktime)获取它的纪元时间。将 time_t 纪元时间转换回日期和时间会导致日期和时间比原始时间少一小时。我经过一些讨论,说如果是夏令时,可能会调整一小时。这是代码:

//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-03-11 时间:04:41:53

我的问题:
1. 检查 tm_idst 标志返回非零值,表明 DST 生效。但是,我如何知道正在谈论哪个时区?
2. 上面给出的时间戳可能与我所在的时区不同。有没有办法指定时区以便正确设置 tm_idst 标志?
3. 当我不确定时间戳记录在哪个时区时,如何处理夏令时?

最佳答案

Plain C++ 的时区数据非常稀疏,并且在要格式化的时间中没有时区规范,在某些时期您会得到不一致的结果 - 例如时钟倒转后的重复时间。这就是为什么总是建议以 UTC 记录所有时间戳 - 即从不将时区应用于记录的时间戳,以 GMT 记录,然后来回将该值作为显示变量,您可以完全控制它。

Linux/BSD 有一些附加字段可用于确定时区以及与 UTC 的偏移量 - 例如在 Linux 上,它是 __tm_gmtoff 字段,在 BSD(/Mac OS X) 中,它称为 tm_gmtoff

还有一个标记时区的附加字段,在 Linux 上为 __tm_zone,在 BSD(/Mac OS X) 上为 tm_zone,但仅当您获取本地时间。

我稍微改变了你的例子,并得到了以下输出:

Time entered = 2013-04-05 15:00
I am here.
Error. Check string for formatting of new date.
Details of the time structure:
Years since 1900 = 113
Months since January = 3
Day of the month = 5
Hour = 15 Minute = 0 second = 0
gmtoff = 0
Time since epoch = 1365174000
Reconverting to the time structure:
Details of the time structure:
Years since 1900 = 113
Months since January = 3
Day of the month = 5
Hour = 16 Minute = 0 second = 0
gmtoff = 3600
Zone = IST
Time in date + time = 2013-04-05 15:00 and since epoch = 1365174000

如果您在 Windows 中使用此结构,则必须使用另一种机制,因为它没有这两个额外的字段。

关于c++ - 处理夏令时 - C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15667253/

相关文章:

模板之外的 Django 时区支持

c++ - OpenCV:使用同一个 Mat 对象作为源和目标安全吗?

C++ 如何处理函数中指针和引用的变化?

c++ - 异常规范在声明和功能实现上不兼容

c# - 从 C++ 类检索时,.NET 中的时间减少了 1

javascript - 如何在不创建新的时刻对象的情况下获得所需时区的小时和分钟?

c++ - 在 For 循环中使用 {} 且不使用 {} 的 If 语句

Python 每 5 分钟执行一次操作

c++ - 计算下个月开始日期的算法

android 时区差异比预期少 1 小时