
Mailing List Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [tlug] It's time_t to Party!
Michal Hajek wrote:
> printf("... %d.%09ld\n",> (int)ts.tv_sec, (long int)ts.tv_nsec);
I'm focusing on the type of the .tv_sec element.
On a box I use, it is a type_t type.
Unfortunately, I don't know how to say:
printf("%(type_t)d...",ts.tv_sec);
which would be automatically be correct when type_t changes,
or even better:
printf("%(typeof ts.tv_sec)d...",ts.tv_sec);
which would spare one from having to scrutinize the man pages
(although one would still have to know that .tv_sec is an
integral type), so one is stuck commiting to making a choice to
use l or to not use l between % and d in the format string.
On a box I use:
.tv_sec is a type_t, which is a __type_t, which is a __TIME_T_TYPE,
which is a __SLONGWORD_TYPE, which is a long int.
Yuch! It was too hard to follow that chain of definitions
with repeated grep -i foo /usr/include commands. There is
such a hall of mirrors effect that one is not confident what
the heck tv_sec is.
I would figure the safer choice, would be to cast to a long
(and use %ld) instead of a mere int. At worst, the cast to
long slows down printf. At worst, the cast to int chops off
significant bits. Granted, with 32-bit ints, that won't happen
for about another 30 years,
http://en.wikipedia.org/wiki/Unix_epoch#32-bit_overflow
http://en.wikipedia.org/wiki/Year_2038_problem
http://en.wikipedia.org/wiki/Unununium_Time#System_time
and even then computers would likely have migrated to 64-bit
ints time_t's which would make the issue moot, but that's what
folks were saying about four-digit numbers for years in the (19)60s.
(And what format is the data stored in?) It's like deja-vu all
over again with Y2K. Some organizations already have to deal with
this kind of issue, and just use their own format, like
YYYY-MM-DD HH:MM:SS.sssssssssss...
If it's for production code, I would first consider something
that would avoid the need for any cast, then consider casting
to a long. Casting to an int would be my last consideration.
Of course, if your code is just a short term testing hack,
then it doesn't matter.
Of course, a completely different consideration would be to
avoid using printf(), or the loosely typed C language.
Some things are just best done in Emacs.
http://en.wikipedia.org/wiki/Unix_epoch#time_t_parties
Jim
Home |
Main Index |
Thread Index