0

After reading a few SO articles about converting to UNIX/Epoch time from a normal datetime, I am still confused. Can someone explain what I'm not seeing here:

import time
from datetime import datetime
import dateutil.parser
import pytz

tz_LA = pytz.timezone('America/Los_Angeles')
tz_DV = pytz.timezone('America/Denver')

date_start_naive = '2017-04-16T00:00:00'
date_end_naive = '2017-04-16T23:59:59'

# Convert the ISO-8601 time into epoch time:
date_start_LA = tz_LA.localize(dateutil.parser.parse(date_start_naive))
date_start_DV = tz_DV.localize(dateutil.parser.parse(date_start_naive))
date_end = dateutil.parser.parse(date_end_naive)

print(date_start_LA.strftime('%s'))
print(date_start_DV.strftime('%s'))
print(date_end.strftime('%s'))

which produces:

>>> 1492322400
>>> 1492322400
>>> 1492408799

forgive my naivety, but shouldn't the first two integers be an hour different since 2017-04-16T00:00:00 in LA is 2017-04-16T01:00:00 in Denver?

melpomene
  • 84,125
  • 8
  • 85
  • 148
Blairg23
  • 11,334
  • 6
  • 72
  • 72
  • You should probably start by seperating out the `dateutil.parser.parse` call and make sure that's giving you want you want. – Paul Apr 17 '17 at 16:32
  • What version of Python are you using that %s is a valid format string? The documentation for Python 2.7 and 3.6 don't include that. – Paul Apr 17 '17 at 16:35
  • @Paul You're right. I was using an SO answer that wasn't right. `%s` is a bad way to format as Epoch time. It uses your system time (which is localized to your system). You should use `.timestamp()` instead and it gets the right answer. – Blairg23 Apr 17 '17 at 16:52

0 Answers0