rename policy -> tzpolicy

This commit is contained in:
Sean Breckenridge 2022-04-26 12:20:46 -07:00
parent 62dd6712f3
commit bec52724e4

View file

@ -10,24 +10,27 @@ Depending on the specific data provider and your level of paranoia you might exp
- it's safer when either all of your objects are tz aware or all are tz unware, not a mixture - it's safer when either all of your objects are tz aware or all are tz unware, not a mixture
- you might trust your original timezone, or it might just be UTC, and you want to use something more reasonable - you might trust your original timezone, or it might just be UTC, and you want to use something more reasonable
''' '''
Policy = Literal[ TzPolicy = Literal[
'keep' , # if datetime is tz aware, just preserve it 'keep' , # if datetime is tz aware, just preserve it
'convert', # if datetime is tz aware, convert to provider's tz 'convert', # if datetime is tz aware, convert to provider's tz
'throw' , # if datetime is tz aware, throw exception 'throw' , # if datetime is tz aware, throw exception
# todo 'warn'? not sure if very useful # todo 'warn'? not sure if very useful
] ]
def default_policy() -> Policy: # backwards compatibility
Policy = TzPolicy
def default_policy() -> TzPolicy:
try: try:
from my.config import time as user_config from my.config import time as user_config
return cast(Policy, user_config.tz.policy) return cast(TzPolicy, user_config.tz.policy)
except Exception as e: except Exception as e:
# todo meh.. need to think how to do this more carefully # todo meh.. need to think how to do this more carefully
# rationale: do not mess with user's data unless they want # rationale: do not mess with user's data unless they want
return 'keep' return 'keep'
def localize_with_policy(lfun: Callable[[datetime], tzdatetime], dt: datetime, policy: Policy=default_policy()) -> tzdatetime: def localize_with_policy(lfun: Callable[[datetime], tzdatetime], dt: datetime, policy: TzPolicy=default_policy()) -> tzdatetime:
tz = dt.tzinfo tz = dt.tzinfo
if tz is None: if tz is None:
return lfun(dt) return lfun(dt)