convert timezone python

from datetime import datetime
from pytz import timezone, all_timezones
# must read else Bug:
# https://blog.ganssle.io/articles/2018/03/pytz-fastest-footgun.html

# aware dt-obj
dt_obj = datetime.strptime('2021-05-19T01:55:10+0000', '%Y-%m-%dT%H:%M:%S%z')
# double confirmaiton: aware dt-obj
dt_obj.tzinfo
# correct
dt_obj.astimezone(timezone('US/Pacific'))
# correct
dt_obj.replace(tzinfo=timezone('UTC')).astimezone(timezone('US/Pacific'))
# confirmation: desired tz
dt_obj.replace(tzinfo=timezone('UTC')).astimezone(timezone('US/Pacific')).tzinfo
# ~~~~~~~
# naive datetime object
datetime.utcnow()
# confirmation: naive dt-obj
print(datetime.utcnow().tzinfo)
# incorrect because started with naive datetime object
datetime.utcnow().astimezone(timezone('US/Pacific'))
# correct because add/replace tzinfo of initial dt-obj before conversion to desired tz
# SEE article link above. The following is probably correct only bc starting w/ utc.
datetime.utcnow().replace(tzinfo=timezone('UTC')).astimezone(timezone('US/Pacific'))
# confirmation: aware dt-obj
datetime.utcnow().replace(tzinfo=timezone('UTC')).astimezone(timezone('US/Pacific')).tzinfo

from dateutil import tz
dt=datetime(2018, 11, 1,20,0,0)

print(dt)
# 2018-11-01 20:00:00
print(dt.replace(tzinfo=tz.gettz('UTC')).astimezone(tz.gettz('US/Eastern')))
# 2018-11-01 16:00:00-04:00
print(datetime(2018, 11, 1,20,0,0,tzinfo=tz.gettz('UTC')).astimezone(tz.gettz('US/Eastern')))
# 2018-11-01 16:00:00-04:00
print(dt.replace(tzinfo=timezone('UTC')).astimezone(timezone('US/Eastern')))
# 2018-11-01 16:00:00-04:00
print(datetime(2018, 11, 1,20,0,0,tzinfo=timezone('UTC')).astimezone(timezone('US/Eastern')))
# 2018-11-01 16:00:00-04:00
print(timezone('UTC').localize(dt).astimezone(timezone('US/Eastern')))
# 2018-11-01 16:00:00-04:00

# ~~ VS ~~
dt=datetime(2018, 11, 1,16,0,0)
print(dt)
# 2018-11-01 16:00:00
print(dt.replace(tzinfo=tz.gettz('US/Eastern')).astimezone(tz.gettz('US/Pacific')))
# 2018-11-01 13:00:00-07:00
print(datetime(2018, 11, 1,16,0,0,tzinfo=tz.gettz('US/Eastern')).astimezone(tz.gettz('US/Pacific')))
# 2018-11-01 13:00:00-07:00
print(dt.replace(tzinfo=timezone('US/Eastern')).astimezone(timezone('US/Pacific'))) # Bug vector
# 2018-11-01 13:56:00-07:00 # incorrect!
print(datetime(2018, 11, 1,16,0,0,tzinfo=timezone('US/Eastern')).astimezone(timezone('US/Pacific'))) # Bug vector
# 2018-11-01 13:56:00-07:00 # incorrect!
print(timezone('US/Eastern').localize(dt).astimezone(timezone('US/Pacific')))
# 2018-11-01 13:00:00-07:00

# ~~~~~~~~~~~
# len(all_timezones) == 593
for zone in all_timezones:
    print(zone)

3.93
7

                                    import pytz
from dateutil import tz
from datetime import datetime, timedelta
# must read source link else Bug:

NYC_p = pytz.timezone('America/New_York')   # 1.53 µs
NYC_d = tz.gettz('America/New_York')        # 863 ns
# tz Awareness
dt_p = NYC_p.localize(datetime(2018, 11, 1))    # 35.4 µs
dt_d = datetime(2018, 11, 1, tzinfo=NYC_d)      # 1.38 µs

dt_p.utcoffset()        # 655 ns
dt_d.utcoffset()        # 13.9 µs

LA_p = pytz.timezone('America/Los_Angeles')
LA_d = tz.gettz('America/Los_Angeles')
# tz Conversion
NYC_p.localize(datetime(2018, 11, 1)).astimezone(LA_p)  # 44.8 µs
datetime(2018, 11, 1, tzinfo=NYC_d).astimzone(LA_d)     # 32.8 µs

# ~~~~~ Date Arithmetic
dt_winter = datetime(2018, 2, 14, 12, tzinfo=NYC_d)
dt_spring = dt_winter + timedelta(days=60)
print(dt_spring)
# 2018-04-15 12:00:00-04:00 # correct

dt_winter = NYC_p.localize(datetime(2018, 2, 14, 12)) # potential Bug vector1
dt_spring = dt_winter + timedelta(days=60)
print(dt_spring)
# 2018-04-15 12:00:00-05:00 # incorrect. subtle gotcha!
print(NYC_p.normalize(dt_spring)) # potential Bug vector2
# 2018-04-15 13:00:00-04:00 # correct

3.93 (45 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
timezone format datetime python python setting timezone python datetime modify timezone python adjust time for timezone convert str to datetime timezone python how to change your timezone in python using pytz module python date format with timezone python set datetime timezone how to set timezone for datetime python timezone to string python python make datetime with timezone change timezone from datetime python timezone python define python3 to timezone set timezone in datetime python convert time zone python python convert timezone offset convert timezone of datetime python python convert to another timezone python3 convert python datetime to timezone get timezone from date string python python timezone conversion timezone in python datetime set timezone python convert string with timezone to datetime python change timezone from time object python convert string to datetime python with timezone date time timezone python set datetime timezone python python convert timestamp to timezone convert to given timezone python python datetime with timezone python change timezone of datetime change timezone of datetime python convert datetime to local timezone python python time set timezone python convert datetime timezone how to get a formatted time and change timezone python convert a particular timezone value into python convert timezone string to datetime python string datetime timezone conver to python datetime timezone change in python 5:30 timezone Python string to datetime with timezone how to convert datetime object to different timezone python python convert timezone convert time to local timezone python python convert time zone timezone python str to datetime with python with timezone convert string to time with timezone python set timezone of datetime python python time format timezone timezone convertor python pytz convert timezone convert to timezone python python datetime convert timezone how to change timezone in python datetime as timezone python python datetime timezone change timezone converter python converting time object python timezone python timezone from string convert datetime string with timezone to datetime python change datetime timezone python python datetime set timezone from string python default timezone python datetime timezone string python convert to timezone time format python without timezone convert date timezone python python set timezone how to convert time to other timzone python python change timezone python datetime change timezone how to change timezone.now() format python change timezone python datetime convert string to datetime with timezone python python timezone python convert timezone of datetime convert datetime to different timezone python python code to change timezones datetime python convert timezone timezone create a date python python convert timezone offset to datetime convert timezone python to local convert timezone python
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source