# HG changeset patch # User Paul Boddie # Date 1439678323 -7200 # Node ID 414cc471e70e49ca65d9e503930d9672d937a114 # Parent 4bf280ba1c3633853fbd012a177fd804b44ece1a Fixed datetime correction for units without predetermined values. Moved correction functions and error class into the dates module. diff -r 4bf280ba1c36 -r 414cc471e70e imiptools/client.py --- a/imiptools/client.py Sun Aug 16 00:36:05 2015 +0200 +++ b/imiptools/client.py Sun Aug 16 00:38:43 2015 +0200 @@ -23,7 +23,7 @@ from imiptools.data import Object, get_address, get_uri, get_window_end, \ is_new_object, make_freebusy, to_part, \ uri_dict, uri_items, uri_values -from imiptools.dates import format_datetime, get_default_timezone, \ +from imiptools.dates import check_resolution, format_datetime, get_default_timezone, \ get_timestamp, to_timezone from imiptools.period import can_schedule, remove_period, \ remove_additional_periods, remove_affected_period, \ @@ -412,9 +412,6 @@ # Constraint application on event periods. - class ValidityError(Exception): - pass - def check_object(self): "Check the object against any scheduling constraints." @@ -423,37 +420,24 @@ if not resolution: return None - tzid = self.get_tzid() invalid = [] - for period in self.obj.get_periods(tzid): + for period in self.obj.get_periods(self.get_tzid()): start = period.get_start() end = period.get_end() - start_errors = self.check_resolution(start, resolution) - end_errors = self.check_resolution(end, resolution) + start_errors = check_resolution(start, resolution) + end_errors = check_resolution(end, resolution) if start_errors or end_errors: invalid.append((period.origin, start_errors, end_errors)) return invalid - def check_resolution(self, dt, resolution): - - "Check the datetime 'dt' against the 'resolution' list." - - if not isinstance(dt, datetime): - raise ValidityError + def correct_object(self): - hours, minutes, seconds = resolution - errors = [] + "Correct the object according to any scheduling constraints." - if hours and dt.hour not in hours: - errors.append("hour") - if minutes and dt.minute not in minutes: - errors.append("minute") - if seconds and dt.second not in seconds: - errors.append("second") - - return errors + resolution = self.get_scheduling_resolution() + return resolution and self.obj.correct_object(self.get_tzid(), resolution) # Object retrieval. diff -r 4bf280ba1c36 -r 414cc471e70e imiptools/dates.py --- a/imiptools/dates.py Sun Aug 16 00:36:05 2015 +0200 +++ b/imiptools/dates.py Sun Aug 16 00:38:43 2015 +0200 @@ -473,6 +473,28 @@ # Time corrections. +class ValidityError(Exception): + pass + +def check_resolution(dt, resolution): + + "Check the datetime 'dt' against the 'resolution' list." + + if not isinstance(dt, datetime): + raise ValidityError + + hours, minutes, seconds = resolution + errors = [] + + if hours and dt.hour not in hours: + errors.append("hour") + if minutes and dt.minute not in minutes: + errors.append("minute") + if seconds and dt.second not in seconds: + errors.append("second") + + return errors + def correct_datetime(dt, resolution): "Correct 'dt' using the given 'resolution' details." @@ -528,12 +550,13 @@ else: carry = 0 - i = bisect_left(values, v) - if i < len(values): - v = values[i] - else: - v = values[0] - carry = 1 + if values: + i = bisect_left(values, v) + if i < len(values): + v = values[i] + else: + v = values[0] + carry = 1 corrected.append(v)