-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move iso_range_to_ts_range to datetime_utils.py
This function is used by db_utils but might be better categorized in datetime_utils. Previously called`get_ts_range`, its new name is `iso_range_to_ts_range`, since I thought that was more descriptive. -- Also, I noticed `arrow` was imported in db_utils twice, so i fixed that while I was here
- Loading branch information
Showing
2 changed files
with
29 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,32 @@ | ||
import arrow | ||
|
||
MAX_EPOCH_TIME = 2 ** 31 - 1 | ||
|
||
|
||
def iso_range_to_ts_range(start_date: str, end_date: str, tz: str): | ||
""" | ||
Returns a tuple of (start_ts, end_ts) as epoch timestamps, given start_date and end_date in | ||
ISO format and the timezone mode in which the dates should be resolved to timestamps ('utc' or 'local') | ||
""" | ||
start_ts, end_ts = None, MAX_EPOCH_TIME | ||
if start_date is not None: | ||
if tz == 'utc': | ||
start_ts = arrow.get(start_date).timestamp() | ||
elif tz == 'local': | ||
start_ts = arrow.get(start_date, tzinfo='local').timestamp() | ||
if end_date is not None: | ||
if tz == 'utc': | ||
end_ts = arrow.get(end_date).replace( | ||
hour=23, minute=59, second=59).timestamp() | ||
elif tz == 'local': | ||
end_ts = arrow.get(end_date, tzinfo='local').replace( | ||
hour=23, minute=59, second=59).timestamp() | ||
return (start_ts, end_ts) | ||
|
||
|
||
def iso_to_date_only(*iso_strs: str): | ||
""" | ||
For each ISO date string in the input, returns only the date part in the format 'YYYY-MM-DD' | ||
e.g. '2021-01-01T00:00:00.000Z' -> '2021-01-01' | ||
""" | ||
return [iso_str[:10] if iso_str else None for iso_str in iso_strs] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters