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

Paul Eggert eggert at CS.UCLA.EDU
Tue Oct 5 20:30:22 UTC 2004


"Olson, Arthur David (NIH/NCI)" <olsona at dc37a.nci.nih.gov> writes:

> + #define C99IPMOD(a, b)	((-1 % 2 < 0 || (a) >= 0) ? \
> + 				((a) % (b)) : ((a) % (b) - (b)))

A quick reaction: this returns the wrong answer if -1%2 is 1 and a%b
is zero.  But see below.

> + #define isleap_sum(a, b)	isleap(C99IPMOD((a), 400) + C99IPMOD((b), 400))

Surely this is overkill.  You can simply use this:

  #define isleap_sum(a, b)  isleap((a) % 400 + (b) % 400)

as this will work regardless of whether % means Fortran remainder or
modulus.

> ! 	year = (double) timeptr->tm_year + (double) TM_YEAR_BASE;

This sort of trick won't work in general on hosts that have 64-bit int
and 64-bit double (e.g., some Crays), due to rounding problems.

> + 	i = a + b;
> + 	if ((i > a) == (b > 0))

This kind of overflow-checking won't work reliably in general, since
the behavior is undefined if signed integer overflow occurs.  I have
run into compilers where the above code won't detect overflow
correctly.

I have a draft solution for the above problems but would like to think
about it for a day or so before posting.



More information about the tz mailing list