Calendar math problems

Martin von Loewis loewis at informatik.hu-berlin.de
Sun Jan 6 11:40:19 EST 2002


Ben Logan <ben at wblogan.net> writes:

> I need to be able to add/subtract an arbitrary number of days to/from
> an arbitrary date.  The days and dates are all within the range
> 1970-2038, so I thought I would convert everything to Unix time
> stamps, do the addition/subtraction on integers, and convert the
> result back to a date tuple.
> 
> Is this the best way to accomplish the task?  

Yes, that is very common. It would be best if you can keep the time
stamps in seconds-since-the-epoch externally also; printing them in
Hex is common. If you need pretty-printed time stamps, time.strptime
is your friend.

> How would one go about it if the dates fall outside the timestamp
> range?

Use negative numbers for dates before 1970; use large integers for
dates after 2038.

> I looked at the calendar module functions, and they would be helpful
> for writing an algorithm to do the dirty work, but I don't see any
> really easy solution.

time.localtime()/time.gmtime() deals with negative numbers nicely on
many systems. If that isn't good enough, you probably will need to
write your own algorithm - or search the web for an existing one.

Regards,
Martin



More information about the Python-list mailing list