import time
time1 = time.time()
right now, we are using time.time()
to get the current time in seconds. What should I do to transfer the GMT current time to the EDT current time in seconds format?
import time
time1 = time.time()
right now, we are using time.time()
to get the current time in seconds. What should I do to transfer the GMT current time to the EDT current time in seconds format?
Since GMT time is 4 hours ahead of EST/EDT time,
You simply need to subtract your time1 variable by 4 hours in seconds (60 seconds * 60 minutes * 4 hours = 14,400 seconds)
Therefore your code should look like this:
import time
time_GMT = time.time()
time_EST = time_GMT - 14400
You can go from Unix time to a correctly localized datetime object like
import time
from datetime import datetime, timezone
from zoneinfo import ZoneInfo # Python 3.9
# alternatively, instead of ZoneInfo:
# from dateutil.tz import gettz
time1 = time.time() # unix time, now
# sames as datetime.now(timezone.utc).timestamp()
dt = datetime.fromtimestamp(time1, tz=timezone.utc) # corresponding datetime object
dt_eastern = dt.astimezone(ZoneInfo('US/Eastern')) # gettz('US/Eastern')
print(dt_eastern)
print(repr(dt_eastern))
# 2020-08-25 01:27:05.230289-04:00
# datetime.datetime(2020, 8, 25, 1, 27, 5, 230289, tzinfo=tzfile('US/Eastern'))