Fix for datetime conversion to integer and back to string; was 1 hour off

Robin Munn rmunn at pobox.com
Wed Apr 30 14:08:58 EDT 2003


La Uruguaya <lacuidadanadelmundo at yahoo.com> wrote:
> I searched this group for a solution to a problem I had converting
> dates from a string format to integer (in seconds since 1970) and back
> to strings.  During some times of the year, the conversion didn't do
> a perfect round trip and I ended up with a datetime that differed from
> the original datetime by one hour.  I found 2 postings with variations
> on the problem, but not a solution.  I implemented the following
> solution and wanted to share it in case somebody needs it later.
[snip code and test]

Interesting: "During some times of the year ...", "... differed from the
original datetime by one hour." Sounds to me like you're running into
daylight savings time issues.

Yes, looking at your code confirms it: you're subtracting the value of
the last element of the tuple (the DST flag, which is 1 if daylight
savings time is in effect) from the third element (the hours).

Did you figure this out from looking at the docs for the time module, or
did you do it by trial and error? Also, it would have been nice if you
had explained more clearly that daylight savings time issues were the
root of your problem; at first glance, your code looked like a "magic"
solution to a strange kind of problem.

-- 
Robin Munn <rmunn at pobox.com>
http://www.rmunn.com/
PGP key ID: 0x6AFB6838    50FF 2478 CFFB 081A 8338  54F7 845D ACFD 6AFB 6838




More information about the Python-list mailing list