468,290 Members | 2,030 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,290 developers. It's quick & easy.

datetime and daylight savings problem

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.
from pytz import timezone
from datetime import datetime
t=timezone("Europe/Paris")
utc=timezone("UTC")
d=datetime(2005,01,24,16,59,tzinfo=t)
d datetime.datetime(2005, 1, 24, 16, 59, tzinfo=<DstTzInfo
'Europe/Paris' WET0:00:00 STD>) d.astimezone(utc) datetime.datetime(2005, 1, 24, 16, 59, tzinfo=<StaticTzInfo 'UTC'>) d2=datetime(2005,06,01,16,59,tzinfo=t)
d2 datetime.datetime(2005, 6, 1, 16, 59, tzinfo=<DstTzInfo 'Europe/Paris'
WET0:00:00 STD>) d2.astimezone(utc) 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
d.astimezone(utc).astimezone(t) datetime.datetime(2005, 1, 24, 18, 59, tzinfo=<DstTzInfo
'Europe/Paris' CEST+2:00:00 DST>) d2.astimezone(utc).astimezone(t)

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
Oct 11 '05 #1
1 3547
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)?

Oct 11 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Bathroom_Monkey | last post: by
1 post views Thread by Adam Monsen | last post: by
3 posts views Thread by chrisdevey | last post: by
9 posts views Thread by Phil B | last post: by
3 posts views Thread by Generic Usenet Account | last post: by
reply views Thread by NPC403 | last post: by
2 posts views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.