0

Here's my problem, I have 2 times I'm feeding into Python, one in EST and other in GMT. I need to convert both to epoch and compare them. From what it's looking like when I convert the EST to epoch, it's should convert to the exact equivalent of GMT I thought. It doesn't look like it is:

from datetime import datetime as dt,datetime,timedelta
import time

# EST
date_time = '09.03.1999' + " " + "08:44:17"
pattern = '%d.%m.%Y %H:%M:%S'
epoch = int(time.mktime(time.strptime(date_time, pattern)))
print epoch

# GMT
date_time2 = '09.03.1999' + " " + "13:44:17.000000"
pattern2 = '%d.%m.%Y %H:%M:%S.%f'
epoch2 = int(time.mktime(time.strptime(date_time2, pattern2)))
print epoch2
Psidom
  • 209,562
  • 33
  • 339
  • 356
user3498593
  • 113
  • 1
  • 1
  • 11
  • You don't need ton convert them to epoch to compare them: http://stackoverflow.com/questions/8142364/how-to-compare-two-dates Otherwise, you may find this resource instructive: http://www.idiotinside.com/2015/03/14/working-with-date-and-time-python-892/ – Aif Jul 22 '16 at 01:04
  • I should clarify that I figured epoch conversion would be easier since I do this comparison later on: `if (epoch - 120) < epoch2 and epoch2 < (epoch + 120)` – user3498593 Jul 22 '16 at 01:08
  • I also will be doing filters with this time in Wireshark, which is easy to do with epoch – user3498593 Jul 22 '16 at 01:11

1 Answers1

0

So, I think you're confusing what epoch means here. Epoch is a representation of time which counts the numbers of seconds from 1970/01/01 00:00:00 to a given a date.

Epoch conversion doesn't care about timezones and you can actually have negative epoch times with timezone conversion (play around at http://www.epochconverter.com/).

A real example: I live in Japan, so local time epoch 0 for me is actually -32400 in GMT epoch.

What you need to do is do something like in this question to first convert between timezones and then do the date to epoch conversion.

Here's some code from the accepted answer:

from datetime import datetime
from dateutil import tz

from_zone = tz.gettz('UTC')
to_zone = tz.gettz('America/New_York')

utc = datetime.strptime('2011-01-21 02:37:21', '%Y-%m-%d %H:%M:%S')

# Tell the datetime object that it's in UTC time zone since 
# datetime objects are 'naive' by default
utc = utc.replace(tzinfo=from_zone)

# Convert time zone
central = utc.astimezone(to_zone)
Community
  • 1
  • 1
DallaRosa
  • 5,737
  • 2
  • 35
  • 53
  • Once I get the times the same, can I use the same convention to convert to epoch: `pattern = '%d.%m.%Y %H:%M:%S' epoch = int(time.mktime(time.strptime(gmt, pattern))) print epoch` – user3498593 Jul 22 '16 at 01:46
  • Nice snippet, however last line: `central = ...` is odd since `'America/New_York'` is Eastern Time Zone in US. Central is for Chicago, and those longitudes in the midwest – joel goldstick Jul 22 '16 at 13:13