classification
Title: datetime needs and "epoch" method
Type: behavior
Components: Library (Lib) Versions: Python 3.0, Python 2.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Neil Muller, davidfraser, hodgestar, tebeka, werneck
Priority: Keywords: patch

Created on 2008-05-01 21:03 by tebeka, last changed 2008-05-11 08:25 by Neil Muller.

Files
File name Uploaded Description Edit Remove
add-datetime-totimestamp-method.diff hodgestar, 2008-05-10 14:55 Implementation of datetime.datetime.timetuple and tests.
add-datetime-totimestamp-method-docs.diff hodgestar, 2008-05-10 15:54
Messages
msg66045 (view) Author: Miki Tebeka (tebeka) Date: 2008-05-01 21:03
If you try to convert datetime objects to seconds since epoch and back
it will not work since the microseconds get lost:

>>> dt = datetime(2008, 5, 1, 13, 35, 41, 567777)
>>> seconds = mktime(dt.timetuple())
>>> datetime.fromtimestamp(seconds) == dt
False

Current fix is to do
>>> seconds += (dt.microsecond / 1000000.0)
>>> datetime.fromtimestamp(seconds) == dt
True
msg66140 (view) Author: Pedro Werneck (werneck) Date: 2008-05-03 02:18
That's expected as mktime is just a thin wrapper over libc mktime() and
it does not expect microseconds. Changing time.mktime doesn't seems an
option, so the best alternative is to implement a method in datetime
type. Is there a real demand for C code implementing this to justify it?
msg66532 (view) Author: Simon Cross (hodgestar) Date: 2008-05-10 14:55
Attached a patch which adds a .totimetuple(...) method to
datetime.datetime and tests for it.

The intention is that the dt.totimetuple(...) method is equivalent to:
mktime(dt.timetuple()) + (dt.microsecond / 1000000.0)
msg66539 (view) Author: Simon Cross (hodgestar) Date: 2008-05-10 15:54
Patch adding documentation for datetime.totimestamp(...).
msg66601 (view) Author: Miki Tebeka (tebeka) Date: 2008-05-11 06:12
I think the name is not good, should be "toepoch" or something like that.
msg66610 (view) Author: Neil Muller (Neil Muller) Date: 2008-05-11 08:25
datetime has fromtimestamp already, so using totimestamp keeps naming
consistency (see toordinal and fromordinal).
History
Date User Action Args
2008-05-11 08:25:30Neil Mullersetnosy: + Neil Muller
messages: + msg66610
2008-05-11 06:12:39tebekasetmessages: + msg66601
2008-05-10 20:48:17davidfrasersetnosy: + davidfraser
2008-05-10 15:54:41hodgestarsetfiles: + add-datetime-totimestamp-method-docs.diff
messages: + msg66539
2008-05-10 14:55:39hodgestarsetfiles: + add-datetime-totimestamp-method.diff
keywords: + patch
messages: + msg66532
nosy: + hodgestar
2008-05-03 02:18:59wernecksetnosy: + werneck
messages: + msg66140
2008-05-01 21:03:25tebekacreate