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
Printable View
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
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
thank you.
but i just want to get the timestamp as a string - not a formatted, human readable time
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