By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,675 Members | 2,270 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,675 IT Pros & Developers. It's quick & easy.

Timezones in python

P: n/a
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.
Dec 5 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
lu********@gmail.com wrote:
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>
--
Dec 5 '07 #2

P: n/a
lu********@gmail.com wrote:
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.
http://pytz.sourceforge.net/
http://labix.org/python-dateutil

Christian

Dec 5 '07 #3

P: n/a
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.
Dec 5 '07 #4

P: n/a
En Wed, 05 Dec 2007 06:43:49 -0300, <lu********@gmail.comescribió:
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')

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

--
Gabriel Genellina

Dec 5 '07 #5

P: n/a
On 5 Dec, 13:59, "Gabriel Genellina" <gagsl-...@yahoo.com.arwrote:
En Wed, 05 Dec 2007 06:43:49 -0300, <lukasz....@gmail.comescribió:
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')

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

--
Gabriel Genellina
Thank you that iwll help a lot
Dec 6 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.