paul@152 | 1 | #!/usr/bin/env python |
paul@152 | 2 | |
paul@152 | 3 | """ |
paul@152 | 4 | Date processing functions. |
paul@152 | 5 | |
paul@152 | 6 | Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> |
paul@152 | 7 | |
paul@152 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@152 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@152 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@152 | 11 | version. |
paul@152 | 12 | |
paul@152 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@152 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@152 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@152 | 16 | details. |
paul@152 | 17 | |
paul@152 | 18 | You should have received a copy of the GNU General Public License along with |
paul@152 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@152 | 20 | """ |
paul@152 | 21 | |
paul@657 | 22 | from bisect import bisect_left |
paul@195 | 23 | from datetime import date, datetime, timedelta |
paul@291 | 24 | from os.path import exists |
paul@152 | 25 | from pytz import timezone, UnknownTimeZoneError |
paul@152 | 26 | import re |
paul@152 | 27 | |
paul@152 | 28 | # iCalendar date and datetime parsing (from DateSupport in MoinSupport). |
paul@152 | 29 | |
paul@388 | 30 | _date_icalendar_regexp_str = ur'(?P<year>[0-9]{4})(?P<month>[0-9]{2})(?P<day>[0-9]{2})' |
paul@388 | 31 | date_icalendar_regexp_str = _date_icalendar_regexp_str + '$' |
paul@388 | 32 | |
paul@388 | 33 | datetime_icalendar_regexp_str = _date_icalendar_regexp_str + \ |
paul@152 | 34 | ur'(?:' \ |
paul@152 | 35 | ur'T(?P<hour>[0-2][0-9])(?P<minute>[0-5][0-9])(?P<second>[0-6][0-9])' \ |
paul@152 | 36 | ur'(?P<utc>Z)?' \ |
paul@388 | 37 | ur')?$' |
paul@152 | 38 | |
paul@388 | 39 | _duration_time_icalendar_regexp_str = \ |
paul@387 | 40 | ur'T' \ |
paul@387 | 41 | ur'(?:' \ |
paul@387 | 42 | ur'([0-9]+H)(?:([0-9]+M)([0-9]+S)?)?' \ |
paul@387 | 43 | ur'|' \ |
paul@387 | 44 | ur'([0-9]+M)([0-9]+S)?' \ |
paul@387 | 45 | ur'|' \ |
paul@387 | 46 | ur'([0-9]+S)' \ |
paul@387 | 47 | ur')' |
paul@387 | 48 | |
paul@387 | 49 | duration_icalendar_regexp_str = ur'P' \ |
paul@387 | 50 | ur'(?:' \ |
paul@387 | 51 | ur'([0-9]+W)' \ |
paul@387 | 52 | ur'|' \ |
paul@387 | 53 | ur'(?:%s)' \ |
paul@387 | 54 | ur'|' \ |
paul@387 | 55 | ur'([0-9]+D)(?:%s)?' \ |
paul@388 | 56 | ur')$' % (_duration_time_icalendar_regexp_str, _duration_time_icalendar_regexp_str) |
paul@387 | 57 | |
paul@152 | 58 | match_date_icalendar = re.compile(date_icalendar_regexp_str, re.UNICODE).match |
paul@152 | 59 | match_datetime_icalendar = re.compile(datetime_icalendar_regexp_str, re.UNICODE).match |
paul@387 | 60 | match_duration_icalendar = re.compile(duration_icalendar_regexp_str, re.UNICODE).match |
paul@152 | 61 | |
paul@551 | 62 | # Datetime formatting. |
paul@152 | 63 | |
paul@152 | 64 | def format_datetime(dt): |
paul@247 | 65 | |
paul@247 | 66 | "Format 'dt' as an iCalendar-compatible string." |
paul@247 | 67 | |
paul@152 | 68 | if not dt: |
paul@152 | 69 | return None |
paul@152 | 70 | elif isinstance(dt, datetime): |
paul@152 | 71 | if dt.tzname() == "UTC": |
paul@152 | 72 | return dt.strftime("%Y%m%dT%H%M%SZ") |
paul@152 | 73 | else: |
paul@152 | 74 | return dt.strftime("%Y%m%dT%H%M%S") |
paul@152 | 75 | else: |
paul@152 | 76 | return dt.strftime("%Y%m%d") |
paul@152 | 77 | |
paul@285 | 78 | def format_time(dt): |
paul@285 | 79 | |
paul@285 | 80 | "Format the time portion of 'dt' as an iCalendar-compatible string." |
paul@285 | 81 | |
paul@285 | 82 | if not dt: |
paul@285 | 83 | return None |
paul@285 | 84 | elif isinstance(dt, datetime): |
paul@285 | 85 | if dt.tzname() == "UTC": |
paul@285 | 86 | return dt.strftime("%H%M%SZ") |
paul@285 | 87 | else: |
paul@285 | 88 | return dt.strftime("%H%M%S") |
paul@285 | 89 | else: |
paul@285 | 90 | return None |
paul@285 | 91 | |
paul@551 | 92 | # Parsing of datetime and related information. |
paul@239 | 93 | |
paul@152 | 94 | def get_datetime(value, attr=None): |
paul@152 | 95 | |
paul@152 | 96 | """ |
paul@152 | 97 | Return a datetime object from the given 'value' in iCalendar format, using |
paul@152 | 98 | the 'attr' mapping (if specified) to control the conversion. |
paul@152 | 99 | """ |
paul@152 | 100 | |
paul@295 | 101 | if not value: |
paul@295 | 102 | return None |
paul@295 | 103 | |
paul@285 | 104 | if len(value) > 9 and (not attr or attr.get("VALUE") in (None, "DATE-TIME")): |
paul@152 | 105 | m = match_datetime_icalendar(value) |
paul@152 | 106 | if m: |
paul@232 | 107 | year, month, day, hour, minute, second = map(m.group, [ |
paul@232 | 108 | "year", "month", "day", "hour", "minute", "second" |
paul@232 | 109 | ]) |
paul@152 | 110 | |
paul@232 | 111 | if hour and minute and second: |
paul@232 | 112 | dt = datetime( |
paul@232 | 113 | int(year), int(month), int(day), int(hour), int(minute), int(second) |
paul@232 | 114 | ) |
paul@152 | 115 | |
paul@232 | 116 | # Impose the indicated timezone. |
paul@232 | 117 | # NOTE: This needs an ambiguity policy for DST changes. |
paul@232 | 118 | |
paul@232 | 119 | return to_timezone(dt, m.group("utc") and "UTC" or attr and attr.get("TZID") or None) |
paul@152 | 120 | |
paul@285 | 121 | return None |
paul@285 | 122 | |
paul@239 | 123 | # Permit dates even if the VALUE is not set to DATE. |
paul@239 | 124 | |
paul@239 | 125 | if not attr or attr.get("VALUE") in (None, "DATE"): |
paul@152 | 126 | m = match_date_icalendar(value) |
paul@152 | 127 | if m: |
paul@232 | 128 | year, month, day = map(m.group, ["year", "month", "day"]) |
paul@232 | 129 | return date(int(year), int(month), int(day)) |
paul@232 | 130 | |
paul@152 | 131 | return None |
paul@152 | 132 | |
paul@551 | 133 | def get_duration(value): |
paul@551 | 134 | |
paul@551 | 135 | "Return a duration for the given 'value'." |
paul@551 | 136 | |
paul@551 | 137 | if not value: |
paul@551 | 138 | return None |
paul@551 | 139 | |
paul@551 | 140 | m = match_duration_icalendar(value) |
paul@551 | 141 | if m: |
paul@551 | 142 | weeks, days, hours, minutes, seconds = 0, 0, 0, 0, 0 |
paul@551 | 143 | for s in m.groups(): |
paul@551 | 144 | if not s: continue |
paul@551 | 145 | if s[-1] == "W": weeks += int(s[:-1]) |
paul@551 | 146 | elif s[-1] == "D": days += int(s[:-1]) |
paul@551 | 147 | elif s[-1] == "H": hours += int(s[:-1]) |
paul@551 | 148 | elif s[-1] == "M": minutes += int(s[:-1]) |
paul@551 | 149 | elif s[-1] == "S": seconds += int(s[:-1]) |
paul@551 | 150 | return timedelta( |
paul@551 | 151 | int(weeks) * 7 + int(days), |
paul@551 | 152 | (int(hours) * 60 + int(minutes)) * 60 + int(seconds) |
paul@551 | 153 | ) |
paul@551 | 154 | else: |
paul@551 | 155 | return None |
paul@551 | 156 | |
paul@387 | 157 | def get_period(value, attr=None): |
paul@387 | 158 | |
paul@387 | 159 | """ |
paul@387 | 160 | Return a tuple of the form (start, end) for the given 'value' in iCalendar |
paul@387 | 161 | format, using the 'attr' mapping (if specified) to control the conversion. |
paul@387 | 162 | """ |
paul@387 | 163 | |
paul@630 | 164 | if not value or attr and attr.get("VALUE") and attr.get("VALUE") != "PERIOD": |
paul@387 | 165 | return None |
paul@387 | 166 | |
paul@387 | 167 | t = value.split("/") |
paul@387 | 168 | if len(t) != 2: |
paul@387 | 169 | return None |
paul@387 | 170 | |
paul@388 | 171 | dtattr = {} |
paul@388 | 172 | if attr: |
paul@388 | 173 | dtattr.update(attr) |
paul@388 | 174 | if dtattr.has_key("VALUE"): |
paul@388 | 175 | del dtattr["VALUE"] |
paul@388 | 176 | |
paul@388 | 177 | start = get_datetime(t[0], dtattr) |
paul@387 | 178 | if t[1].startswith("P"): |
paul@387 | 179 | end = start + get_duration(t[1]) |
paul@387 | 180 | else: |
paul@388 | 181 | end = get_datetime(t[1], dtattr) |
paul@387 | 182 | |
paul@387 | 183 | return start, end |
paul@387 | 184 | |
paul@551 | 185 | # Time zone conversions and retrieval. |
paul@551 | 186 | |
paul@551 | 187 | def ends_on_same_day(dt, end, tzid): |
paul@387 | 188 | |
paul@551 | 189 | """ |
paul@551 | 190 | Return whether 'dt' ends on the same day as 'end', testing the date |
paul@551 | 191 | components of 'dt' and 'end' against each other, but also testing whether |
paul@551 | 192 | 'end' is the actual end of the day in which 'dt' is positioned. |
paul@387 | 193 | |
paul@551 | 194 | Since time zone transitions may occur within a day, 'tzid' is required to |
paul@551 | 195 | determine the end of the day in which 'dt' is positioned, using the zone |
paul@551 | 196 | appropriate at that point in time, not necessarily the zone applying to |
paul@551 | 197 | 'dt'. |
paul@551 | 198 | """ |
paul@387 | 199 | |
paul@551 | 200 | return ( |
paul@551 | 201 | to_timezone(dt, tzid).date() == to_timezone(end, tzid).date() or |
paul@551 | 202 | end == get_end_of_day(dt, tzid) |
paul@551 | 203 | ) |
paul@551 | 204 | |
paul@551 | 205 | def get_default_timezone(): |
paul@551 | 206 | |
paul@551 | 207 | "Return the system time regime." |
paul@551 | 208 | |
paul@551 | 209 | filename = "/etc/timezone" |
paul@551 | 210 | |
paul@551 | 211 | if exists(filename): |
paul@551 | 212 | f = open(filename) |
paul@551 | 213 | try: |
paul@551 | 214 | return f.read().strip() |
paul@551 | 215 | finally: |
paul@551 | 216 | f.close() |
paul@387 | 217 | else: |
paul@387 | 218 | return None |
paul@387 | 219 | |
paul@551 | 220 | def get_end_of_day(dt, tzid): |
paul@431 | 221 | |
paul@431 | 222 | """ |
paul@551 | 223 | Get the end of the day in which 'dt' is positioned, using the given 'tzid' |
paul@551 | 224 | to obtain a datetime in the appropriate time zone. Where time zone |
paul@551 | 225 | transitions occur within a day, the zone of 'dt' may not be the eventual |
paul@551 | 226 | zone of the returned object. |
paul@431 | 227 | """ |
paul@431 | 228 | |
paul@551 | 229 | return get_start_of_day(dt + timedelta(1), tzid) |
paul@431 | 230 | |
paul@244 | 231 | def get_start_of_day(dt, tzid): |
paul@245 | 232 | |
paul@245 | 233 | """ |
paul@245 | 234 | Get the start of the day in which 'dt' is positioned, using the given 'tzid' |
paul@245 | 235 | to obtain a datetime in the appropriate time zone. Where time zone |
paul@245 | 236 | transitions occur within a day, the zone of 'dt' may not be the eventual |
paul@245 | 237 | zone of the returned object. |
paul@245 | 238 | """ |
paul@245 | 239 | |
paul@244 | 240 | start = datetime(dt.year, dt.month, dt.day, 0, 0) |
paul@244 | 241 | return to_timezone(start, tzid) |
paul@152 | 242 | |
paul@244 | 243 | def get_start_of_next_day(dt, tzid): |
paul@245 | 244 | |
paul@245 | 245 | """ |
paul@245 | 246 | Get the start of the day after the day in which 'dt' is positioned. This |
paul@245 | 247 | function is intended to extend either dates or datetimes to the end of a |
paul@245 | 248 | day for the purpose of generating a missing end date or datetime for an |
paul@245 | 249 | event. |
paul@245 | 250 | |
paul@245 | 251 | If 'dt' is a date and not a datetime, a plain date object for the next day |
paul@245 | 252 | will be returned. |
paul@245 | 253 | |
paul@245 | 254 | If 'dt' is a datetime, the given 'tzid' is used to obtain a datetime in the |
paul@245 | 255 | appropriate time zone. Where time zone transitions occur within a day, the |
paul@245 | 256 | zone of 'dt' may not be the eventual zone of the returned object. |
paul@245 | 257 | """ |
paul@245 | 258 | |
paul@239 | 259 | if isinstance(dt, datetime): |
paul@239 | 260 | return get_end_of_day(dt, tzid) |
paul@239 | 261 | else: |
paul@239 | 262 | return dt + timedelta(1) |
paul@239 | 263 | |
paul@616 | 264 | def get_datetime_tzid(dt): |
paul@616 | 265 | |
paul@616 | 266 | "Return the time zone identifier from 'dt' or None if unknown." |
paul@616 | 267 | |
paul@616 | 268 | if not isinstance(dt, datetime): |
paul@616 | 269 | return None |
paul@616 | 270 | elif dt.tzname() == "UTC": |
paul@616 | 271 | return "UTC" |
paul@616 | 272 | elif dt.tzinfo and hasattr(dt.tzinfo, "zone"): |
paul@616 | 273 | return dt.tzinfo.zone |
paul@616 | 274 | else: |
paul@616 | 275 | return None |
paul@616 | 276 | |
paul@627 | 277 | def get_period_tzid(start, end): |
paul@627 | 278 | |
paul@627 | 279 | "Return the time zone identifier for 'start' and 'end' or None if unknown." |
paul@627 | 280 | |
paul@627 | 281 | if isinstance(start, datetime) or isinstance(end, datetime): |
paul@627 | 282 | return get_datetime_tzid(start) or get_datetime_tzid(end) |
paul@627 | 283 | else: |
paul@627 | 284 | return None |
paul@627 | 285 | |
paul@551 | 286 | def to_date(dt): |
paul@551 | 287 | |
paul@551 | 288 | "Return the date of 'dt'." |
paul@551 | 289 | |
paul@551 | 290 | return date(dt.year, dt.month, dt.day) |
paul@551 | 291 | |
paul@551 | 292 | def to_datetime(dt, tzid): |
paul@551 | 293 | |
paul@551 | 294 | """ |
paul@551 | 295 | Return a datetime for 'dt', using the start of day for dates, and using the |
paul@551 | 296 | 'tzid' for the conversion. |
paul@551 | 297 | """ |
paul@551 | 298 | |
paul@551 | 299 | if isinstance(dt, datetime): |
paul@637 | 300 | return to_timezone(dt, tzid) |
paul@551 | 301 | else: |
paul@551 | 302 | return get_start_of_day(dt, tzid) |
paul@551 | 303 | |
paul@637 | 304 | def to_utc_datetime(dt, tzid=None): |
paul@245 | 305 | |
paul@245 | 306 | """ |
paul@637 | 307 | Return a datetime corresponding to 'dt' in the UTC time zone. If 'tzid' |
paul@637 | 308 | is specified, dates and floating datetimes are converted to UTC datetimes |
paul@637 | 309 | using the time zone information; otherwise, such dates and datetimes remain |
paul@637 | 310 | unconverted. |
paul@245 | 311 | """ |
paul@245 | 312 | |
paul@551 | 313 | if not dt: |
paul@551 | 314 | return None |
paul@637 | 315 | elif get_datetime_tzid(dt): |
paul@551 | 316 | return to_timezone(dt, "UTC") |
paul@637 | 317 | elif tzid: |
paul@637 | 318 | return to_timezone(to_datetime(dt, tzid), "UTC") |
paul@551 | 319 | else: |
paul@551 | 320 | return dt |
paul@551 | 321 | |
paul@616 | 322 | def to_timezone(dt, tzid): |
paul@551 | 323 | |
paul@551 | 324 | """ |
paul@551 | 325 | Return a datetime corresponding to 'dt' in the time regime having the given |
paul@616 | 326 | 'tzid'. |
paul@551 | 327 | """ |
paul@195 | 328 | |
paul@551 | 329 | try: |
paul@616 | 330 | tz = tzid and timezone(tzid) or None |
paul@551 | 331 | except UnknownTimeZoneError: |
paul@551 | 332 | tz = None |
paul@551 | 333 | return to_tz(dt, tz) |
paul@551 | 334 | |
paul@551 | 335 | def to_tz(dt, tz): |
paul@247 | 336 | |
paul@551 | 337 | "Return a datetime corresponding to 'dt' employing the pytz.timezone 'tz'." |
paul@247 | 338 | |
paul@551 | 339 | if tz is not None and isinstance(dt, datetime): |
paul@551 | 340 | if not dt.tzinfo: |
paul@551 | 341 | return tz.localize(dt) |
paul@551 | 342 | else: |
paul@551 | 343 | return dt.astimezone(tz) |
paul@551 | 344 | else: |
paul@551 | 345 | return dt |
paul@222 | 346 | |
paul@551 | 347 | # iCalendar-related conversions. |
paul@551 | 348 | |
paul@551 | 349 | def end_date_from_calendar(dt): |
paul@291 | 350 | |
paul@291 | 351 | """ |
paul@551 | 352 | Change end dates to refer to the actual dates, not the iCalendar "next day" |
paul@551 | 353 | dates. |
paul@291 | 354 | """ |
paul@291 | 355 | |
paul@551 | 356 | if not isinstance(dt, datetime): |
paul@551 | 357 | return dt - timedelta(1) |
paul@551 | 358 | else: |
paul@551 | 359 | return dt |
paul@291 | 360 | |
paul@532 | 361 | def end_date_to_calendar(dt): |
paul@532 | 362 | |
paul@532 | 363 | """ |
paul@532 | 364 | Change end dates to refer to the iCalendar "next day" dates, not the actual |
paul@532 | 365 | dates. |
paul@532 | 366 | """ |
paul@532 | 367 | |
paul@532 | 368 | if not isinstance(dt, datetime): |
paul@532 | 369 | return dt + timedelta(1) |
paul@532 | 370 | else: |
paul@532 | 371 | return dt |
paul@532 | 372 | |
paul@551 | 373 | def get_datetime_attributes(dt, tzid=None): |
paul@551 | 374 | |
paul@616 | 375 | """ |
paul@616 | 376 | Return attributes for the 'dt' date or datetime object with 'tzid' |
paul@616 | 377 | indicating the time zone if not otherwise defined. |
paul@616 | 378 | """ |
paul@551 | 379 | |
paul@551 | 380 | if isinstance(dt, datetime): |
paul@551 | 381 | attr = {"VALUE" : "DATE-TIME"} |
paul@616 | 382 | tzid = get_datetime_tzid(dt) or tzid |
paul@551 | 383 | if tzid: |
paul@551 | 384 | attr["TZID"] = tzid |
paul@551 | 385 | return attr |
paul@551 | 386 | else: |
paul@551 | 387 | return {"VALUE" : "DATE"} |
paul@551 | 388 | |
paul@551 | 389 | def get_datetime_item(dt, tzid=None): |
paul@551 | 390 | |
paul@615 | 391 | """ |
paul@615 | 392 | Return an iCalendar-compatible string and attributes for 'dt' using any |
paul@616 | 393 | specified 'tzid' to assert a particular time zone if not otherwise defined. |
paul@615 | 394 | """ |
paul@551 | 395 | |
paul@551 | 396 | if not dt: |
paul@551 | 397 | return None, None |
paul@616 | 398 | if not get_datetime_tzid(dt): |
paul@616 | 399 | dt = to_timezone(dt, tzid) |
paul@551 | 400 | value = format_datetime(dt) |
paul@551 | 401 | attr = get_datetime_attributes(dt, tzid) |
paul@551 | 402 | return value, attr |
paul@551 | 403 | |
paul@627 | 404 | def get_period_attributes(start, end, tzid=None): |
paul@551 | 405 | |
paul@627 | 406 | """ |
paul@627 | 407 | Return attributes for the 'start' and 'end' datetime objects with 'tzid' |
paul@627 | 408 | indicating the time zone if not otherwise defined. |
paul@627 | 409 | """ |
paul@551 | 410 | |
paul@551 | 411 | attr = {"VALUE" : "PERIOD"} |
paul@627 | 412 | tzid = get_period_tzid(start, end) or tzid |
paul@551 | 413 | if tzid: |
paul@551 | 414 | attr["TZID"] = tzid |
paul@551 | 415 | return attr |
paul@551 | 416 | |
paul@551 | 417 | def get_period_item(start, end, tzid=None): |
paul@532 | 418 | |
paul@532 | 419 | """ |
paul@551 | 420 | Return an iCalendar-compatible string and attributes for 'start', 'end' and |
paul@551 | 421 | 'tzid'. |
paul@532 | 422 | """ |
paul@532 | 423 | |
paul@551 | 424 | if start and end: |
paul@627 | 425 | attr = get_period_attributes(start, end, tzid) |
paul@627 | 426 | start_value = format_datetime(to_timezone(start, attr.get("TZID"))) |
paul@627 | 427 | end_value = format_datetime(to_timezone(end, attr.get("TZID"))) |
paul@551 | 428 | return "%s/%s" % (start_value, end_value), attr |
paul@551 | 429 | elif start: |
paul@551 | 430 | attr = get_datetime_attributes(start, tzid) |
paul@627 | 431 | start_value = format_datetime(to_timezone(start, attr.get("TZID"))) |
paul@551 | 432 | return start_value, attr |
paul@532 | 433 | else: |
paul@551 | 434 | return None, None |
paul@551 | 435 | |
paul@551 | 436 | def get_timestamp(): |
paul@551 | 437 | |
paul@551 | 438 | "Return the current time as an iCalendar-compatible string." |
paul@551 | 439 | |
paul@551 | 440 | return format_datetime(to_timezone(datetime.utcnow(), "UTC")) |
paul@551 | 441 | |
paul@551 | 442 | def get_tzid(dtstart_attr, dtend_attr): |
paul@551 | 443 | |
paul@551 | 444 | """ |
paul@551 | 445 | Return any time regime details from the given 'dtstart_attr' and |
paul@551 | 446 | 'dtend_attr' attribute collections. |
paul@551 | 447 | """ |
paul@551 | 448 | |
paul@551 | 449 | return dtstart_attr and dtstart_attr.get("TZID") or dtend_attr and dtend_attr.get("TZID") or None |
paul@551 | 450 | |
paul@561 | 451 | def get_recurrence_start(recurrenceid): |
paul@561 | 452 | |
paul@561 | 453 | """ |
paul@561 | 454 | Return 'recurrenceid' in a form suitable for comparison with period start |
paul@627 | 455 | dates or datetimes. The 'recurrenceid' should be an identifier normalised to |
paul@627 | 456 | a UTC datetime or employing a date or floating datetime representation where |
paul@627 | 457 | no time zone information was originally provided. |
paul@561 | 458 | """ |
paul@561 | 459 | |
paul@561 | 460 | return get_datetime(recurrenceid) |
paul@561 | 461 | |
paul@561 | 462 | def get_recurrence_start_point(recurrenceid, tzid): |
paul@551 | 463 | |
paul@551 | 464 | """ |
paul@551 | 465 | Return 'recurrenceid' in a form suitable for comparison with free/busy start |
paul@551 | 466 | datetimes, using 'tzid' to convert recurrence identifiers that are dates. |
paul@627 | 467 | The 'recurrenceid' should be an identifier normalised to a UTC datetime or |
paul@627 | 468 | employing a date or floating datetime representation where no time zone |
paul@627 | 469 | information was originally provided. |
paul@551 | 470 | """ |
paul@551 | 471 | |
paul@552 | 472 | return to_utc_datetime(get_datetime(recurrenceid), tzid) |
paul@552 | 473 | |
paul@657 | 474 | # Time corrections. |
paul@657 | 475 | |
paul@660 | 476 | class ValidityError(Exception): |
paul@660 | 477 | pass |
paul@660 | 478 | |
paul@669 | 479 | def check_permitted_values(dt, permitted_values): |
paul@660 | 480 | |
paul@669 | 481 | "Check the datetime 'dt' against the 'permitted_values' list." |
paul@660 | 482 | |
paul@660 | 483 | if not isinstance(dt, datetime): |
paul@660 | 484 | raise ValidityError |
paul@660 | 485 | |
paul@669 | 486 | hours, minutes, seconds = permitted_values |
paul@660 | 487 | errors = [] |
paul@660 | 488 | |
paul@660 | 489 | if hours and dt.hour not in hours: |
paul@660 | 490 | errors.append("hour") |
paul@660 | 491 | if minutes and dt.minute not in minutes: |
paul@660 | 492 | errors.append("minute") |
paul@660 | 493 | if seconds and dt.second not in seconds: |
paul@660 | 494 | errors.append("second") |
paul@660 | 495 | |
paul@660 | 496 | return errors |
paul@660 | 497 | |
paul@669 | 498 | def correct_datetime(dt, permitted_values): |
paul@657 | 499 | |
paul@669 | 500 | "Correct 'dt' using the given 'permitted_values' details." |
paul@657 | 501 | |
paul@669 | 502 | carry, hour, minute, second = correct_value((dt.hour, dt.minute, dt.second), permitted_values) |
paul@657 | 503 | return datetime(dt.year, dt.month, dt.day, hour, minute, second, dt.microsecond, dt.tzinfo) + \ |
paul@657 | 504 | (carry and timedelta(1) or timedelta(0)) |
paul@657 | 505 | |
paul@669 | 506 | def correct_value(value, permitted_values): |
paul@657 | 507 | |
paul@657 | 508 | """ |
paul@657 | 509 | Correct the given (hour, minute, second) tuple 'value' according to the |
paul@669 | 510 | 'permitted_values' details. |
paul@657 | 511 | """ |
paul@657 | 512 | |
paul@657 | 513 | limits = 23, 59, 59 |
paul@657 | 514 | |
paul@657 | 515 | corrected = [] |
paul@657 | 516 | reset = False |
paul@657 | 517 | |
paul@657 | 518 | # Find invalid values and reset all following values. |
paul@657 | 519 | |
paul@669 | 520 | for v, values, limit in zip(value, permitted_values, limits): |
paul@657 | 521 | if reset: |
paul@657 | 522 | if values: |
paul@657 | 523 | v = values[0] |
paul@657 | 524 | else: |
paul@657 | 525 | v = 0 |
paul@657 | 526 | |
paul@657 | 527 | elif values and v not in values: |
paul@657 | 528 | reset = True |
paul@657 | 529 | |
paul@657 | 530 | corrected.append(v) |
paul@657 | 531 | |
paul@657 | 532 | value = corrected |
paul@657 | 533 | corrected = [] |
paul@657 | 534 | carry = 0 |
paul@657 | 535 | |
paul@657 | 536 | # Find invalid values and update them to the next valid value, updating more |
paul@657 | 537 | # significant values if the next valid value is the first in the appropriate |
paul@657 | 538 | # series. |
paul@657 | 539 | |
paul@669 | 540 | for v, values, limit in zip(value, permitted_values, limits)[::-1]: |
paul@657 | 541 | if carry: |
paul@657 | 542 | v += 1 |
paul@657 | 543 | if v > limit: |
paul@657 | 544 | if values: |
paul@657 | 545 | v = values[0] |
paul@657 | 546 | else: |
paul@657 | 547 | v = 0 |
paul@657 | 548 | corrected.append(v) |
paul@657 | 549 | continue |
paul@657 | 550 | else: |
paul@657 | 551 | carry = 0 |
paul@657 | 552 | |
paul@660 | 553 | if values: |
paul@660 | 554 | i = bisect_left(values, v) |
paul@660 | 555 | if i < len(values): |
paul@660 | 556 | v = values[i] |
paul@660 | 557 | else: |
paul@660 | 558 | v = values[0] |
paul@660 | 559 | carry = 1 |
paul@657 | 560 | |
paul@657 | 561 | corrected.append(v) |
paul@657 | 562 | |
paul@657 | 563 | return [carry] + corrected[::-1] |
paul@657 | 564 | |
paul@152 | 565 | # vim: tabstop=4 expandtab shiftwidth=4 |