Time Parser¶
IOTimeParser
¶
Collection of classmethods to parse a given date time string. The string can contain an absolute date and time, or a date and time that are relative to another time or even each other.
is_relative(input_str)
classmethod
¶
Check if the given string is a relative time (e.g. '+1d', '-8h', '-1w 08:00')
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_str
|
str
|
String to be checked if in relative time format |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the provided string is in relative time format |
Source code in trace/utilities/time_parser.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |
is_absolute(input_str)
classmethod
¶
Check if the given string is an absolute time (e.g. '2024-07-16 08:00')
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_str
|
str
|
String to be checked if in absolute time format |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the provided string is in absolute time format |
Source code in trace/utilities/time_parser.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
relative_to_delta(time)
classmethod
¶
Convert the given string containing a relative time into a datetime.timedelta
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time
|
str
|
String consisting of a time in a relative format (e.g. '-1d') |
required |
Returns:
| Type | Description |
|---|---|
timedelta
|
A duration expressing the difference between two datetimes |
Source code in trace/utilities/time_parser.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
set_time_on_datetime(dt, time_str)
classmethod
¶
Set an absolute time on a datetime object
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dt
|
datetime
|
The datetime to alter |
required |
time_str
|
str
|
The string containing the new time to set (e.g. '-1d 15:00') |
required |
Returns:
| Type | Description |
|---|---|
datetime
|
The datetime object with the same date and the new time |
Source code in trace/utilities/time_parser.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
parse_times(start_str, end_str)
classmethod
¶
Convert 2 strings containing a start and end date & time, return the values' datetime objects. The strings can be formatted as either absolute times or relative times. Both are needed as relative times may be relative to the other time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_str
|
str
|
The leftmost time the x-axis of the plot should show |
required |
end_str
|
str
|
The rigthmost time the x-axis of the plot should show, should be >start |
required |
Returns:
| Type | Description |
|---|---|
tuple[datetime, datetime]
|
The python datetime objects for the exact start and end datetimes referenced |
Raises:
| Type | Description |
|---|---|
ValueError
|
One of the given strings is in an incorrect format |
Source code in trace/utilities/time_parser.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |