0
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?

Justin Ding
  • 415
  • 1
  • 5
  • 16

2 Answers2

0

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
Jacob K
  • 742
  • 6
  • 13
  • Is the questioner looking to convert GMT to EDT (which is GTM - 4 hours) or to Eastern time, which can be 4 for 5 hours, depending on the date? – Frank Yellin Aug 25 '20 at 02:39
  • That gives you UNIX time that is not UNIX time but off by some hours. I'd consider this rather confusing... better go for a correctly localized datetime object. – FObersteiner Aug 25 '20 at 04:57
0

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'))
FObersteiner
  • 22,500
  • 8
  • 42
  • 72