Thread: fastest way to change a time_t into a string

  1. #1
    Registered User whackaxe's Avatar
    Join Date
    Mar 2004
    Posts
    332

    fastest way to change a time_t into a string

    i need to change the string of numbers returned by time() (in a time_t type variable) into a string type varaible. what is the best way to do this?

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    ctime() or strftime() via localtime() or gmtime() if you need more control.

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    http://cppreference.com/stddate.html

    more detail on the above functions
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    strftime() is a better choice than ctime() or asctime() simply because ctime() and asctime() return a pointer to a local static buffer that will be overwritten during each subquent call. Of course if you are only dealing with one date then thats perfectly fine

  5. #5
    Registered User whackaxe's Avatar
    Join Date
    Mar 2004
    Posts
    332
    i don't think you understood what i meant. time returns time_t which contains the number of seconds since 1/1/1970 i need to convert that number into a string (just the same thing, but i need it as a string type). sorry for late reply

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    sprintf() is the easist way
    Code:
    sprintf(buffer, "%u", (unsigned) time(NULL));
    Where buffer of course is an array of characters.

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    127
    Quote Originally Posted by whackaxe
    i don't think you understood what i meant. time returns time_t which contains the number of seconds since 1/1/1970 i need to convert that number into a string (just the same thing, but i need it as a string type). sorry for late reply
    Such an operation is inherently nonportable. time_t is an arithmetic type, but the only operation defined for it is a comparison to (time_t)-1. So any integer-string conversions would be relying on a specific representation of time_t. However, because most implementations use unsigned long or similar, sprintf should work.
    Code:
    time_t present = time(0);
    sprintf(buffer, "%lu", (unsigned long)present);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How properly inherit from template?
    By 6tr6tr in forum C++ Programming
    Replies: 118
    Last Post: 04-25-2008, 04:30 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM