datetime and daylight savings problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James

    #1

    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.[color=blue][color=green][color=darkred]
    >>> from pytz import timezone
    >>> from datetime import datetime
    >>> t=timezone("Eur ope/Paris")
    >>> utc=timezone("U TC")
    >>> d=datetime(2005 ,01,24,16,59,tz info=t)
    >>> d[/color][/color][/color]
    datetime.dateti me(2005, 1, 24, 16, 59, tzinfo=<DstTzIn fo
    'Europe/Paris' WET0:00:00 STD>)[color=blue][color=green][color=darkred]
    >>> d.astimezone(ut c)[/color][/color][/color]
    datetime.dateti me(2005, 1, 24, 16, 59, tzinfo=<StaticT zInfo 'UTC'>)[color=blue][color=green][color=darkred]
    >>> d2=datetime(200 5,06,01,16,59,t zinfo=t)
    >>> d2[/color][/color][/color]
    datetime.dateti me(2005, 6, 1, 16, 59, tzinfo=<DstTzIn fo 'Europe/Paris'
    WET0:00:00 STD>)[color=blue][color=green][color=darkred]
    >>> d2.astimezone(u tc)[/color][/color][/color]
    datetime.dateti me(2005, 6, 1, 16, 59, tzinfo=<StaticT zInfo 'UTC'>)

    One of these should be in DST, the other shouldn't, I'm not sure why.
    Additional oddness here
    [color=blue][color=green][color=darkred]
    >>> d.astimezone(ut c).astimezone(t )[/color][/color][/color]
    datetime.dateti me(2005, 1, 24, 18, 59, tzinfo=<DstTzIn fo
    'Europe/Paris' CEST+2:00:00 DST>)[color=blue][color=green][color=darkred]
    >>> d2.astimezone(u tc).astimezone( t)[/color][/color][/color]
    datetime.dateti me(2005, 6, 1, 17, 59, tzinfo=<DstTzIn fo 'Europe/Paris'
    CET+1:00:00 STD>)

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

    James
  • wittempj@hotmail.com

    #2
    Re: datetime and daylight savings problem

    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(secon ds = -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(secon ds = -time.altzone)
    def dst(self, dt):
    return timedelta(secon ds = -time.altzone) - \
    timedelta(secon ds = -time.timezone)
    def tzname(self, dt):
    return "CEST"

    And you create these objects:

    utc = UTC()
    cet = CET()
    cest = CEST()
    d = datetime(2005,0 6,01,16,59,tzin fo=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(ce t)
    Will print:
    As CET 2005-06-01 17:59:00+01:00

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

    Additional:
    This:
    d = datetime(2005,0 6,01,16,59,tzin fo=cet)
    print cet, d

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

    And this:
    d = datetime(2005,0 6,01,16,59,tzin fo=cest)
    print cest, d
    Will print:
    <__main__.CES T 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)?

    Comment

    Working...