From e1fcc2546b18791483787f0e4c3361945637ac23 Mon Sep 17 00:00:00 2001 From: Ben West Date: Wed, 21 Oct 2015 12:51:47 -0700 Subject: [PATCH 1/2] make much more flexible * Able to express times in local zone. * Able to shift time to another zone correctly. * Can select and transform multiple date fields at the same time. * Introduces/demos new feature of openaps to ease how uses store and retrieve their configuration from config. * Can save timezone (or None), as well as list of date fields. --- openapscontrib/timezones/__init__.py | 33 +++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/openapscontrib/timezones/__init__.py b/openapscontrib/timezones/__init__.py index 8249cb2..0eb60fc 100644 --- a/openapscontrib/timezones/__init__.py +++ b/openapscontrib/timezones/__init__.py @@ -10,6 +10,7 @@ import argparse from dateutil.tz import gettz from dateutil.parser import parse +from datetime import datetime def set_config (args, device): return device @@ -22,12 +23,29 @@ def display_device (device): class ConvertInput (Use): FIELDNAME = ['date'] + def to_ini (self, args): + params = self.get_params(args) + now = datetime.now( ).replace(tzinfo=args.timezone) + params['timezone'] = now.tzname( ) + params['date'] = ' '.join(args.date) + return params + def from_ini (self, fields): + fields['date'] = fields['date'].split(' ') + zone = fields.get('timezone', None) + if zone in ['None', None]: + zone = gettz( ) + else: + zone = gettz(zone) + fields['timezone'] = zone + fields['astimezone'] = fields.get('astimezone', False) is 'True' + return fields def get_params (self, args): - return dict(input=args.input, timezone=args.timezone, adjust=args.adjust, field=args.field) + return dict(input=args.input, timezone=args.timezone, adjust=args.adjust, date=args.date, astimezone=args.astimezone) def configure_app (self, app, parser): parser.add_argument('--timezone','-z', default=gettz( ), type=gettz) parser.add_argument('--adjust','-a', default='missing', choices=['missing', 'replace']) - parser.add_argument('--field','-f', action='append', default=self.FIELDNAME) + parser.add_argument('--date','-d', action='append', default=self.FIELDNAME) + parser.add_argument('--astimezone','-r', action='store_true', default=False) parser.add_argument('input', default='-') def get_program (self, args): params = self.get_params(args) @@ -35,13 +53,16 @@ def get_program (self, args): return program def set_converter (self, args): params = self.get_params(args) - self.FIELDNAME = params.get('field') + self.FIELDNAME = params.get('date') self.adjust = params.get('adjust') self.timezone = params.get('timezone') + self.astimezone = params.get('astimezone') def rezone (self, dt): if (self.adjust == 'missing' and dt.tzinfo == None) or self.adjust == 'replace': - return dt.replace(tzinfo=self.timezone) + dt = dt.replace(tzinfo=self.timezone) # .astimezone(self.timezone) + if self.astimezone: + dt = dt.astimezone(self.timezone) return dt def range (self, program): return [ program ] @@ -85,9 +106,9 @@ def range (self, program): @use( ) class rezone (glucose): """ - Manage timezones on glucose times. + Manage how timezones are expressed in data. """ - FIELDNAME = ['timestamp', 'dateString', 'start_at', 'end_at', 'created_at'] + FIELDNAME = ['timestamp', 'dateString', 'start_at', 'end_at', 'created_at' ] def get_uses (device, config): From 03ac99224c041f45f451630a199a1d3eb8bd2fc7 Mon Sep 17 00:00:00 2001 From: Ben West Date: Wed, 28 Oct 2015 16:04:24 -0700 Subject: [PATCH 2/2] only add date FIELDNAMES to config if there is one This fixes a bug which now allows clock's use to be registered as a report properly. --- openapscontrib/timezones/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/openapscontrib/timezones/__init__.py b/openapscontrib/timezones/__init__.py index 0eb60fc..21f153f 100644 --- a/openapscontrib/timezones/__init__.py +++ b/openapscontrib/timezones/__init__.py @@ -27,7 +27,8 @@ def to_ini (self, args): params = self.get_params(args) now = datetime.now( ).replace(tzinfo=args.timezone) params['timezone'] = now.tzname( ) - params['date'] = ' '.join(args.date) + if args.date: + params['date'] = ' '.join(args.date) return params def from_ini (self, fields): fields['date'] = fields['date'].split(' ')