1.1 --- a/imiptools/client.py Sun Aug 16 00:36:05 2015 +0200
1.2 +++ b/imiptools/client.py Sun Aug 16 00:38:43 2015 +0200
1.3 @@ -23,7 +23,7 @@
1.4 from imiptools.data import Object, get_address, get_uri, get_window_end, \
1.5 is_new_object, make_freebusy, to_part, \
1.6 uri_dict, uri_items, uri_values
1.7 -from imiptools.dates import format_datetime, get_default_timezone, \
1.8 +from imiptools.dates import check_resolution, format_datetime, get_default_timezone, \
1.9 get_timestamp, to_timezone
1.10 from imiptools.period import can_schedule, remove_period, \
1.11 remove_additional_periods, remove_affected_period, \
1.12 @@ -412,9 +412,6 @@
1.13
1.14 # Constraint application on event periods.
1.15
1.16 - class ValidityError(Exception):
1.17 - pass
1.18 -
1.19 def check_object(self):
1.20
1.21 "Check the object against any scheduling constraints."
1.22 @@ -423,37 +420,24 @@
1.23 if not resolution:
1.24 return None
1.25
1.26 - tzid = self.get_tzid()
1.27 invalid = []
1.28
1.29 - for period in self.obj.get_periods(tzid):
1.30 + for period in self.obj.get_periods(self.get_tzid()):
1.31 start = period.get_start()
1.32 end = period.get_end()
1.33 - start_errors = self.check_resolution(start, resolution)
1.34 - end_errors = self.check_resolution(end, resolution)
1.35 + start_errors = check_resolution(start, resolution)
1.36 + end_errors = check_resolution(end, resolution)
1.37 if start_errors or end_errors:
1.38 invalid.append((period.origin, start_errors, end_errors))
1.39
1.40 return invalid
1.41
1.42 - def check_resolution(self, dt, resolution):
1.43 -
1.44 - "Check the datetime 'dt' against the 'resolution' list."
1.45 -
1.46 - if not isinstance(dt, datetime):
1.47 - raise ValidityError
1.48 + def correct_object(self):
1.49
1.50 - hours, minutes, seconds = resolution
1.51 - errors = []
1.52 + "Correct the object according to any scheduling constraints."
1.53
1.54 - if hours and dt.hour not in hours:
1.55 - errors.append("hour")
1.56 - if minutes and dt.minute not in minutes:
1.57 - errors.append("minute")
1.58 - if seconds and dt.second not in seconds:
1.59 - errors.append("second")
1.60 -
1.61 - return errors
1.62 + resolution = self.get_scheduling_resolution()
1.63 + return resolution and self.obj.correct_object(self.get_tzid(), resolution)
1.64
1.65 # Object retrieval.
1.66
2.1 --- a/imiptools/dates.py Sun Aug 16 00:36:05 2015 +0200
2.2 +++ b/imiptools/dates.py Sun Aug 16 00:38:43 2015 +0200
2.3 @@ -473,6 +473,28 @@
2.4
2.5 # Time corrections.
2.6
2.7 +class ValidityError(Exception):
2.8 + pass
2.9 +
2.10 +def check_resolution(dt, resolution):
2.11 +
2.12 + "Check the datetime 'dt' against the 'resolution' list."
2.13 +
2.14 + if not isinstance(dt, datetime):
2.15 + raise ValidityError
2.16 +
2.17 + hours, minutes, seconds = resolution
2.18 + errors = []
2.19 +
2.20 + if hours and dt.hour not in hours:
2.21 + errors.append("hour")
2.22 + if minutes and dt.minute not in minutes:
2.23 + errors.append("minute")
2.24 + if seconds and dt.second not in seconds:
2.25 + errors.append("second")
2.26 +
2.27 + return errors
2.28 +
2.29 def correct_datetime(dt, resolution):
2.30
2.31 "Correct 'dt' using the given 'resolution' details."
2.32 @@ -528,12 +550,13 @@
2.33 else:
2.34 carry = 0
2.35
2.36 - i = bisect_left(values, v)
2.37 - if i < len(values):
2.38 - v = values[i]
2.39 - else:
2.40 - v = values[0]
2.41 - carry = 1
2.42 + if values:
2.43 + i = bisect_left(values, v)
2.44 + if i < len(values):
2.45 + v = values[i]
2.46 + else:
2.47 + v = values[0]
2.48 + carry = 1
2.49
2.50 corrected.append(v)
2.51