datetime and daylight savings problem

J

James

I need to import a bunch of data into our database for which there's a
single entry each day which occurs at the same time every day in local
time - so I need to convert this to UTC taking into account local
daylight savings. However daylight savings just don't seem to be
working at all...

Python 2.3.5 (#2, May 4 2005, 08:51:39)
[GCC 3.3.5 (Debian 1:3.3.5-12)] on linux2
Type "help", "copyright", "credits" or "license" for more information.datetime.datetime(2005, 1, 24, 16, 59, tzinfo=<DstTzInfo
'Europe/Paris' WET0:00:00 STD>)datetime.datetime(2005, 6, 1, 16, 59, tzinfo=<DstTzInfo 'Europe/Paris'
WET0:00:00 STD>)datetime.datetime(2005, 6, 1, 16, 59, tzinfo=<StaticTzInfo 'UTC'>)

One of these should be in DST, the other shouldn't, I'm not sure why.
Additional oddness here
datetime.datetime(2005, 1, 24, 18, 59, tzinfo=<DstTzInfo
'Europe/Paris' CEST+2:00:00 DST>)datetime.datetime(2005, 6, 1, 17, 59, tzinfo=<DstTzInfo 'Europe/Paris'
CET+1:00:00 STD>)

I'm not sure if it's just something I'm doing completely wrong here...

James
 
W

wittempj

When working with timezones datetime objects are represented in the
tzinfo object you supply, eg. when you define these classes (and run
them on a system set to Paris time):

from datetime import tzinfo, timedelta, datetime
import time

class UTC(tzinfo):
"""UTC timezone"""
def utcoffset(self, dt):
return timedelta(0)

def tzname(self, dt):
return "UTC"

def dst(self, dt):
return timedelta(0)

class CET(tzinfo):
"""CET timezone"""
def utcoffset(self, dt):
return timedelta(seconds = -time.timezone)
def dst(self, dt):
return timedelta(0)
def tzname(self, dt):
return "CET"


class CEST(tzinfo):
"""CET timezone with DST"""
def utcoffset(self, dt):
return timedelta(seconds = -time.altzone)
def dst(self, dt):
return timedelta(seconds = -time.altzone) - \
timedelta(seconds = -time.timezone)
def tzname(self, dt):
return "CEST"

And you create these objects:

utc = UTC()
cet = CET()
cest = CEST()
d = datetime(2005,06,01,16,59,tzinfo=utc)

This statement
print 'UTC %s' % d
Will print:
UTC 2005-06-01 16:59:00+00:00

And this one:
print 'As CET %s' % d.astimezone(cet)
Will print:
As CET 2005-06-01 17:59:00+01:00

And this one:
print 'As CET with DST %s' % d.astimezone(cest)
Will print:
As CET with DST 2005-06-01 18:59:00+02:00

Additional:
This:
d = datetime(2005,06,01,16,59,tzinfo=cet)
print cet, d

Will print:
<__main__.CET object at 0xb7d3aaac> 2005-06-01 16:59:00+01:00

And this:
d = datetime(2005,06,01,16,59,tzinfo=cest)
print cest, d
Will print:
<__main__.CEST object at 0xb7d3aaec> 2005-06-01 16:59:00+02:00

So at least with these tzinfo objects everything is as expected, I'm
not sure where your actual problem is, is it in the pytz module (with
which I do not have experience)?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top