1

I have a date time say 06-07-2021 11:59:00 (mm-dd-yyyy HH:MM:SS) which is time in EST. This time need to be changed directly to UTC.

To be noted that EST follows Day light saving whereas UTC would not.

nonuser
  • 183
  • 1
  • 10

1 Answers1

2
from datetime import datetime, timedelta, timezone
import time

# make datetime from timestamp, thus no timezone info is attached
now = datetime.fromtimestamp(time.time())

# make local timezone with time.timezone
local_tz = timezone(timedelta(seconds=-time.timezone))

# attach different timezones as you wish
utc_time = now.astimezone(timezone.utc)
local_time = now.astimezone(local_tz)

print(utc_time.isoformat(timespec='seconds')) 
print(local_time.isoformat(timespec='seconds'))
Jay Prakash
  • 149
  • 10