2013-04-23 18:51:17 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2012-12-05 19:53:23 +01:00
|
|
|
import datetime
|
2012-12-11 23:08:17 +01:00
|
|
|
import calendar
|
2017-04-15 03:29:56 +02:00
|
|
|
from django.utils.timezone import utc as timezone_utc
|
2012-12-05 19:53:23 +01:00
|
|
|
|
2017-02-28 18:39:36 +01:00
|
|
|
def floor_to_hour(dt):
|
2016-12-20 03:34:05 +01:00
|
|
|
# type: (datetime.datetime) -> datetime.datetime
|
2017-02-28 18:39:36 +01:00
|
|
|
return datetime.datetime(*dt.timetuple()[:4]) \
|
|
|
|
.replace(tzinfo=dt.tzinfo)
|
2016-12-20 03:34:05 +01:00
|
|
|
|
2017-02-28 18:39:36 +01:00
|
|
|
def floor_to_day(dt):
|
analytics: Simplify frequency and measurement interval options.
Change the CountStat object to take an is_gauge variable instead of a
smallest_interval variable. Previously, (smallest_interval, frequency)
could be any of (hour, hour), (hour, day), (hour, gauge), (day, hour),
(day, day), or (day, gauge).
The current change is equivalent to excluding (hour, day) and (day, hour)
from the list above.
This change, along with other recent changes, allows us to simplify how we
handle time intervals. This commit also removes the TimeInterval object.
2016-10-14 00:15:46 +02:00
|
|
|
# type: (datetime.datetime) -> datetime.datetime
|
2017-02-28 18:39:36 +01:00
|
|
|
return datetime.datetime(*dt.timetuple()[:3]) \
|
|
|
|
.replace(tzinfo=dt.tzinfo)
|
analytics: Simplify frequency and measurement interval options.
Change the CountStat object to take an is_gauge variable instead of a
smallest_interval variable. Previously, (smallest_interval, frequency)
could be any of (hour, hour), (hour, day), (hour, gauge), (day, hour),
(day, day), or (day, gauge).
The current change is equivalent to excluding (hour, day) and (day, hour)
from the list above.
This change, along with other recent changes, allows us to simplify how we
handle time intervals. This commit also removes the TimeInterval object.
2016-10-14 00:15:46 +02:00
|
|
|
|
2017-02-28 18:39:36 +01:00
|
|
|
def ceiling_to_hour(dt):
|
2016-12-20 03:34:05 +01:00
|
|
|
# type: (datetime.datetime) -> datetime.datetime
|
2017-02-28 18:39:36 +01:00
|
|
|
floor = floor_to_hour(dt)
|
|
|
|
if floor == dt:
|
2016-12-20 03:34:05 +01:00
|
|
|
return floor
|
|
|
|
return floor + datetime.timedelta(hours=1)
|
|
|
|
|
2017-02-28 18:39:36 +01:00
|
|
|
def ceiling_to_day(dt):
|
2016-12-20 03:34:05 +01:00
|
|
|
# type: (datetime.datetime) -> datetime.datetime
|
2017-02-28 18:39:36 +01:00
|
|
|
floor = floor_to_day(dt)
|
|
|
|
if floor == dt:
|
2016-12-20 03:34:05 +01:00
|
|
|
return floor
|
|
|
|
return floor + datetime.timedelta(days=1)
|
|
|
|
|
2012-12-05 19:53:23 +01:00
|
|
|
def timestamp_to_datetime(timestamp):
|
2016-06-03 23:32:55 +02:00
|
|
|
# type: (float) -> datetime.datetime
|
2017-04-15 03:29:56 +02:00
|
|
|
return datetime.datetime.fromtimestamp(float(timestamp), tz=timezone_utc)
|
2012-12-11 23:08:17 +01:00
|
|
|
|
2017-02-28 18:39:36 +01:00
|
|
|
class TimezoneNotUTCException(Exception):
|
|
|
|
pass
|
2016-07-29 21:52:45 +02:00
|
|
|
|
2017-02-28 18:39:36 +01:00
|
|
|
def datetime_to_timestamp(dt):
|
|
|
|
# type: (datetime.datetime) -> int
|
2017-04-15 03:29:56 +02:00
|
|
|
if dt.tzinfo is None or dt.tzinfo.utcoffset(dt) != timezone_utc.utcoffset(dt):
|
2017-02-28 18:39:36 +01:00
|
|
|
raise TimezoneNotUTCException("Datetime %s to be converted does not have a UTC timezone." % (dt,))
|
|
|
|
return calendar.timegm(dt.timetuple())
|