# HG changeset patch # User Paul Boddie # Date 1428437199 -7200 # Node ID 387f3fd3df267ae73b020b1216cf31689e36d30a # Parent 0d92bf9751a5e00a7dcd5540b9272cb527f2ea69 Permit the retrieval of UTC datetimes where dates are converted to datetimes. diff -r 0d92bf9751a5 -r 387f3fd3df26 imiptools/data.py --- a/imiptools/data.py Tue Apr 07 18:57:35 2015 +0200 +++ b/imiptools/data.py Tue Apr 07 22:06:39 2015 +0200 @@ -58,8 +58,8 @@ def get_value(self, name): return get_value(self.details, name) - def get_utc_datetime(self, name): - return get_utc_datetime(self.details, name) + def get_utc_datetime(self, name, date_tzid=None): + return get_utc_datetime(self.details, name, date_tzid) def get_date_values(self, name, tzid=None): items = get_date_value_items(self.details, name, tzid) @@ -300,13 +300,19 @@ else: return None -def get_utc_datetime(d, name): +def get_utc_datetime(d, name, date_tzid=None): + + """ + Return the value provided by 'd' for 'name' as a datetime in the UTC zone + or as a date, converting any date to a datetime if 'date_tzid' is specified. + """ + t = get_datetime_item(d, name) if not t: return None else: dt, attr = t - return to_utc_datetime(dt) + return to_utc_datetime(dt, date_tzid) def get_datetime_item(d, name): t = get_item(d, name) diff -r 0d92bf9751a5 -r 387f3fd3df26 imiptools/dates.py --- a/imiptools/dates.py Tue Apr 07 18:57:35 2015 +0200 +++ b/imiptools/dates.py Tue Apr 07 22:06:39 2015 +0200 @@ -58,14 +58,20 @@ match_datetime_icalendar = re.compile(datetime_icalendar_regexp_str, re.UNICODE).match match_duration_icalendar = re.compile(duration_icalendar_regexp_str, re.UNICODE).match -def to_utc_datetime(dt): +def to_utc_datetime(dt, date_tzid=None): - "Return a datetime corresponding to 'dt' in the UTC time zone." + """ + Return a datetime corresponding to 'dt' in the UTC time zone. If 'date_tzid' + is specified, dates are converted to datetimes using the time zone + information; otherwise, dates remain unconverted. + """ if not dt: return None elif isinstance(dt, datetime): return to_timezone(dt, "UTC") + elif date_tzid: + return to_timezone(to_datetime(dt, date_tzid), "UTC") else: return dt @@ -380,21 +386,17 @@ end point defined in UTC. """ - start = to_utc_datetime_only(start, tzid) - end = to_utc_datetime_only(end, tzid) + start = to_utc_datetime(start, tzid) + end = to_utc_datetime(end, tzid) return start, end -def to_utc_datetime_only(dt, tzid): +def to_recurrence_start(recurrenceid, tzid): """ - Return the datetime 'dt' as a point in time in the UTC time zone, given the - 'tzid' defined for the datetime. Where 'dt' is a date, the start of the - indicated day is returned, defined in UTC. + Return 'recurrenceid' in a form suitable for comparison with free/busy start + datetimes, using 'tzid' to convert recurrence identifiers that are dates. """ - if not isinstance(dt, datetime): - return to_timezone(get_start_of_day(dt, tzid), "UTC") - else: - return to_timezone(dt, "UTC") + return format_datetime(to_utc_datetime(get_datetime(recurrenceid), tzid)) # vim: tabstop=4 expandtab shiftwidth=4