I have a string representing seconds since epoch, and I need to convert it to a human readable string. I've seen a few posts online that suggest simply casting an integer to time_t as so:
time_t time = (time_t)(atoi(secs_since_epoch_str));
But, if I look up the definition of time_t:
typedef /* unspecified */ time_t;
Although not defined by the C standard, this is almost always an integral
value holding the number of seconds (not counting leap seconds) since 00:00,
Jan 1 1970 UTC, corresponding to POSIX time.
So, this is not guaranteed to work. I'm wondering if there's a proper way of doing this?
Add the seconds offset to a
.tm_secmember of an epochstruct tmand form thetime_t.long. Error handling omitted for brevity.time_tis not certainly a count of seconds (e.g. maybe nanoseconds) and maybe the epoch is not Jan 1, 1970 - it is more interesting. Form a timestamp for the epoch.struct tmmembers. Perhaps segmented the offset addition into parts to avoidintoverflow. More code needed whenintis 16-bit andoffsetis large (e. g. 88+ years).mktime()to bring the timestamp into the primary range and print. Note than sincetime_tmight be some integer type or some floating point type, covert to a FP type to print.