#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    9
    Rep Power
    0

    Question Convert string to time_t


    Hi All!

    Let me try to explain my problem... Im reading rows from a database, using a time_t parameter to tell the initial time (only rows with time greater than this parameter will be returned). ok!

    I need to save (in a file) the last time_t parameter used in the request. Im saving this parameter like it is (a long int, for example 1083075041). But...

    In this format, its hard to users to change this information, so I want to save this time_t parameter in the format return by c_time function (a string).

    Is there any way, after doing this, to convert this string back to time_t? :confused:

    Since now, thanks a lot!!!! :)
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    Take the parts of the date, stick them in a tm struct, then pull it back out as a time_t. Time consuming, but to my knowledge there is no library out there (at least not standard) that will help you.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    9
    Rep Power
    0
    I was affraid of with :)

    Thanks!
  6. #4
  7. Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jan 2004
    Location
    near St. Louis Illinois
    Posts
    3,288
    Rep Power
    24
    it's really not all that difficult as long as you know the ascii format. Once you put everything in the struct tm, just call mktime(), which will return the time_t. Make sure you pre-initialize the struct tm to all binary zeros before doing anything with it because, if you don't, mktime() may not work correctly. there are some fields you don't have to worry about, such as julian day or day-or-week. Just complete year, month, day, hour, minute and second.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    9
    Rep Power
    0
    Thanks! Ill do this!!!

IMN logo majestic logo threadwatch logo seochat tools logo