简体   繁体   中英

Python - Convert UTC datetime.timetz() to local time

Having a hard time searching for this since it's mostly datetime or similar-but-different results and still seeming to struggle with getting suggestions for slightly different problems to work with this one.

I have some UTC datetime object: timestamp . What I'm doing is dealing with many timestamps and corresponding values and bucketing them hour by hour, so I'm bucketing them by timestamp.timetz() .

Now I want to loop through these buckets, take this UTC time object, and convert it to Eastern time, eg I want 00:00 to become 20:00 , keeping daylight savings in mind.

How do I accomplish this?

You can use the "pytz" package to get this done. Below is a example to get current time using pytz in eastern timezone.

import pytz
from datetime import datetime

est_tz = pytz.timezone('US/Eastern')
datetime.now(est_tz)

Hope it helps. Happy Coding !! :)

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