c++ - 使用 strptime/strftime 的结果不一致

标签 c++ datetime ubuntu-14.04

我编写了一个表示日期的 C++ 类,我使用 strptime/strftime 从字符串编写和实例化日期。

The full code is here

当我在我的 Linux 上使用“示例输出”选项卡中的 bash 多次运行它时,有时我会创建相同的日期并解析回来,有时我会得到一个小时偏移的日期(我的时区是 UTC +1).

所以,这里发生了什么,我不知道!

#ifndef DOLIPRANE_TIMEUNIT_HPP
#define DOLIPRANE_TIMEUNIT_HPP

enum TimeUnit  {
    DAY,
    HOUR,
    MINUTE,
    SECOND
  };
#endif
#ifndef DOLIPRANE_DATE_HPP
#define DOLIPRANE_DATE_HPP

#include <ctime>
#include <string>


class Date
{
public:

  Date();
  Date(time_t epoch);

  /**
   * Expected format: dd/MM/YYYY HH:mm:[ss]
   */
  Date(const std::string &date);

  ~Date();

  void
  add(long val, TimeUnit u = SECOND);


  bool
  operator==(const Date &other) const;

  bool
  operator!=(const Date &other) const;

  bool
  operator<(const Date &other) const;

  bool
  operator<=(const Date &other) const;

  bool
  operator>(const Date &other) const;

  bool
  operator>=(const Date &other) const;

  friend std::ostream&
  operator<<(std::ostream &, const Date&);

  friend std::istream&
  operator>>(std::istream &, Date&);

private:

  static const std::string FORMAT;

  time_t m_time;


};
#endif


#include <iostream>
#include <stdexcept>
#include <ctime>

const char SEPARATOR=';';

const std::string Date::FORMAT="%d/%m/%Y %H:%M:%S";

Date::Date()
{
  m_time = time(NULL);
}

Date::Date(time_t epoch) 
  : m_time(epoch)
{}

Date::Date(const std::string &date)
{
  struct tm t;
  const char* ptr = strptime(date.c_str(), FORMAT.c_str(), &t);
  if (!ptr) {
    std::string cause = "Cannot parse date ";
    cause += date;
    throw std::invalid_argument(cause);
  }
  m_time = mktime(&t);
  if (m_time == -1) {
    std::string cause = "Cannot compute epoch from " + date;
    throw std::range_error(cause);
  }
}

Date::~Date()
{
}

void
Date::add(long val, TimeUnit u) {

  switch(u){
  case DAY:
    m_time += 86400*val;
    break;
  case HOUR:
    m_time += 3600*val;
    break;
  case MINUTE:
    m_time += 60*val;
    break;
  case SECOND:
    m_time += val;
    break;
  default:
    throw std::invalid_argument("Unknown TimeUnit specified");
  }
}

bool
Date::operator==(const Date& o) const
{
  return m_time == o.m_time;
}

bool
Date::operator!=(const Date& o) const
{
  return ! (*this==o);
}

bool
Date::operator<(const Date &other) const
{
  return m_time < other.m_time;
}

bool
Date::operator<=(const Date &other) const
{
  return m_time <= other.m_time;
}

bool
Date::operator>(const Date &other) const
{
  return m_time > other.m_time;
}

bool
Date::operator>=(const Date &other) const
{
  return m_time >= other.m_time;
}

std::ostream&
operator<<(std::ostream& out, const Date &d)
{
  struct tm* tm = localtime(&d.m_time);
  char buffer[20];
  strftime(buffer, 20, Date::FORMAT.c_str(), tm);
  out << buffer << SEPARATOR;
  return out;
}

std::istream&
operator>>(std::istream &in, Date &d)
{
  std::string buf;
  std::getline(in, buf, SEPARATOR);
  Date o(buf);
  d = o;
  return in;
}

#include <iostream>
#include <fstream>

int
main(void)
{
  Date d;
  std::cout << d << std::endl;
  std::ofstream out("tmp.txt");
  out << d;
  out.close();

  std::ifstream in("tmp.txt");
  Date d2;
  in >> d2;
  in.close();
  std::cout << d2 << std::endl;

}

最后,我是如何测试它的:

$ for i in `seq 1 10`; do echo "test $i:"; ./test; rm tmp.txt; done
test 1:
26/03/2016 00:30:31;
26/03/2016 00:30:31;
test 2:
26/03/2016 00:30:31;
26/03/2016 00:30:31;
test 3:
26/03/2016 00:30:31;
25/03/2016 23:30:31;
test 4:
26/03/2016 00:30:31;
26/03/2016 00:30:31;
test 5:
26/03/2016 00:30:31;
25/03/2016 23:30:31;
test 6:
26/03/2016 00:30:31;
26/03/2016 00:30:31;
test 7:
26/03/2016 00:30:31;
25/03/2016 23:30:31;
test 8:
26/03/2016 00:30:31;
25/03/2016 23:30:31;
test 9:
26/03/2016 00:30:31;
25/03/2016 23:30:31;
test 10:
26/03/2016 00:30:31;
26/03/2016 00:30:31;

最佳答案

问题是
struct tm t;
在采用字符串的 Date 构造函数中, 它从不初始化对象,因此它的 tm_isdst 字段具有未指定的值。正确设置 tm_isdst 字段,您将获得一致 多次运行的结果。

关于c++ - 使用 strptime/strftime 的结果不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36229908/

相关文章:

go - 从golang中的xls文件中读取数字类型单元格

c++ - 此程序中的段错误

c++ - 如何在某个类中定义的其他类中使用用户定义的类型?

c++ - Qt 忽略 const 说明符

ruby-on-rails - 如何在 Rails 中将随机 DateTime 四舍五入到小时开始?

linux - linux安装软件时如何解决循环依赖?

ruby-on-rails - 尝试加载 gem 'yard' 时出错

c++ - 编码竞赛的 C 编码评估器

ios - 使用 Moment Tz 格式化日期

python - 将看起来像日期的字符串转换为日期