Timezones in python

L

lukasz.f24

Hello,

Is there any build in solution in python to handle timezones? My
problem is I have to convert +4 time to +0. In the worst case i can
just add +4 to the houer but I'm not very happy about that. Another
problem is the summer/winter timechange which happen with one week
difference. I am looking for something what can convert different
timezone to localtime.

Thank you.
 
M

Matt Nordhoff

Is there any build in solution in python to handle timezones? My
problem is I have to convert +4 time to +0. In the worst case i can
just add +4 to the houer but I'm not very happy about that. Another
problem is the summer/winter timechange which happen with one week
difference. I am looking for something what can convert different
timezone to localtime.

There's the pytz library [1] [2], which uses the tz database popular on
Unix systems, and python-dateutil [3] [4] also includes support for it,
among doing other date- and time-related things.

Those support time zone names (America/New_York or EST5EDT), and also
converting between them. If you only want to convert between +4 and +0,
without giving names for them, you don't need them, but I don't know how
to do it. Using UTC internally and only converting to other zones when
reading data in and displaying it makes things simpler . .

[1] <http://pytz.sourceforge.net/>
[2] <http://pypi.python.org/pypi/pytz>
[3] <http://labix.org/python-dateutil>
[4] <http://pypi.python.org/pypi/dateutil>
--
 
L

lukasz.f24

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.
 
G

Gabriel Genellina

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
 
L

lukasz.f24

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

Thank you that iwll help a lot
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top