0

My database stores datetime as UTC epoch. I want to filter for a specific date. How can I get the epoch UTC time for the start and end time of that date?

I tried this, but it returns the epoch for the local time (Eastern).

def toEpoch(dt):
    return (dt - datetime.datetime(1970,1,1)).total_seconds()



def getTodayStartEpoch():
    td = datetime.date.today()
    dt = datetime.datetime(td.year, td.month, td.day, 0, 0, 0)
    return toEpoch(dt)
lairtech
  • 2,337
  • 4
  • 25
  • 38
  • possible duplicate of [Converting datetime.date to UTC timestamp in Python](http://stackoverflow.com/questions/8777753/converting-datetime-date-to-utc-timestamp-in-python) – Pavel Anossov Sep 09 '13 at 02:24

1 Answers1

1
import calendar
import datetime

theDay = datetime.date(YEAR, MONTH, DAY)
beginDayEpochTime = calendar.timegm(theDay.timetuple())

The end day epoch time is calculated the same way, but do the next day and subtract 1. Or add 86399 to the begin time.

rlbond
  • 65,341
  • 56
  • 178
  • 228