date_utils¶
Datetime and timezone helpers built on pytz and dateutil.
This module centralizes the timezone handling of the package: it exposes the DEFAULT_TZ resolved from the DEFAULT_TZ environment variable plus helpers to get the current time, convert timestamps, parse date strings and shift or floor datetimes.
ny_market_timezone()
¶
Returns the NYSE market timezone.
Source code in py_misc_utils/date_utils.py
26 27 28 | |
now(tz=None)
¶
Returns the current datetime, in a given timezone.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tz
|
An optional timezone - the default timezone if None. |
None
|
Returns:
| Type | Description |
|---|---|
|
The current timezone-aware datetime. |
Source code in py_misc_utils/date_utils.py
31 32 33 34 35 36 37 38 39 40 | |
from_timestamp(ts, tz=None)
¶
Converts a unix timestamp to a datetime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ts
|
The unix timestamp - in seconds. |
required | |
tz
|
An optional timezone to attach to the datetime - the default timezone if None. |
None
|
Returns:
| Type | Description |
|---|---|
|
The timezone-aware datetime. |
Source code in py_misc_utils/date_utils.py
43 44 45 46 47 48 49 50 51 52 53 54 | |
make_datetime_from_epoch(s, tz=None)
¶
Converts epoch-second values to datetimes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
The epoch-second values: a scalar, list, numpy array, or pandas object. |
required | |
tz
|
An optional timezone the values are converted to - UTC by default. |
None
|
Returns:
| Type | Description |
|---|---|
|
The timezone-aware datetime(s), in the type of the input values. |
Source code in py_misc_utils/date_utils.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
parse_date(dstr, tz=None)
¶
Parses a date-time string.
Accepts epoch timestamps - prefixed with '@' - ISO-8601 strings, and the date formats best-effort parsed by dateutil.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dstr
|
The date-time string to parse. |
required | |
tz
|
An optional timezone to attach to the parsed datetime, when the string does not hold timezone information. |
None
|
Returns:
| Type | Description |
|---|---|
|
The timezone-aware datetime. |
Source code in py_misc_utils/date_utils.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | |
day_offset(dt)
¶
Splits a datetime into its time-of-day offset and day start.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dt
|
The datetime to split. |
required |
Returns:
| Type | Description |
|---|---|
|
A (offset, day start datetime) tuple, where offset is the number of |
|
|
seconds from the start of the day of the datetime, and the day start |
|
|
datetime has its time-of-day components zeroed. |
Source code in py_misc_utils/date_utils.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 | |
np_datetime_to_epoch(dt, dtype=np.float64)
¶
Converts numpy datetime64 values to epoch seconds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dt
|
The numpy datetime64 array to convert. |
required | |
dtype
|
The numpy dtype of the returned values - float64 by default. |
float64
|
Returns:
| Type | Description |
|---|---|
|
The epoch-seconds values. |
Source code in py_misc_utils/date_utils.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
align(dt, step, ceil=False)
¶
Aligns a datetime to a time step grid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dt
|
The datetime to align. |
required | |
step
|
The alignment step - in seconds, or as a timedelta. |
required | |
ceil
|
Whether to align to the next step boundary (ceiling) instead of the previous one (floor). |
False
|
Returns:
| Type | Description |
|---|---|
|
The aligned datetime, in the timezone of the input datetime. |
Source code in py_misc_utils/date_utils.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | |
at_time(dt, hour=0, minute=0, second=0, microsecond=0)
¶
Replaces the time-of-day components of a datetime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dt
|
The datetime to modify. |
required | |
hour
|
The hour of the day - midnight by default. |
0
|
|
minute
|
The minute. |
0
|
|
second
|
The second. |
0
|
|
microsecond
|
The microsecond. |
0
|
Returns:
| Type | Description |
|---|---|
|
The datetime with the replaced time-of-day components. |
Source code in py_misc_utils/date_utils.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 | |