convert time_t to char*

This is a discussion on convert time_t to char* within the C++ Programming forums, part of the General Programming Boards category; hey, i want to convert a time_t = time(NULL); timestamp to a char*. What would be the best way to ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Location
    Hamburg, Germany
    Posts
    13

    convert time_t to char*

    hey,

    i want to convert a time_t = time(NULL); timestamp to a char*.

    What would be the best way to do that?

    Thank you
    Lennart

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Simplest form is ctime (and friends). More flexible, but also more complex to use, is strftime().

    http://www.hmug.org/man/3/ctime.php

    http://www.hmug.org/man/3/strftime.php

    --
    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.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Location
    Hamburg, Germany
    Posts
    13
    thank you.

    but i just want to get the timestamp as a string - not a formatted, human readable time

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    How do you mean? It's either in binary form, or it's in a character array - the latter will be more or less human readable.

    Of course, if you really just want it as something that is ASCII, but not human readable, something along these lines would do it:
    Code:
    char *time_t_2_ascii(const time_t time)
    {
        size_t i;
        char *str = malloc(sizeof(time_t) * 2 + 1);
        unsigned char *ptr = (unsigned char *)&time;
        for(i = 0; i < sizeof(time_t); i++) {
           sprintf(&str[i*2], "%02x", ptr);
           ptr++;
        }
        return str;
    }


    --
    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.

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    Quote Originally Posted by crisis View Post
    thank you.

    but i just want to get the timestamp as a string - not a formatted, human readable time
    You mean convert the NUMBER to a string representing that number? Just run it through stringstream:

    Code:
    std::string time_t_to_string(time_t t)
    {
        std::stringstream sstr;
        sstr << t;
        return sstr.str();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  2. Convert to title case
    By gertie in forum C Programming
    Replies: 18
    Last Post: 07-06-2008, 11:58 PM
  3. Replies: 3
    Last Post: 08-21-2006, 07:42 AM
  4. Convert Char to Int Function
    By drdroid in forum C++ Programming
    Replies: 9
    Last Post: 02-19-2003, 12:53 PM
  5. please help ... to convert date to string
    By mel in forum C Programming
    Replies: 1
    Last Post: 06-12-2002, 11:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21