[Zope-Coders] DateTime with negative dates (Collector #484)

Tim Peters tim@zope.com
Fri, 17 Jan 2003 15:05:50 -0500


[Lennart Regebro]
> If you create a DateTime object that is before 1970/01/01 00:00
> on windows, you get an error message:
>
>   File "DateTime.py", line 405, in safelocaltime
>     rval = localtime(t_int)
> IOError: [Errno 22] Invalid argument
>
> This is because windows localtime() can't handle dates before 1970/01/01.

The "Invalid argument" part is due to the platform C localtime() returning a
NULL pointer.  The "IOError" part of it is Python's fault, and Python 2.3
will raise ValueError in this case instead (thanks to you pointing it out!).

> There is a couple of ways of handling this:
>
> 1. Either make some special magick windows code to handle
> negative dates (I don't even know if it's possible).

This won't happen in Python.  Python's time.localtime() is meant to be a
thin wrapper around the platform C function of the same name, and when
platform localtime() can't handle negative ticks, chances are that *all*
platform tick functions also can't.  Unfortunately, the range of dates
supported isn't defined by the C standard (not even a minimal range).  POSIX
specifies that "the epoch" starts at 1970, though, and explicitly warns that
dates before 1970 (or after, approximately, 2038) may not "work".

> 2. Refuse to use negative dates.

It's really a negative tick count here.

> 3. Refuse to use negative dates on Windows, but allow them on unix.

I doubt that all flavors of Unix act alike here (see POSIX notes above).

> 4. ????
>
> Input appriciated.

5. At least in Zope3, you can use the datetime.py module, a Python
implementation of Python 2.3's C module of the same name.  That supports
years in 1 through 9999 inclusive.