cube-studio/myapp/utils/dates.py
2021-11-25 18:07:05 +08:00

19 lines
399 B
Python

from datetime import datetime
import pytz
EPOCH = datetime(1970, 1, 1)
def datetime_to_epoch(dttm):
if dttm.tzinfo:
dttm = dttm.replace(tzinfo=pytz.utc)
epoch_with_tz = pytz.utc.localize(EPOCH)
return (dttm - epoch_with_tz).total_seconds() * 1000
return (dttm - EPOCH).total_seconds() * 1000
def now_as_float():
return datetime_to_epoch(datetime.utcnow())