[tz] localtime.c issue

Alan Barrett apb at cequrux.com
Mon Jul 8 21:49:53 UTC 2013


On Mon, 08 Jul 2013, Paul_Koning at Dell.com wrote:
>> Perhaps this will work without either a compiler warning or potential loss of precision:
>>
>>           seconds = tdays * SECSPERDAY + (time_t)0.5;
>>
>> --apb (Alan Barrett)
>
> I rather doubt it.  Casting 0.5 to an integer type (as time_t 
> is) ends up adding 0 or 1, but not 0.5.

The whole point of adding 0.5 before converting from time_t to 
int_fast32_t is to round up if time_t happens to be a floating 
point type, and to do nothing if time_t happens to be an integral 
type.  You can't assume that time_t is an integral type.

>How about (tdays * SECSPERDAY * 2 + 1) / 2;   ?

Multiplying by 2 might overflow the available range.  (I have not 
checked whether there are reasons why it would always be safe.) 

--apb (Alan Barrett)


More information about the tz mailing list