datetime_utils¶
round_datetime¶
- datetime_utils.round_datetime(dt, period, tzinfo=<UTC>, force=False)¶
Rounds a datetime to the nearest period. Valid periods are: minute, minute-15, hour, day, week
Parameters: - dt (datetime) – A naive or aware datetime object.
- period (str) – Options are minute, minute-15, hour, day, week
- tzinfo (pytz timezone) – A pytz timezone object. If given, the round will be performed with respect to the timezone.
- force (bool) – A boolean value. If force=True, it causes pre-rounded values to jump another step anyway.
Return type: datetime
Returns: A datetime object that results from rounding datetime to the period. The timezone of the returned datetime will be equivalent to the original timezone of dt (or its DST equivalent if a DST border was crossed). If the input time was naive, it returns a naive datetime object.
Raises: Exception if the period is not supported
>>> import datetime >>> import pytz >>> import datetime_utils >>> # Weeks start on Monday, so the ceil will be for the next Monday >>> print datetime_utils.round_datetime(datetime.datetime(2013, 3, 3, 5), period='week') 2013-03-04 00:00:00 >>> print datetime_utils.round_datetime(datetime.datetime(2013, 3, 3, 5), period='day') 2013-03-03 00:00:00 >>> # Pass an aware datetime and return an aware datetime >>> print datetime_utils.round_datetime(datetime.datetime(2013, 3, 3, 5, tzinfo=pytz.utc), period='day') 2013-03-03 00:00:00+00:00 >>> print datetime_utils.round_datetime(datetime.datetime(2013, 3, 4, 6), period='day', ... tzinfo=pytz.timezone('US/Eastern')) 2013-03-04 05:00:00 >>> # Start with a naive UTC time and floor it with respect to EST >>> dt = datetime.datetime(2013, 2, 1) >>> # Since it is January 31 in EST, the resulting floored value >>> # for a day will be the previous day. Also, the returned value is >>> # in the original naive timezone of UTC >>> print datetime_utils.round_datetime(dt, period='day', tzinfo=pytz.timezone('US/Eastern')) 2013-01-31 05:00:00 >>> # Since it is January 31 in CST, the resulting floored value >>> # for a day will be the previous day. Also, the returned value is >>> # in the original timezone of EST >>> print datetime_utils.round_datetime(datetime.datetime(2013, 2, 1, 5, tzinfo=pytz.timezone('US/Eastern')), ... period='day', tzinfo=pytz.timezone('US/Central')) 2013-02-01 01:00:00-05:00
round_datetime_to_15min¶
- datetime_utils.datetime_utils.round_datetime_to_15min(dt, tzinfo=None, force=False)¶
Rounds a datetime to the nearest 15 minute interval.
Parameters: - dt (datetime) – A naive or aware datetime object.
- tzinfo (pytz timezone) – A pytz timezone object. If given, the round will be performed with respect to the timezone.
- force (bool) – A boolean value. If force=True, it causes pre-rounded values to jump another step anyway.
Return type: datetime
Returns: A datetime object that results from rounding datetime to a 15 minute interval. The timezone of the returned datetime will be equivalent to the original timezone of dt (or its DST equivalent if a DST border was crossed). If the input time was naive, it returns a naive datetime object.
>>> import datetime >>> import pytz >>> import datetime_utils >>> # Weeks start on Monday, so the floor will be for the previous Monday >>> print datetime_utils.round_datetime_to_15min(datetime.datetime(2013, 3, 3, 5, 17)) 2013-03-03 05:15:00 >>> print datetime_utils.round_datetime_to_15min(datetime.datetime(2013, 3, 3, 5, 40)) 2013-03-03 05:45:00 >>> # Pass an aware datetime and return an aware datetime >>> print datetime_utils.round_datetime_to_15min(datetime.datetime(2013, 3, 3, 5, 34, tzinfo=pytz.utc)) 2013-03-03 05:30:00+00:00 >>> print datetime_utils.round_datetime_to_15min(datetime.datetime(2013, 3, 4, 6, 10), ... tzinfo=pytz.timezone('US/Eastern')) 2013-03-04 06:15:00 >>> # Start with a naive UTC time and floor it with respect to EST >>> dt = datetime.datetime(2013, 2, 1, 2, 56) >>> # Since it is January 31 in EST, the resulting floored value >>> # for a day will be the previous day. Also, the returned value is >>> # in the original naive timezone of UTC >>> print datetime_utils.round_datetime_to_15min(dt, tzinfo=pytz.timezone('US/Eastern')) 2013-02-01 03:00:00 >>> # Since it is January 31 in CST, the resulting floored value >>> # for a day will be the previous day. Also, the returned value is >>> # in the original timezone of EST >>> print datetime_utils.round_datetime_to_15min(datetime.datetime(2013, 2, 1, 5, 2, ... tzinfo=pytz.timezone('US/Eastern')), tzinfo=pytz.timezone('US/Central')) 2013-02-01 04:45:00-05:00
is_snapped_to¶
- datetime_utils.datetime_utils.is_snapped_to(dt, period, tzinfo=None)¶
Checks if the datetime is ‘snapped’ to the period.
Parameters: - dt (datetime) – A naive or aware datetime object.
- period (str) – Options are minute, minute-15, hour, day
- tzinfo (pytz timezone) – A pytz timezone object. If a timezone is specified, the check is done in that timezone. Else it is done in the timezone of the datetime.
Return type: bool
Returns: A boolean value that results from checking if the given datetime is snapped to the period specified
>>> import datetime >>> import pytz >>> import datetime_utils >>> print datetime_utils.is_snapped_to(datetime.datetime(2013, 3, 3), period='day') True >>> print datetime_utils.is_snapped_to(datetime.datetime(2013, 3, 3, 5), period='day') False >>> # Pass an aware datetime and return an aware datetime >>> print datetime_utils.is_snapped_to(datetime.datetime(2013, 3, 3, tzinfo=pytz.utc), period='day') True >>> print datetime_utils.is_snapped_to(datetime.datetime(2013, 2, 1, 5, tzinfo=pytz.utc), period='day', ... tzinfo=pytz.timezone('US/Eastern')) True >>> print datetime_utils.is_snapped_to(datetime.datetime(2013, 3, 3, 4, tzinfo=pytz.utc), period='hour') True >>> print datetime_utils.is_snapped_to(datetime.datetime(2013, 2, 1, 5, tzinfo=pytz.utc), period='hour', ... tzinfo=pytz.timezone('US/Eastern')) True >>> print datetime_utils.is_snapped_to(datetime.datetime(2013, 3, 3, 4, 20, tzinfo=pytz.utc), period='minute') True >>> print datetime_utils.is_snapped_to(datetime.datetime(2013, 2, 1, 5, 20, tzinfo=pytz.utc), period='minute', ... tzinfo=pytz.timezone('US/Eastern')) True >>> print datetime_utils.is_snapped_to(datetime.datetime(2013, 3, 3, 4, 20, ... tzinfo=pytz.utc), period='minute-15') False >>> print datetime_utils.is_snapped_to(datetime.datetime(2013, 2, 1, 5, 45, ... tzinfo=pytz.utc), period='minute-15', ... tzinfo=pytz.timezone('US/Eastern')) True
is_snapped_to_15min¶
- datetime_utils.datetime_utils.is_snapped_to_15min(dt, tzinfo=None)¶
Checks if the datetime is ‘snapped’ to a 15 minute interval.
Parameters: - dt (datetime) – A naive or aware datetime object.
- tzinfo (pytz timezone) – A pytz timezone object. If a timezone is specified, the check is done in that timezone. Else it is done in the timezone of the datetime.
Return type: bool
Returns: A boolean value that results from checking if the given datetime is snapped to a 15 minute interval
>>> import datetime >>> import pytz >>> import datetime_utils >>> print datetime_utils.is_snapped_to(datetime.datetime(2013, 3, 3, 4, 20, ... tzinfo=pytz.utc), period='minute-15') False >>> print datetime_utils.is_snapped_to(datetime.datetime(2013, 2, 1, 5, 45, ... tzinfo=pytz.utc), period='minute-15', tzinfo=pytz.timezone('US/Eastern')) True