Skip to content

Instantly share code, notes, and snippets.

@joshgeller
Created May 2, 2018 21:46
Show Gist options
  • Save joshgeller/20679c00f11d47c5e25910ed6816bedc to your computer and use it in GitHub Desktop.
Save joshgeller/20679c00f11d47c5e25910ed6816bedc to your computer and use it in GitHub Desktop.
Convert local date to midnight UTC timestamp
import pytz
from datetime import date, datetime, time
def today_to_midnight_utc(local_tz='UTC'):
"""
Returns today's date (local timezone) as a midnight UTC timestamp.
e.g. If date is 04/20/18, and server timezone is America/Los_Angeles:
today_to_midnight_utc(local_tz='America/Los_Angeles')
>> 1524182400
"""
tz = pytz.timezone(local_tz)
local_date = datetime.now(tz).date()
midnight_no_tz = datetime.combine(local_date, time())
midnight_tz = tz.localize(midnight_no_tz)
midnight_utc = midnight_tz.astimezone(pytz.utc)
return int(midnight_utc.timestamp())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment