From 65781dd1528097ce19bfcc1d8a1ea52b7c179892 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Fri, 21 Aug 2020 00:08:55 +0100 Subject: [PATCH] emfit: patch up timezone for correct local sleep time --- my/emfit/__init__.py | 26 +++++++++++++++++++++++--- tests/emfit.py | 1 - 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/my/emfit/__init__.py b/my/emfit/__init__.py index 25cf093..09fc61b 100755 --- a/my/emfit/__init__.py +++ b/my/emfit/__init__.py @@ -32,8 +32,28 @@ def dir_hash(path: Path): # TODO take __file__ into account somehow? @mcachew(cache_path=cache_dir() / 'emfit.cache', hashf=dir_hash, logger=dal.log) def datas(path: Path=config.export_path) -> Iterable[Res[Emfit]]: - # TODO FIXME excluded_sids - yield from dal.sleeps(config.export_path) + import dataclasses + + # data from emfit is coming in UTC. There is no way (I think?) to know the 'real' timezone, and local times matter more for sleep analysis + emfit_tz = config.timezone + + for x in dal.sleeps(config.export_path): + if isinstance(x, Exception): + yield x + else: + if x.sid in config.excluded_sids: + # TODO should be responsibility of export_path (similar to HPI?) + continue + # TODO maybe have a helper to 'patch up' all dattetimes in a namedtuple/dataclass? + # TODO do the same for jawbone data? + x = dataclasses.replace( + x, + start =x.start .astimezone(emfit_tz), + end =x.end .astimezone(emfit_tz), + sleep_start=x.sleep_start.astimezone(emfit_tz), + sleep_end =x.sleep_end .astimezone(emfit_tz), + ) + yield x # TODO should be used for jawbone data as well? @@ -49,7 +69,7 @@ def pre_dataframe() -> Iterable[Res[Emfit]]: g.clear() yield r else: - err = RuntimeError('Multiple sleeps per night, not supprted yet: {g}') + err = RuntimeError(f'Multiple sleeps per night, not supported yet: {g}') g.clear() yield err diff --git a/tests/emfit.py b/tests/emfit.py index 5add85c..cd88945 100644 --- a/tests/emfit.py +++ b/tests/emfit.py @@ -18,7 +18,6 @@ def test_tz() -> None: [s0109] = [s for s in ds if s.date.strftime('%Y%m%d') == '20190109'] assert s0109.end.strftime('%H:%M') == '06:42' - # TODO FIXME ugh, it's broken?... # summer time, so UTC+1 [s0411] = [s for s in ds if s.date.strftime('%Y%m%d') == '20190411'] assert s0411.end.strftime('%H:%M') == '09:30'