Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Convert UTC datetime or string of UTC date to local datetime
- # phython 3.3+
- from datetime import datetime, timezone
- time_utc = datetime.strptime(utc_string, "%Y-%m-%d %H:%M:%S")
- time = time_utc.replace(tzinfo=timezone.utc).astimezone(tz=None)
- # Python 2
- import calendar
- from datetime import datetime, timedelta
- def utc_to_local(utc_dt):
- # get integer timestamp to avoid precision lost
- timestamp = calendar.timegm(utc_dt.timetuple())
- local_dt = datetime.fromtimestamp(timestamp)
- assert utc_dt.resolution >= timedelta(microseconds=1)
- return local_dt.replace(microsecond=utc_dt.microsecond)
Add Comment
Please, Sign In to add comment