Caught out by daylight saving :-(

Chris cwitts at gmail.com
Mon Mar 30 08:08:48 EDT 2009


On Mar 30, 1:47 pm, CinnamonDonkey <CinnamonDon... at googlemail.com>
wrote:
> Hi All,
>
> I had the following bit of code which was working fine until we went
> into Daylight saving this weekend, now the result is an hour out.
>
>     timeString = "20090330 15:45:23"
>
>     timeFormat = '%Y-%m-%d %H:%M:%S'
>
>     modificationTime = datetime.datetime.utcfromtimestamp( time.mktime
> ( time.strptime( timeString, timeFormat ) ) )
>     minutesToAdvance = datetime.timedelta( minutes=5 )
>
>     modificationTime = modificationTime + minutesToAdvance
>
>     datetimeString = str ( modificationTime ).replace( ' ', 'T' )
>
> The expected result should be:
>
>     datetimeString = "20090330T15:50:23"
>
> But instead I get:
>
>     datetimeString = "20090330T14:50:23"
>
> I believe it is going wrong at either the mktime() or utcfromtimestamp
> () stage.
>
> What is the correct way to fix this compensating for daylight saving
> automatically?
>
> Regards,
> SHaun >8)

Take a look at the datetime docs
http://docs.python.org/library/datetime.html#datetime.tzinfo.dst



More information about the Python-list mailing list