473,513 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Timezones in python

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
5 1917
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
7006
by: Michael | last post by:
I would like to set the timezone of a thread to allow me to calculate the UTC time for data sourced from a number of time zones. Although this can be done in C and C++, I annot find how to do...
0
1135
by: Robert Treat | last post by:
I am trying to figure out if there is a way to determine the timezones supported in postgresql from within the database. If you look at...
1
2123
by: Flack | last post by:
Hey guys, I need to compare two times that the user selects. The user selects the hour, date, and timezone (which can be either NY, LN, or HK timezones). How can I compare two dates of...
2
2915
by: MrBlueSky | last post by:
Hi, I've got a Python application that (as well as lots of other stuff!) has to translate time_t values into strings in the TZ of the users choice. Looking at the Python Library Reference, I can...
5
7797
by: Alex | last post by:
Hi My website is hosted in the States (EST), but the website itself is targeted for UK users (GMT). How can I offset the time so that the server reports it as GMT when my ASP.NET app needs to...
7
3844
by: David T. Ashley | last post by:
In a web database (PHP), per user, I'd like to allow each user to specify their timezone (this would change how times are adjusted for display for that user). How do I enumerate all possible...
27
5280
by: Sanjay | last post by:
Hi All, I am using pytz.common_timezones to populate the timezone combo box of some user registration form. But as it has so many timezones (around 400), it is a bit confusing to the users. Is...
0
795
by: shandy.b | last post by:
I'm trying to write some code to deal with world timezones, and I'm getting a strange result. America/Iqaluit should not be showing up in UTC+00:00, but it is. Can anyone see what's wrong with...
3
2836
by: Tom Anderson | last post by:
Hello! Possibly i'm missing something really obvious here. But ... If i have a date-time string of the kind specified in RFC 1123, like this: Tue, 12 Aug 2008 20:48:59 -0700 Can i turn...
0
7160
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7537
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7099
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5086
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3233
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.