Answers for "datetime convert timezone python to local"

2

get local timezone python

import datetime

now = datetime.datetime.now()
local_now = now.astimezone()
local_tz = local_now.tzinfo
local_tzname = local_tz.tzname(local_now)
print(local_tzname)
Posted by: Guest on January-26-2021
0

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)
Posted by: Guest on May-20-2021

Code answers related to "datetime convert timezone python to local"

Python Answers by Framework

Browse Popular Code Answers by Language