From 35b91a6fa27069f7e11722cede5d797bbd22acc8 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Tue, 6 Oct 2020 00:39:47 +0100 Subject: [PATCH] timezone provider: add stat(), use cachew (daily resolution) --- my/time/tz/via_location.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/my/time/tz/via_location.py b/my/time/tz/via_location.py index 09c9528..c38405e 100644 --- a/my/time/tz/via_location.py +++ b/my/time/tz/via_location.py @@ -17,7 +17,8 @@ from typing import Dict, Iterator, List, NamedTuple, Optional, Tuple from more_itertools import seekable import pytz -from ...core.common import LazyLogger +from ...core.common import LazyLogger, mcachew +from ...core.cachew import cache_dir from ...location.google import locations @@ -75,6 +76,7 @@ def most_common(l): return res +@mcachew(cache_path=cache_dir()) def _iter_tzs() -> Iterator[DayWithZone]: for d, gr in groupby(_iter_local_dates(), key=lambda p: p.day): logger.info('processed %s', d) @@ -137,4 +139,17 @@ def localize(dt: datetime) -> datetime: return tz.localize(dt) -# TODO: cache stuff +from ...core import stat, Stats +def stats() -> Stats: + from ...core.common import fromisoformat + # TODO not sure what would be a good stat() for this module... + # might be nice to print some actual timezones? + # there aren't really any great iterables to expose + def localized_years(): + last = datetime.now().year + 2 + # note: deliberately take + 2 years, so the iterator exhausts. otherwise stuff might never get cached + # need to think about it... + for Y in range(1990, last): + dt = fromisoformat(f'{Y}-01-01 01:01:01') + yield localize(dt) + return stat(localized_years)