Re: [Hampshire] Is a given date DST?

Top Page

Reply to this message
Author: Daniel Pope
Date:  
To: hampshire
Subject: Re: [Hampshire] Is a given date DST?
On Tue, Aug 12, 2008 at 09:38:59AM +0100, Keith Edmunds wrote:
> Hi all
>
> I'm looking for a way to write a function (ideally in Python) that takes
> one parameter, a datetime, and returns a Boolean which is true if the
> passed datetime is during "daylight saving time" and is false otherwise.
>
> Background (in case there's a better way): I need to generate some Apache
> logfiles from data stored in a MySQL database, and the output date needs
> the time zone offset included (for the UK, either +0000 or +0100). All the
> dates in question are only ever UK dates, so the offset is either 0 or 1
> hour.


Unfortunately you can't reliably tell which. When the clocks go back, you
get 1am twice, first in BST and an hour later in GMT.

If you install python-tz you can try

>>> import pytz
>>> import datetime
>>> tz = pytz.timezone('Europe/London')
>>> d = datetime.datetime(2008, 8, 12, 11, 35) #naive datetime
>>> d2 = tz.localize(d) #d2 is now a timezone-aware datetime... but may be wrong :(
>>> d2

datetime.datetime(2008, 8, 12, 11, 35, tzinfo=<DstTzInfo 'Europe/London'
BST+1:00:00 DST>)
>>> d2.utcoffset()

datetime.timedelta(0, 3600)

See
http://pytz.sourceforge.net/
for more details including the is_dst kwarg you can use if you know
which timezone you're actually in.

Dan