Thread: time_t troubles

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    69

    time_t troubles

    I have a string with the following contents: "1950-01-01 00:00:00".

    I have the following code. It should create a time_t object from the string. But it crashes and I don't understand why..
    Code:
    tm dbTime;
    dbTime.tm_year = atoi((s.substr(0, 4)).c_str()) - 1900;
    dbTime.tm_mon = atoi((s.substr(5, 2)).c_str());
    dbTime.tm_mday = atoi((s.substr(8, 2)).c_str());
    dbTime.tm_hour = atoi((s.substr(11,2)).c_str());
    dbTime.tm_min = atoi((s.substr(14, 2)).c_str());
    dbTime.tm_sec = atoi((s.substr(7, 2)).c_str());
    dbTime.tm_isdst = false;
    
    // Seems to be correct:
    cout << "datetime values read: " << endl;
    cout << dbTime.tm_year << endl;
    cout << dbTime.tm_mon << endl;
    cout << dbTime.tm_mday << endl;
    cout << dbTime.tm_hour << endl;
    cout << dbTime.tm_min << endl;
    cout << dbTime.tm_sec << endl;
    cout << dbTime.tm_isdst << endl;
    
    
    time_t t = mktime(&dbTime);  // returns  -1, error code
    cout << ctime(&t) << endl;     // crashes
    Why does mktime return -1? Is something wrong with the tm instance?

    I wasn't sure where to post this. If it should be in the C subforum, please move it there.

    Thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps because in most implementations, time_t starts from 1-Jan-1970. That's not guaranteed, but it's certainly quite common.

    ctime shouldn't crash - that would probably be a consequence of the bad value tho'.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    69
    Thanks, matsp!

    I knew about the 1 jan 1970 thing, but somehow it didn't occur to me when debugging this problem.. Thanks for pointing that out.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by C+/-
    I have a string with the following contents: "1950-01-01 00:00:00".
    Code:
    dbTime.tm_sec = atoi((s.substr(7, 2)).c_str());
    Typo? s.substr(7,2) is "-0". Is this actually s.substr(17,2) instead?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-15-2006, 05:14 AM
  2. Having troubles
    By dison in forum C++ Programming
    Replies: 1
    Last Post: 04-07-2005, 12:58 AM
  3. More strtod troubles
    By Bajanine in forum Windows Programming
    Replies: 2
    Last Post: 03-09-2003, 01:23 PM
  4. More Program Troubles...
    By face_master in forum Windows Programming
    Replies: 2
    Last Post: 08-01-2002, 05:40 AM
  5. having troubles
    By neandrake in forum C++ Programming
    Replies: 7
    Last Post: 03-07-2002, 09:31 PM