简体   繁体   中英

How to create a timezone-aware time object in python

I need to add timezones (for example, New York) to a time object I create

Like this:

import datetime
import pytz

mytime = datetime.time(12,00,00)
time_zone = pytz.timezone("America/New_York")
local_time = time_zone.localize(mytime)

but I got this error:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/admin1/envs/myvenv/lib/python3.8/site-packages/pytz/tzinfo.py", line 323, in localize
    loc_dt = dt + delta
TypeError: unsupported operand type(s) for +: 'datetime.time' and 'datetime.timedelta'
import pytz
import datetime

time_zone = pytz.timezone("America/New_York")
local_time = datetime.time(hour=12, tzinfo=time_zone)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM