时代以来的时间是-1

时间:2013-04-04 05:53:32

标签: c++ epoch ctime

我试图获得自纪元以来已经过的秒数。代码:

long parseTime(string time) {

  cout << "Time entered = " << time << endl;

  long timeSinceEpoch;

  //takes in time in string format - date + time and returns seconds from epoch.
  /* Steps:
   * 1. Remove trailing and leading spaces.
   * 2. Check format of date.
   * 3. Convert to epoch.
   */

  //remove trailing and leading spaces.
  /*
  unsigned int leading = 0, trailing = 0;
  string whitespace = " \t";

  leading = time.find_first_not_of(whitespace);
  trailing = time.find_last_not_of(whitespace);

  string newTime = time.substr(leading, (trailing - leading + 1));
  cout << "Old time = " << time << " new time = " << newTime << endl;
  */
  string newTime = time;

  struct tm t;

  if(newTime.find("/") != string::npos) {
    //format of date is mm/dd/yyyy. followed by clock in hh:mm (24 hour clock).
    cout << "Time format contains slashes." << endl;
    if(strptime(newTime.c_str(), "%m/%e/%Y %H:%M", &t) == NULL) {
      cout << "Error. Check string for formatting." << endl;
    }
  } else if(newTime.find("-") != string::npos) {
    //format of date is yyyy-mm-dd hh:mm:ss (hh in 24 hour clock format).
    if(strptime(newTime.c_str(), "%Y-%m-%e %H:%M:%S", &t) == NULL) {
     cout << "Error. Check string for formatting of new date." << endl;
    }
  }

  if(t.tm_isdst) {
    t.tm_isdst = 0;
  }

  timeSinceEpoch = mktime(&t);
  cout << "Time since epoch = " << timeSinceEpoch << endl;

  return timeSinceEpoch;
}

现在,当一个包含日期和时间的字符串传递给函数时:
3/26/2013 3:17
它会导致epoch = -1以来的时间。以下是调试器的输出:

Breakpoint 2, parseTime (time=...) at informationExtractor.cpp:44
44        cout << "Time entered = " << time << endl;
(gdb) n
Time entered = 3/26/2013 3:17
66        string newTime = time;
(gdb)
70        if(newTime.find("/") != string::npos) {
(gdb) p newTime
$3 = {static npos = <optimized out>,
  _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>},
    _M_p = 0x8004ab0c "3/26/2013 3:17"}}
(gdb) n
72          cout << "Time format contains slashes." << endl;
(gdb)
Time format contains slashes.
73          if(strptime(newTime.c_str(), "%m/%e/%Y %H:%M", &t) == NULL) {
(gdb)
83        if(t.tm_isdst) {
(gdb) p newTime.c_str()@strlen(newTime.c_str())
Only values in memory can be extended with '@'.
(gdb) n
84          t.tm_isdst = 0;
(gdb) p newTime
$4 = {static npos = <optimized out>,
  _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>},
    _M_p = 0x8004ab0c "3/26/2013 3:17"}}
(gdb) n
87        timeSinceEpoch = mktime(&t);
(gdb) p t
$5 = {tm_sec = 1628993312, tm_min = 17, tm_hour = 3, tm_mday = 26, tm_mon = 2, tm_year = 113, tm_wday = 2, tm_yday = 84,
  tm_isdst = 0}
(gdb) n
88        cout << "Time since epoch = " << timeSinceEpoch << endl;
(gdb)
Time since epoch = -1
90        return timeSinceEpoch;
(gdb)  

如果您注意到,tm_sec中的t为1628993312,而timesinceEpoch为-1。 tm_sec也在long范围内,timesinceEpoch是{{1}}的数据类型。关于为什么以及如何解决这个问题的任何想法都是受欢迎的。

2 个答案:

答案 0 :(得分:3)

关键是在你的代码中,tm_sec是一个未初始化的值。它应该是0到59之间的值。所以将这行代码添加到第一个if分支。

if(strptime(newTime.c_str(), "%m/%e/%Y %H:%M", &t) == NULL) {
  cout << "Error. Check string for formatting." << endl;
}
t.tm_sec = 0; // initialise seconds field

答案 1 :(得分:1)

约翰是对的。 strptime未初始化tm,只触及明确指定的字段(see "Notes" of strptime(3)), 所以t.tm_sec未初始化。

为防止出现此类错误,您可以使用初始化声明变量

struct tm t = { 0 }; // zero filled