# Definitions of date arithmetic

D. J. Bernstein djb at cr.yp.to
Sat Oct 10 21:28:50 UTC 1998

```Ken Pizzini writes:
> I just don't think that the other dates you ask about are problemmatical.

You misunderstand.

Some people seem to believe that 1 month before 31 May is ``clearly''
not 1 May; it is ``obviously'' 30 April.

Does this mean that 1 month before 30 May is 29 April? And 1 month
before 29 May is 28 April? And 1 month before 25 May is 24 April?

On the other hand, surely 1 month before 1 May is 1 April, and 1 month
before 2 May is 2 April. So 1 month before 25 May is 25 April?

> If "guess_flag" is zero, then add_tm() shall fail if the request is
> not well defined ("one month after March 31").

What exactly do you mean by ``well defined''?

What should this function do with 31 March plus -1 months and -3 days?
For which values of M and D is ``31 March plus M months and D days''
defined, and what is the result in those cases?

> If guess_flag is
> non-zero, then add_tm() shall force some plausible interpretation
> (*) on the result (e.g., adjusting a tm_mon where tm_mday is in
> {29,30,31} and the target month won't hold that value results in
> tm_mday being forced to the maximum value for the target month).

How does that adjustment interact with the added days? What is 31 March
plus 1 month plus 1 day? What is 31 March plus 1 month plus -1 day?

> Using C89 the best one could do is to
> use mktime() to do the normalization, but the mktime() interface
> looses information which can be helpful to the implementation
> in trying to disambibuate what the user was trying to request.

I still haven't seen a complete, coherent explanation of what _you_ are
trying to request. At least the mktime() behavior is comprehensible.

> My main concern is: this is an often-desired function,

_When_ is it desired? Point out some programs that are currently using
mktime() and obtaining unsatisfactory results.

> I suppose that what would be even better is if there were some
> reasonable way to directly expose the encoding of the calendar

libtai provides caldate_mjd() and caldate_frommjd().

---Dan
1000 recipients, 28.8 modem, 10 seconds. http://pobox.com/~djb/qmail/mini.html

```