Strftime's %C and %y formats versus wide-ranging tm_year valu es

Ken Pizzini tz. at explicate.org
Fri Sep 24 16:26:34 UTC 2004


On Thu, Sep 23, 2004 at 03:39:37PM -0400, Olson, Arthur David (NIH/NCI) wrote:
> ! 						i = year % 100;
> ! 						if (i < 0) {
> ! 							i = -i;
> ! 						}

The C language definition has typically left as "implementation defined"
whether the result of (-42)%100 is -42 or +58 (with corresponding variation
in what the results of integer division will be); this code is making the
assumption that the -42 answer will always be given.  Now I grant that
this is true on any system I can think of offhand, I still think it is
worth recoding this (in both the %y and %g fragments) as something more
like:
   i = (year < 0 ? -year : year) % 100;

And the %C code:
> ! 					top = year / 100;
would need a corresponding pedantic fix, such as:
   top = (year + (year < 0 ? -year % 100 : 0)) / 100;


		--Ken Pizzini



More information about the tz mailing list