Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Timezone conversion

I am looking for a quick way to type in a time and then python convert it into other timezones ( maybe up to 10 different timezones )

Sorry. I am not familar with time in python at all, if someone could put me in the right direction I would really appreciate it.

like image 316
Trying_hard Avatar asked Jun 12 '12 13:06

Trying_hard


People also ask

How do I convert between time zones in Python?

Use the datetime. astimezone() method to convert the datetime from one timezone to another. This method uses an instance of the datetime object and returns a new datetime of a given timezone.

How do you convert UTC to timezone in Python?

This datetime object will have no timezone associated with it. Therefore assign the UTC timezone to this datetime object using replace(tzinfo=pytz. UTC) function. Convert the timezone of the datetime object to local timezone by calling the astimezone() function on datetime object.

How do I convert datetime to timezone?

DateTime currentTime = TimeZoneInfo. ConvertTime(DateTime. Now, TimeZoneInfo. FindSystemTimeZoneById("Central Standard Time"));

Which Python library is used for timezone?

pytz brings the Olson tz database into Python. This library allows accurate and cross platform timezone calculations using Python 2.4 or higher. It also solves the issue of ambiguous times at the end of daylight saving time, which you can read more about in the Python Library Reference ( datetime. tzinfo ).


1 Answers

I have found that the best approach is to convert the "moment" of interest to a utc-timezone-aware datetime object (in python, the timezone component is not required for datetime objects).

Then you can use astimezone to convert to the timezone of interest (reference).

from datetime import datetime import pytz  utcmoment_naive = datetime.utcnow() utcmoment = utcmoment_naive.replace(tzinfo=pytz.utc)  # print "utcmoment_naive: {0}".format(utcmoment_naive) # python 2 print("utcmoment_naive: {0}".format(utcmoment_naive)) print("utcmoment:       {0}".format(utcmoment))  localFormat = "%Y-%m-%d %H:%M:%S"  timezones = ['America/Los_Angeles', 'Europe/Madrid', 'America/Puerto_Rico']  for tz in timezones:     localDatetime = utcmoment.astimezone(pytz.timezone(tz))     print(localDatetime.strftime(localFormat))  # utcmoment_naive: 2017-05-11 17:43:30.802644 # utcmoment:       2017-05-11 17:43:30.802644+00:00 # 2017-05-11 10:43:30 # 2017-05-11 19:43:30 # 2017-05-11 13:43:30 

So, with the moment of interest in the local timezone (a time that exists), you convert it to utc like this (reference).

localmoment_naive = datetime.strptime('2013-09-06 14:05:10', localFormat)  localtimezone = pytz.timezone('Australia/Adelaide')  try:     localmoment = localtimezone.localize(localmoment_naive, is_dst=None)     print("Time exists")      utcmoment = localmoment.astimezone(pytz.utc)  except pytz.exceptions.NonExistentTimeError as e:     print("NonExistentTimeError") 
like image 153
juan Avatar answered Sep 29 '22 09:09

juan