Timezones in python

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Dec 5 08:59:32 EST 2007


En Wed, 05 Dec 2007 06:43:49 -0300, <lukasz.f24 at gmail.com> escribió:

> Thanks guys for your answers! I know those library's but I was
> wondering is there something build-in for this simple convert convert.
> I have to do it only from +4 to +0.

Copying the example from the tzinfo docs:

 from datetime import tzinfo, timedelta, datetime

ZERO = timedelta(0)
HOUR = timedelta(hours=1)

# A class building tzinfo objects for fixed-offset time zones.
# Note that FixedOffset(0, "UTC") is a different way to build a
# UTC tzinfo object.

class FixedOffset(tzinfo):
     """Fixed offset in minutes east from UTC."""
     def __init__(self, offset, name):
         self.__offset = timedelta(minutes = offset)
         self.__name = name
     def utcoffset(self, dt):
         return self.__offset
     def tzname(self, dt):
         return self.__name
     def dst(self, dt):
         return ZERO

UTC = FixedOffset(0, 'UTC')
Argentina = FixedOffset(-3*60, 'UTC-3')
UTCPlus4 = FixedOffset(4*60, 'UTC+4')

py> now = datetime.now(Argentina)
py> print now.strftime('%c %Z')
12/05/07 10:52:28 UTC-3
py> print now.astimezone(UTC).strftime('%c %Z')
12/05/07 13:52:28 UTC
py> print now.astimezone(Argentina).strftime('%c %Z')
12/05/07 10:52:28 UTC-3
py> print now.astimezone(UTCPlus4).strftime('%c %Z')
12/05/07 17:52:28 UTC+4

-- 
Gabriel Genellina




More information about the Python-list mailing list