Guest User

Untitled

a guest
Dec 12th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. # Convert UTC datetime or string of UTC date to local datetime
  2. # phython 3.3+
  3. from datetime import datetime, timezone
  4.  
  5. time_utc = datetime.strptime(utc_string, "%Y-%m-%d %H:%M:%S")
  6. time = time_utc.replace(tzinfo=timezone.utc).astimezone(tz=None)
  7.  
  8.  
  9. # Python 2
  10. import calendar
  11. from datetime import datetime, timedelta
  12.  
  13. def utc_to_local(utc_dt):
  14. # get integer timestamp to avoid precision lost
  15. timestamp = calendar.timegm(utc_dt.timetuple())
  16. local_dt = datetime.fromtimestamp(timestamp)
  17. assert utc_dt.resolution >= timedelta(microseconds=1)
  18. return local_dt.replace(microsecond=utc_dt.microsecond)
Add Comment
Please, Sign In to add comment