portable way to deal with 64/32 bit time_t
Solution 1
According to the C standard, time_t
is an arithmetic type, "capable of representing times". So, it could be double
for example. (Posix mentions this more explicitly, and also guarantees that time()
returns the number of seconds since the Epoch—the latter is not guaranteed by the C standard.)
Maybe the cleanest solution is to convert the value to whatever type you want. You may want one of unsigned long long
or unsigned long
:
printf("%llu\n", (unsigned long long)t);
Solution 2
I think the only truly portable way is to use strftime
to convert the time_t
to a string.
If you're sure that you're only operating on platforms where time_t
is an int
, you could cast to intmax_t
(from stdint.h
) and print it using PRIdMAX
(from inttypes.h
).
Solution 3
If you want to go with the macro specifier, I would recommend one minor tweak. Instead of encapsulating the entire specifier, encapsulate just the modifier:
#ifdef 64_BIT_TIME
#define TT_MOD "ll"
#else
#define TT_MOD ""
#endif
and then using it like this:
printf("current time in seconds is: %" TT_MOD "u", time(0));
The reason why is that while you primarily want the second in decimal, every so often you may want hex (or perhaps you want leading 0's). By only having the modifier there, you can easily write:
"%" TT_MOD "x" // in hex
"%08" TT_MOD "d" // left pad with 0's so the number is at least 8 digits
![MK.](https://i.stack.imgur.com/1bC0L.png?s=256&g=1)
MK.
Updated on January 09, 2020Comments
-
MK. over 4 years
I have some code which is built both on Windows and Linux. Linux at this point is always 32bit but Windows is 32 and 64bit. Windows wants to have time_t be 64 bit and Linux still has it as 32 bit. I'm fine with that, except in some places time_t values are converted to strings. So when time_T is 32 bit it should be done with %d and when it is 64bit with %lld... what is the smart way to do this? Also: any ideas how I may find all places where time_t's are passed to printf-style functions to address this issue?
edit: I came up with declaring TT_FMT as "%d" or "%lld" and then changing my printfs as in printf("time: %d, register: blah") to be printf("time: " TT_FMT ", register: blah") Is there a better way? And how do I find them all?
-
caf over 14 yearsThis has always seemed like the cleanest method to me. It seems a pity that
printf
didn't include a format specifier fortime_t
values, even if it was just the%c
behaviour fromstrftime
. -
MK. over 14 yearsThanks, I think I like it, except time_t is signed; I'll probably use int64_t.
-
Jonathan Leffler over 11 yearsMinor nitpick; macro names can't start with digits. I would probably go for a name more like the
<inttypes.h>
names, without encroaching on the reserved namespace (ISO/IEC 9899:2011 §7.31.6 Macros that begin with either PRI or SCN, and either a lowercase letter or X may be added to the macros defined in the<inttypes.h>
header). -
John Hascall about 5 yearsNow, (9 years after this answer), you could use
%jd
in place ofPRIdMAX
as insnprintf(str, sizeof(str), "%jd", (intmax_t)i_am_a_time_t_var)
-
S.S. Anne over 4 yearsHow about
%jd
andintmax_t
? -
Alok Singhal over 4 years@JL2210 Yeah, that should work.. although I think
"%" PRIdMAX
might be more portable.