Created
May 2, 2018 21:46
-
-
Save joshgeller/20679c00f11d47c5e25910ed6816bedc to your computer and use it in GitHub Desktop.
Convert local date to midnight UTC timestamp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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