Comment 11 for bug 1328600

Revision history for this message
Barry Warsaw (barry) wrote :

In the example:

python3 -c "import datetime; datetime.timedelta(2983579200)"

realize that the first argument is days, not seconds, so you're trying to create a date more than 8 million years in the future. In the abstract, I hope Ubuntu - and the human race - is around that long :). Try this:

python3 -c "import datetime; datetime.timedelta(seconds=2983579200)"

That should work even on 32 bit machines (it works in my i386 utopic chroot).

The documentation does describe that datetime.{utc,}fromtimestamp() is limited by the argument size to libc's gtime() so you'd have the same problems with a C program. How would you solve 64 bit timestamps on 32 bit platforms in C or C++? I'm not sure there is a solution for 64 bit datetimes on 32 bit platforms. E.g. numpy has a datetime64, but substituting the above I get a NaT (i.e. not-a-time). Maybe I'm using it wrong though. There's also egenix-datetime, but that's Python 2 only in the archive (and maybe upstream). It's probably worth investigating available options on PyPI.

On the second point, remember that Python datetimes have two "modes", a naive mode where timezones are ignored but local time is usually assumed, and timezone aware datetimes. IMHO, it's *always* a good idea to do your internal time calculations in UTC time, and create the appropriate timezone objects for conversion/display to local time at the edges. I think of it the same way as Unicode: convert from whatever encoding your bytes are in to Unicode at the edges, manipulate internally as unicode, and then encode to bytes on the way out. So by analogy, convert to UTC time on the way in and local time on the way out. E.g. datetime.utcfromteimstamp() is probably better for your use case.