2013-04-04 78 views
2

我正在嘗試獲取自時代以來已經過去的秒數。的代碼:自世紀以來的時間爲-1

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
它導致的時間,因爲曆元= -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_sect同時1628993312是timesinceEpoch -1。 tm_sec也在long的範圍內,它是timesinceEpoch的數據類型。歡迎任何關於爲什麼以及如何解決此問題的想法。

回答

3

問題是,在你的代碼中tm_sec是一個未初始化的值。它應該是0到59之間的一個值。因此,將這行代碼添加到您的第一個分支。

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 
+0

varialbes'tm_sec'設置爲0,已使用'strptime'初始化之後?這將如何工作?此外,調試器輸出顯示'tm_sec'(自epoch以來的時間)被初始化爲1628993312,然後在調用mktime之後被設置爲-1。 – Sriram 2013-04-04 06:34:21

+1

我的觀點是它沒有被strptime初始化,在你的格式字符串中沒有%S。 – john 2013-04-04 06:36:20

+0

即時通訊不知道我已經明白這一點。 '%S'爲秒,輸入到函數的時間字符串沒有秒,只有小時:分鐘。 – Sriram 2013-04-04 07:18:59

1

約翰是對的。 strptime不會初始化tm並且只觸摸明確指定的字段(see "Notes" of strptime(3)), 因此t.tm_sec未被初始化。

爲了避免這種錯誤,你可以聲明與初始化

struct tm t = { 0 }; // zero filled 
相關問題