2

我正在尝试获取自纪元以来经过的秒数。编码:

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_secint是 1628993312timesinceEpoch而是 -1。tm_sec也在long数据类型的范围内timesinceEpoch。欢迎任何关于为什么以及如何解决这个问题的想法。

4

2 回答 2

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
于 2013-04-04T05:58:32.723 回答
1

约翰是对的。 strptime不初始化tm,仅触摸明确指定的字段(参见strptime(3) 的“注释” ),因此t.tm_sec不初始化。

为了防止这种错误,您可以声明带有初始化的变量

struct tm t = { 0 }; // zero filled
于 2013-04-04T08:38:53.613 回答