Class - mitto.tz.RelativeDates

JSON Schema

RelativeDates

General support for creation of relative dates spanning various amounts of time as well as Tableau-specific support for creating view filters with relative dates.

>>> from datetime import datetime
>>> mon = datetime(2020, 9, 14, 12, 23, 49, 456789)
>>> tue = datetime(2020, 9, 15, 12, 23, 49, 987654)
>>> conf = {"timezone": "America/Chicago", "date_range":"TODAY"}
>>> rd = RelativeDates(**conf)
>>> rd.as_date_range(now=mon)
{'global_start_datetime': '2020-09-14 00:00:00.000000', 'global_end_datetime': '2020-09-14 23:59:59.999999'}
>>> rd.as_date_range(now=tue)
{'global_start_datetime': '2020-09-15 00:00:00.000000', 'global_end_datetime': '2020-09-15 23:59:59.999999'}
>>> rd.as_date_range(now=tue, date_range="THIS_YEAR")
{'global_start_datetime': '2020-01-01 00:00:00.000000', 'global_end_datetime': '2020-12-31 23:59:59.999999'}
>>> conf = {"timezone": "America/Chicago", "date_range": "LAST_YEAR", "datetime_format": "%Y-%m-%d"}
>>> rd = RelativeDates(**conf)
>>> rd.as_date_range(now=mon)
{'global_start_datetime': '2019-01-01', 'global_end_datetime': '2019-12-31'}

TIMEZONE HANDLING

“now” last hour of year and naive datetime, range should be in “timezone” >>> conf = {“now”: “2023-12-31T23:00:00”, “timezone”: “America/Chicago”, “date_range”: “THIS_YEAR”, “datetime_format”: “%Y-%m-%dT%H:%M:%S.%f%z”} >>> rd = RelativeDates(**conf) >>> rd.as_date_range() {‘global_start_datetime’: ‘2023-01-01T00:00:00.000000-0600’, ‘global_end_datetime’: ‘2023-12-31T23:59:59.999999-0600’}

“now” is last hour of year and in CT, range should be in “timezone” >>> conf = {“now”: “2023-12-31T23:00:00-06:00”, “timezone”: “America/Chicago”, “date_range”: “THIS_YEAR”, “datetime_format”: “%Y-%m-%dT%H:%M:%S.%f%z”} >>> rd = RelativeDates(**conf) >>> rd.as_date_range() {‘global_start_datetime’: ‘2023-01-01T00:00:00.000000-0600’, ‘global_end_datetime’: ‘2023-12-31T23:59:59.999999-0600’}

“now” is last hour of year and in CT, range should be in “timezone” and in the next year >>> conf = {“now”: “2023-12-31T23:00:00-06:00”, “timezone”: “UTC”, “date_range”: “THIS_YEAR”, “datetime_format”: “%Y-%m-%dT%H:%M:%S.%f%z”} >>> rd = RelativeDates(**conf) >>> rd.as_date_range() {‘global_start_datetime’: ‘2024-01-01T00:00:00.000000+0000’, ‘global_end_datetime’: ‘2024-12-31T23:59:59.999999+0000’}

“now” is first hour of year and in UTC, range should be 6 hours behind in “timezone” and in the previous year >>> conf = {“now”: “2024-01-01T00:00:00-00:00”, “timezone”: “America/Chicago”, “date_range”: “THIS_HOUR”, “datetime_format”: “%Y-%m-%dT%H:%M:%S.%f%z”} >>> rd = RelativeDates(**conf) >>> rd.as_date_range() {‘global_start_datetime’: ‘2023-12-31T18:00:00.000000-0600’, ‘global_end_datetime’: ‘2023-12-31T18:59:59.999999-0600’}

>>> config = {"start_datetime": "begin", "end_datetime": "end"}
>>> rd = RelativeDates(**config)
>>> rd.as_view_filter(mon, tue)
{'begin': '2020-09-14 12:23:49.456789', 'end': '2020-09-15 12:23:49.987654'}
>>> config["datetime_format"] = "%Y-%m-%d %H:%M:%S.%F"
>>> rd=RelativeDates(**config)
>>> rd.as_view_filter(mon, tue)
{'begin': '2020-09-14 12:23:49.456', 'end': '2020-09-15 12:23:49.987'}
>>> mon = datetime(2020, 9, 14, 12, 23, 49, 0)
>>> rd.as_view_filter(mon, tue)
{'begin': '2020-09-14 12:23:49.000', 'end': '2020-09-15 12:23:49.987'}
>>> config["datetime_format"] = "%Y/%m/%d"
>>> rd=RelativeDates(**config)
>>> rd.as_view_filter(mon, tue)
{'begin': '2020/09/14', 'end': '2020/09/15'}

type

object

properties

  • start_datetime

Start Datetime

Filter name to use when specifying the beginning of a datetime range.

type

string

examples

earliest_datetime

default

global_start_datetime

  • end_datetime

End Datetime

Filter name to use when specifying the end of a datetime range.

type

string

examples

latest_datetime

default

global_end_datetime

  • datetime_format

Datetime Format

Python datetime.strftime() format string to use when formatting start_datetime and end_datetime. The format string supports the non-standard format code of %F, which similar to %f except that it is milliseconds instead of microseconds (three digits instead of six). %F can only be used at the end of datetime_format value.

Reference: https://strftime.org

type

string

examples

%Y-%m-%d

%H:%M:%S

%Y-%m-%d %H:%M:%S.%F

%Y-%m-%dT%H:%M:%S.%f%z

default

%Y-%m-%d %H:%M:%S.%f

  • timezone

Timezone

The timezone to use when creating relative dates. This This must be a timezone tz (Olsen) database format. See: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

type

string

examples

America/Chicago

UTC

default

UTC

  • date_range

Date Range

If present, it must be a string supported by mitto.tz.DateRange. The value controls the span of time covered by the date range.

type

string

examples

THIS_HOUR

LAST_YEAR

THIS_YEAR

NEXT_YEAR

YESTERDAY

TODAY

TOMORROW

LAST_7_DAY

NEXT_5_YEAR

  • now

Now

Only for use in testing. If present, it will be used as the current date/time when generating relative dates.

type

string

examples

2024-07-26 01:23:45

2024-07-26 01:23:45Z

2024-07-26 01:23:45 T0500

default

2024-07-26 01:23:45

format

date-time

additionalProperties

False