diff options
-rw-r--r-- | glucometerutils/exceptions.py | 8 | ||||
-rwxr-xr-x | glucometerutils/glucometer.py | 6 | ||||
-rw-r--r-- | glucometerutils/support/freestyle.py | 8 |
3 files changed, 18 insertions, 4 deletions
diff --git a/glucometerutils/exceptions.py b/glucometerutils/exceptions.py index 415c97f..1cc8cfd 100644 --- a/glucometerutils/exceptions.py +++ b/glucometerutils/exceptions.py @@ -46,3 +46,11 @@ class InvalidGlucoseUnit(Error): def __init__(self, unit): super(InvalidGlucoseUnit, self).__init__( 'Invalid glucose unit received:\n%s' % unit) + + +class InvalidDateTime(Error): + """The device has an invalid date/time setting.""" + + def __init__(self): + super(InvalidDateTime, self).__init__( + 'Invalid date and time for device') diff --git a/glucometerutils/glucometer.py b/glucometerutils/glucometer.py index 485b32b..14999a7 100755 --- a/glucometerutils/glucometer.py +++ b/glucometerutils/glucometer.py @@ -86,9 +86,9 @@ def main(): if args.action == 'info': try: time_str = device.get_datetime() - except ValueError: - time_str = 'N/A' #handles value error exception for when time format found isn't expected - except NotImplementedError: + except exceptions.InvalidDateTime: + time_str = 'INVALID' + except NotImplementedError, ValueError: # Catch any leftover ValueError time_str = 'N/A' print("{device_info}Time: {time}".format( device_info=str(device_info), time=time_str)) diff --git a/glucometerutils/support/freestyle.py b/glucometerutils/support/freestyle.py index d18a81f..475d6f5 100644 --- a/glucometerutils/support/freestyle.py +++ b/glucometerutils/support/freestyle.py @@ -200,7 +200,13 @@ class FreeStyleHidDevice(hiddevice.HidDevice): month, day, year = (int(x) for x in date.split(',')) hour, minute = (int(x) for x in time.split(',')) - return datetime.datetime(year + 2000, month, day, hour, minute) + # At least Precision Neo devices can have an invalid date (bad RTC?), + # and report 255 for each field, which is not valid for + # datetime.datetime(). + try: + return datetime.datetime(year + 2000, month, day, hour, minute) + except ValueError: + raise exceptions.InvalidDateTime() def set_datetime(self, date=datetime.datetime.now()): # type: (datetime.datetime) -> datetime.datetime |