All, thank you again for your help and insights, thought I would share my final solution.
(still need to revisit for some exception handling)
@jshaffstall the JS works for me, I added . . .
from anvil.js.window import Intl
time_zone = Intl.DateTimeFormat().resolvedOptions().timeZone
print(f'JS TIMEZONE IS: {time_zone}')
. . . and in the console!
JS TIMEZONE IS: America/New_York
Which I can then pass to my function as the override.
Background on the function: I am consuming some data from multiple data sources, all return data as a list of dicts with all dates as string values in UTC. Wanted a single function that I could pass the response from any of these sources and provide dates in any flavor for other purposes.
Tried to be a little verbose in the doc string so other newbies like me might be able repurpose.
def dates_process(lst_of_dic, lst_date_keys, str_strip, str_pretty="%b %d %Y %I:%M %p", timezone_override=None):
"""
Create date objects and pretty strings for specified keys in passed dic.
For each dict in list and each date_key in dict. Convert the field to datetime object with tz=utc.
Then convert the utc to specified local timezone(ins) and America/New_York(est)
Then create pretty strings of all three.
The original data is returned with six additional key/values appended to each dict for each field.
Passed data needs a key "timezone" with string recognized by pytz or must be passed the override parameter.
https://gist.github.com/heyalexej/8bf688fd67d7199be4a1682b3eec7568
i.e. "America/New_York
Creates new key/values for:
[orig]_utc_dtm as aware datetime object in utc
[orig]_ins_dtm as aware datetime object in instructors default timezone
[orig]_est_dtm as aware datetime object in America/New_York timezone
[orig]_utc_str as string in utc
[orig]_ins_str as string in instructors default timezone
[orig]_est_str as string in America/New_York timezone
:param lst_of_dic: List of dicts as data rows
:type lst_of_dic: list
:param lst_date_keys: List of date dict keys to process exp. ['start_time', 'created_at']
:type lst_date_keys: list
:param str_strip: String format of the date string to convert to datetime
For each data source the original date string format of the time may be slightly different
and str_strip will depend on source data.
exp. "%Y-%m-%dT%H:%M:%S%fZ"
:type str_strip: str
:param str_pretty: String format for pretty presentation of date. A default is defined for consistent
presentation across the app but can be overridden
exp. "%b %d %Y %I:%M %p"
:type str_pretty: str
:param timezone_override:
:type timezone_override: str
:return: lst of dicts appended with new dtm fields
"""
# lst_date_keys = ['start_time', 'created_at']
tz_utc = pytz.timezone("UTC")
tz_est = pytz.timezone("America/New_York")
for m in lst_of_dic:
# Use either passed time zone override or read from 'timezone' key in dic
if timezone_override is not None:
tz_inst = timezone_override
else:
tz_inst = pytz.timezone(m['timezone'])
# for each date key passed, apply date functions derriving new key names from original
for f in lst_date_keys:
try:
# Create date objects
m[f'{f}_utc_dtm'] = dt.strptime(m[f], str_strip).replace(tzinfo=tz_utc)
m[f'{f}_ins_dtm'] = m[f'{f}_utc_dtm'].astimezone(tz_inst)
m[f'{f}_est_dtm'] = m[f'{f}_utc_dtm'].astimezone(tz_est)
# Create pretty strings
m[f'{f}_utc_str'] = m[f'{f}_utc_dtm'].strftime(str_pretty)
m[f'{f}_ins_str'] = m[f'{f}_ins_dtm'].strftime(str_pretty)
m[f'{f}_est_str'] = m[f'{f}_est_dtm'].strftime(str_pretty)
except:
continue
return lst_of_dic