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@759 | 135 | """ |
paul@759 | 136 | Return a duration for the given 'value' as a timedelta object. |
paul@759 | 137 | Where no valid duration is specified, None is returned. |
paul@759 | 138 | """ |
paul@551 | 139 | |
paul@551 | 140 | if not value: |
paul@551 | 141 | return None |
paul@551 | 142 | |
paul@551 | 143 | m = match_duration_icalendar(value) |
paul@551 | 144 | if m: |
paul@551 | 145 | weeks, days, hours, minutes, seconds = 0, 0, 0, 0, 0 |
paul@551 | 146 | for s in m.groups(): |
paul@551 | 147 | if not s: continue |
paul@551 | 148 | if s[-1] == "W": weeks += int(s[:-1]) |
paul@551 | 149 | elif s[-1] == "D": days += int(s[:-1]) |
paul@551 | 150 | elif s[-1] == "H": hours += int(s[:-1]) |
paul@551 | 151 | elif s[-1] == "M": minutes += int(s[:-1]) |
paul@551 | 152 | elif s[-1] == "S": seconds += int(s[:-1]) |
paul@551 | 153 | return timedelta( |
paul@551 | 154 | int(weeks) * 7 + int(days), |
paul@551 | 155 | (int(hours) * 60 + int(minutes)) * 60 + int(seconds) |
paul@551 | 156 | ) |
paul@551 | 157 | else: |
paul@551 | 158 | return None |
paul@551 | 159 | |
paul@387 | 160 | def get_period(value, attr=None): |
paul@387 | 161 | |
paul@387 | 162 | """ |
paul@387 | 163 | Return a tuple of the form (start, end) for the given 'value' in iCalendar |
paul@387 | 164 | format, using the 'attr' mapping (if specified) to control the conversion. |
paul@387 | 165 | """ |
paul@387 | 166 | |
paul@630 | 167 | if not value or attr and attr.get("VALUE") and attr.get("VALUE") != "PERIOD": |
paul@387 | 168 | return None |
paul@387 | 169 | |
paul@387 | 170 | t = value.split("/") |
paul@387 | 171 | if len(t) != 2: |
paul@387 | 172 | return None |
paul@387 | 173 | |
paul@388 | 174 | dtattr = {} |
paul@388 | 175 | if attr: |
paul@388 | 176 | dtattr.update(attr) |
paul@388 | 177 | if dtattr.has_key("VALUE"): |
paul@388 | 178 | del dtattr["VALUE"] |
paul@388 | 179 | |
paul@388 | 180 | start = get_datetime(t[0], dtattr) |
paul@387 | 181 | if t[1].startswith("P"): |
paul@387 | 182 | end = start + get_duration(t[1]) |
paul@387 | 183 | else: |
paul@388 | 184 | end = get_datetime(t[1], dtattr) |
paul@387 | 185 | |
paul@387 | 186 | return start, end |
paul@387 | 187 | |
paul@551 | 188 | # Time zone conversions and retrieval. |
paul@551 | 189 | |
paul@551 | 190 | def ends_on_same_day(dt, end, tzid): |
paul@387 | 191 | |
paul@551 | 192 | """ |
paul@551 | 193 | Return whether 'dt' ends on the same day as 'end', testing the date |
paul@551 | 194 | components of 'dt' and 'end' against each other, but also testing whether |
paul@551 | 195 | 'end' is the actual end of the day in which 'dt' is positioned. |
paul@387 | 196 | |
paul@551 | 197 | Since time zone transitions may occur within a day, 'tzid' is required to |
paul@551 | 198 | determine the end of the day in which 'dt' is positioned, using the zone |
paul@551 | 199 | appropriate at that point in time, not necessarily the zone applying to |
paul@551 | 200 | 'dt'. |
paul@551 | 201 | """ |
paul@387 | 202 | |
paul@551 | 203 | return ( |
paul@551 | 204 | to_timezone(dt, tzid).date() == to_timezone(end, tzid).date() or |
paul@551 | 205 | end == get_end_of_day(dt, tzid) |
paul@551 | 206 | ) |
paul@551 | 207 | |
paul@551 | 208 | def get_default_timezone(): |
paul@551 | 209 | |
paul@551 | 210 | "Return the system time regime." |
paul@551 | 211 | |
paul@551 | 212 | filename = "/etc/timezone" |
paul@551 | 213 | |
paul@551 | 214 | if exists(filename): |
paul@551 | 215 | f = open(filename) |
paul@551 | 216 | try: |
paul@551 | 217 | return f.read().strip() |
paul@551 | 218 | finally: |
paul@551 | 219 | f.close() |
paul@387 | 220 | else: |
paul@387 | 221 | return None |
paul@387 | 222 | |
paul@551 | 223 | def get_end_of_day(dt, tzid): |
paul@431 | 224 | |
paul@431 | 225 | """ |
paul@551 | 226 | Get the end of the day in which 'dt' is positioned, using the given 'tzid' |
paul@551 | 227 | to obtain a datetime in the appropriate time zone. Where time zone |
paul@551 | 228 | transitions occur within a day, the zone of 'dt' may not be the eventual |
paul@551 | 229 | zone of the returned object. |
paul@431 | 230 | """ |
paul@431 | 231 | |
paul@551 | 232 | return get_start_of_day(dt + timedelta(1), tzid) |
paul@431 | 233 | |
paul@244 | 234 | def get_start_of_day(dt, tzid): |
paul@245 | 235 | |
paul@245 | 236 | """ |
paul@245 | 237 | Get the start of the day in which 'dt' is positioned, using the given 'tzid' |
paul@245 | 238 | to obtain a datetime in the appropriate time zone. Where time zone |
paul@245 | 239 | transitions occur within a day, the zone of 'dt' may not be the eventual |
paul@245 | 240 | zone of the returned object. |
paul@245 | 241 | """ |
paul@245 | 242 | |
paul@244 | 243 | start = datetime(dt.year, dt.month, dt.day, 0, 0) |
paul@244 | 244 | return to_timezone(start, tzid) |
paul@152 | 245 | |
paul@244 | 246 | def get_start_of_next_day(dt, tzid): |
paul@245 | 247 | |
paul@245 | 248 | """ |
paul@245 | 249 | Get the start of the day after the day in which 'dt' is positioned. This |
paul@245 | 250 | function is intended to extend either dates or datetimes to the end of a |
paul@245 | 251 | day for the purpose of generating a missing end date or datetime for an |
paul@245 | 252 | event. |
paul@245 | 253 | |
paul@245 | 254 | If 'dt' is a date and not a datetime, a plain date object for the next day |
paul@245 | 255 | will be returned. |
paul@245 | 256 | |
paul@245 | 257 | If 'dt' is a datetime, the given 'tzid' is used to obtain a datetime in the |
paul@245 | 258 | appropriate time zone. Where time zone transitions occur within a day, the |
paul@245 | 259 | zone of 'dt' may not be the eventual zone of the returned object. |
paul@245 | 260 | """ |
paul@245 | 261 | |
paul@239 | 262 | if isinstance(dt, datetime): |
paul@239 | 263 | return get_end_of_day(dt, tzid) |
paul@239 | 264 | else: |
paul@239 | 265 | return dt + timedelta(1) |
paul@239 | 266 | |
paul@616 | 267 | def get_datetime_tzid(dt): |
paul@616 | 268 | |
paul@616 | 269 | "Return the time zone identifier from 'dt' or None if unknown." |
paul@616 | 270 | |
paul@616 | 271 | if not isinstance(dt, datetime): |
paul@616 | 272 | return None |
paul@616 | 273 | elif dt.tzname() == "UTC": |
paul@616 | 274 | return "UTC" |
paul@616 | 275 | elif dt.tzinfo and hasattr(dt.tzinfo, "zone"): |
paul@616 | 276 | return dt.tzinfo.zone |
paul@616 | 277 | else: |
paul@616 | 278 | return None |
paul@616 | 279 | |
paul@627 | 280 | def get_period_tzid(start, end): |
paul@627 | 281 | |
paul@627 | 282 | "Return the time zone identifier for 'start' and 'end' or None if unknown." |
paul@627 | 283 | |
paul@627 | 284 | if isinstance(start, datetime) or isinstance(end, datetime): |
paul@627 | 285 | return get_datetime_tzid(start) or get_datetime_tzid(end) |
paul@627 | 286 | else: |
paul@627 | 287 | return None |
paul@627 | 288 | |
paul@551 | 289 | def to_date(dt): |
paul@551 | 290 | |
paul@551 | 291 | "Return the date of 'dt'." |
paul@551 | 292 | |
paul@551 | 293 | return date(dt.year, dt.month, dt.day) |
paul@551 | 294 | |
paul@551 | 295 | def to_datetime(dt, tzid): |
paul@551 | 296 | |
paul@551 | 297 | """ |
paul@551 | 298 | Return a datetime for 'dt', using the start of day for dates, and using the |
paul@551 | 299 | 'tzid' for the conversion. |
paul@551 | 300 | """ |
paul@551 | 301 | |
paul@551 | 302 | if isinstance(dt, datetime): |
paul@637 | 303 | return to_timezone(dt, tzid) |
paul@551 | 304 | else: |
paul@551 | 305 | return get_start_of_day(dt, tzid) |
paul@551 | 306 | |
paul@637 | 307 | def to_utc_datetime(dt, tzid=None): |
paul@245 | 308 | |
paul@245 | 309 | """ |
paul@637 | 310 | Return a datetime corresponding to 'dt' in the UTC time zone. If 'tzid' |
paul@637 | 311 | is specified, dates and floating datetimes are converted to UTC datetimes |
paul@637 | 312 | using the time zone information; otherwise, such dates and datetimes remain |
paul@637 | 313 | unconverted. |
paul@245 | 314 | """ |
paul@245 | 315 | |
paul@551 | 316 | if not dt: |
paul@551 | 317 | return None |
paul@637 | 318 | elif get_datetime_tzid(dt): |
paul@551 | 319 | return to_timezone(dt, "UTC") |
paul@637 | 320 | elif tzid: |
paul@637 | 321 | return to_timezone(to_datetime(dt, tzid), "UTC") |
paul@551 | 322 | else: |
paul@551 | 323 | return dt |
paul@551 | 324 | |
paul@616 | 325 | def to_timezone(dt, tzid): |
paul@551 | 326 | |
paul@551 | 327 | """ |
paul@551 | 328 | Return a datetime corresponding to 'dt' in the time regime having the given |
paul@616 | 329 | 'tzid'. |
paul@551 | 330 | """ |
paul@195 | 331 | |
paul@551 | 332 | try: |
paul@616 | 333 | tz = tzid and timezone(tzid) or None |
paul@551 | 334 | except UnknownTimeZoneError: |
paul@551 | 335 | tz = None |
paul@551 | 336 | return to_tz(dt, tz) |
paul@551 | 337 | |
paul@551 | 338 | def to_tz(dt, tz): |
paul@247 | 339 | |
paul@551 | 340 | "Return a datetime corresponding to 'dt' employing the pytz.timezone 'tz'." |
paul@247 | 341 | |
paul@551 | 342 | if tz is not None and isinstance(dt, datetime): |
paul@551 | 343 | if not dt.tzinfo: |
paul@551 | 344 | return tz.localize(dt) |
paul@551 | 345 | else: |
paul@551 | 346 | return dt.astimezone(tz) |
paul@551 | 347 | else: |
paul@551 | 348 | return dt |
paul@222 | 349 | |
paul@551 | 350 | # iCalendar-related conversions. |
paul@551 | 351 | |
paul@551 | 352 | def end_date_from_calendar(dt): |
paul@291 | 353 | |
paul@291 | 354 | """ |
paul@551 | 355 | Change end dates to refer to the actual dates, not the iCalendar "next day" |
paul@551 | 356 | dates. |
paul@291 | 357 | """ |
paul@291 | 358 | |
paul@551 | 359 | if not isinstance(dt, datetime): |
paul@551 | 360 | return dt - timedelta(1) |
paul@551 | 361 | else: |
paul@551 | 362 | return dt |
paul@291 | 363 | |
paul@532 | 364 | def end_date_to_calendar(dt): |
paul@532 | 365 | |
paul@532 | 366 | """ |
paul@532 | 367 | Change end dates to refer to the iCalendar "next day" dates, not the actual |
paul@532 | 368 | dates. |
paul@532 | 369 | """ |
paul@532 | 370 | |
paul@532 | 371 | if not isinstance(dt, datetime): |
paul@532 | 372 | return dt + timedelta(1) |
paul@532 | 373 | else: |
paul@532 | 374 | return dt |
paul@532 | 375 | |
paul@551 | 376 | def get_datetime_attributes(dt, tzid=None): |
paul@551 | 377 | |
paul@616 | 378 | """ |
paul@616 | 379 | Return attributes for the 'dt' date or datetime object with 'tzid' |
paul@616 | 380 | indicating the time zone if not otherwise defined. |
paul@616 | 381 | """ |
paul@551 | 382 | |
paul@551 | 383 | if isinstance(dt, datetime): |
paul@551 | 384 | attr = {"VALUE" : "DATE-TIME"} |
paul@616 | 385 | tzid = get_datetime_tzid(dt) or tzid |
paul@551 | 386 | if tzid: |
paul@551 | 387 | attr["TZID"] = tzid |
paul@551 | 388 | return attr |
paul@551 | 389 | else: |
paul@551 | 390 | return {"VALUE" : "DATE"} |
paul@551 | 391 | |
paul@551 | 392 | def get_datetime_item(dt, tzid=None): |
paul@551 | 393 | |
paul@615 | 394 | """ |
paul@615 | 395 | Return an iCalendar-compatible string and attributes for 'dt' using any |
paul@616 | 396 | specified 'tzid' to assert a particular time zone if not otherwise defined. |
paul@615 | 397 | """ |
paul@551 | 398 | |
paul@551 | 399 | if not dt: |
paul@551 | 400 | return None, None |
paul@616 | 401 | if not get_datetime_tzid(dt): |
paul@616 | 402 | dt = to_timezone(dt, tzid) |
paul@551 | 403 | value = format_datetime(dt) |
paul@551 | 404 | attr = get_datetime_attributes(dt, tzid) |
paul@551 | 405 | return value, attr |
paul@551 | 406 | |
paul@627 | 407 | def get_period_attributes(start, end, tzid=None): |
paul@551 | 408 | |
paul@627 | 409 | """ |
paul@627 | 410 | Return attributes for the 'start' and 'end' datetime objects with 'tzid' |
paul@627 | 411 | indicating the time zone if not otherwise defined. |
paul@627 | 412 | """ |
paul@551 | 413 | |
paul@551 | 414 | attr = {"VALUE" : "PERIOD"} |
paul@627 | 415 | tzid = get_period_tzid(start, end) or tzid |
paul@551 | 416 | if tzid: |
paul@551 | 417 | attr["TZID"] = tzid |
paul@551 | 418 | return attr |
paul@551 | 419 | |
paul@551 | 420 | def get_period_item(start, end, tzid=None): |
paul@532 | 421 | |
paul@532 | 422 | """ |
paul@551 | 423 | Return an iCalendar-compatible string and attributes for 'start', 'end' and |
paul@551 | 424 | 'tzid'. |
paul@532 | 425 | """ |
paul@532 | 426 | |
paul@551 | 427 | if start and end: |
paul@627 | 428 | attr = get_period_attributes(start, end, tzid) |
paul@627 | 429 | start_value = format_datetime(to_timezone(start, attr.get("TZID"))) |
paul@627 | 430 | end_value = format_datetime(to_timezone(end, attr.get("TZID"))) |
paul@551 | 431 | return "%s/%s" % (start_value, end_value), attr |
paul@551 | 432 | elif start: |
paul@551 | 433 | attr = get_datetime_attributes(start, tzid) |
paul@627 | 434 | start_value = format_datetime(to_timezone(start, attr.get("TZID"))) |
paul@551 | 435 | return start_value, attr |
paul@532 | 436 | else: |
paul@551 | 437 | return None, None |
paul@551 | 438 | |
paul@759 | 439 | def get_timestamp(offset=None): |
paul@551 | 440 | |
paul@551 | 441 | "Return the current time as an iCalendar-compatible string." |
paul@551 | 442 | |
paul@759 | 443 | offset = offset or timedelta(0) |
paul@759 | 444 | return format_datetime(to_timezone(datetime.utcnow(), "UTC") + offset) |
paul@759 | 445 | |
paul@759 | 446 | def get_time(offset=None): |
paul@759 | 447 | |
paul@759 | 448 | "Return the current time." |
paul@759 | 449 | |
paul@759 | 450 | offset = offset or timedelta(0) |
paul@759 | 451 | return to_timezone(datetime.utcnow(), "UTC") + offset |
paul@551 | 452 | |
paul@551 | 453 | def get_tzid(dtstart_attr, dtend_attr): |
paul@551 | 454 | |
paul@551 | 455 | """ |
paul@551 | 456 | Return any time regime details from the given 'dtstart_attr' and |
paul@551 | 457 | 'dtend_attr' attribute collections. |
paul@551 | 458 | """ |
paul@551 | 459 | |
paul@551 | 460 | return dtstart_attr and dtstart_attr.get("TZID") or dtend_attr and dtend_attr.get("TZID") or None |
paul@551 | 461 | |
paul@561 | 462 | def get_recurrence_start(recurrenceid): |
paul@561 | 463 | |
paul@561 | 464 | """ |
paul@561 | 465 | Return 'recurrenceid' in a form suitable for comparison with period start |
paul@627 | 466 | dates or datetimes. The 'recurrenceid' should be an identifier normalised to |
paul@627 | 467 | a UTC datetime or employing a date or floating datetime representation where |
paul@627 | 468 | no time zone information was originally provided. |
paul@561 | 469 | """ |
paul@561 | 470 | |
paul@561 | 471 | return get_datetime(recurrenceid) |
paul@561 | 472 | |
paul@561 | 473 | def get_recurrence_start_point(recurrenceid, tzid): |
paul@551 | 474 | |
paul@551 | 475 | """ |
paul@551 | 476 | Return 'recurrenceid' in a form suitable for comparison with free/busy start |
paul@551 | 477 | datetimes, using 'tzid' to convert recurrence identifiers that are dates. |
paul@627 | 478 | The 'recurrenceid' should be an identifier normalised to a UTC datetime or |
paul@627 | 479 | employing a date or floating datetime representation where no time zone |
paul@627 | 480 | information was originally provided. |
paul@551 | 481 | """ |
paul@551 | 482 | |
paul@552 | 483 | return to_utc_datetime(get_datetime(recurrenceid), tzid) |
paul@552 | 484 | |
paul@657 | 485 | # Time corrections. |
paul@657 | 486 | |
paul@660 | 487 | class ValidityError(Exception): |
paul@660 | 488 | pass |
paul@660 | 489 | |
paul@669 | 490 | def check_permitted_values(dt, permitted_values): |
paul@660 | 491 | |
paul@669 | 492 | "Check the datetime 'dt' against the 'permitted_values' list." |
paul@660 | 493 | |
paul@660 | 494 | if not isinstance(dt, datetime): |
paul@660 | 495 | raise ValidityError |
paul@660 | 496 | |
paul@669 | 497 | hours, minutes, seconds = permitted_values |
paul@660 | 498 | errors = [] |
paul@660 | 499 | |
paul@660 | 500 | if hours and dt.hour not in hours: |
paul@660 | 501 | errors.append("hour") |
paul@660 | 502 | if minutes and dt.minute not in minutes: |
paul@660 | 503 | errors.append("minute") |
paul@660 | 504 | if seconds and dt.second not in seconds: |
paul@660 | 505 | errors.append("second") |
paul@660 | 506 | |
paul@660 | 507 | return errors |
paul@660 | 508 | |
paul@669 | 509 | def correct_datetime(dt, permitted_values): |
paul@657 | 510 | |
paul@669 | 511 | "Correct 'dt' using the given 'permitted_values' details." |
paul@657 | 512 | |
paul@669 | 513 | carry, hour, minute, second = correct_value((dt.hour, dt.minute, dt.second), permitted_values) |
paul@657 | 514 | return datetime(dt.year, dt.month, dt.day, hour, minute, second, dt.microsecond, dt.tzinfo) + \ |
paul@657 | 515 | (carry and timedelta(1) or timedelta(0)) |
paul@657 | 516 | |
paul@669 | 517 | def correct_value(value, permitted_values): |
paul@657 | 518 | |
paul@657 | 519 | """ |
paul@657 | 520 | Correct the given (hour, minute, second) tuple 'value' according to the |
paul@669 | 521 | 'permitted_values' details. |
paul@657 | 522 | """ |
paul@657 | 523 | |
paul@657 | 524 | limits = 23, 59, 59 |
paul@657 | 525 | |
paul@657 | 526 | corrected = [] |
paul@657 | 527 | reset = False |
paul@657 | 528 | |
paul@657 | 529 | # Find invalid values and reset all following values. |
paul@657 | 530 | |
paul@669 | 531 | for v, values, limit in zip(value, permitted_values, limits): |
paul@657 | 532 | if reset: |
paul@657 | 533 | if values: |
paul@657 | 534 | v = values[0] |
paul@657 | 535 | else: |
paul@657 | 536 | v = 0 |
paul@657 | 537 | |
paul@657 | 538 | elif values and v not in values: |
paul@657 | 539 | reset = True |
paul@657 | 540 | |
paul@657 | 541 | corrected.append(v) |
paul@657 | 542 | |
paul@657 | 543 | value = corrected |
paul@657 | 544 | corrected = [] |
paul@657 | 545 | carry = 0 |
paul@657 | 546 | |
paul@657 | 547 | # Find invalid values and update them to the next valid value, updating more |
paul@657 | 548 | # significant values if the next valid value is the first in the appropriate |
paul@657 | 549 | # series. |
paul@657 | 550 | |
paul@669 | 551 | for v, values, limit in zip(value, permitted_values, limits)[::-1]: |
paul@657 | 552 | if carry: |
paul@657 | 553 | v += 1 |
paul@657 | 554 | if v > limit: |
paul@657 | 555 | if values: |
paul@657 | 556 | v = values[0] |
paul@657 | 557 | else: |
paul@657 | 558 | v = 0 |
paul@657 | 559 | corrected.append(v) |
paul@657 | 560 | continue |
paul@657 | 561 | else: |
paul@657 | 562 | carry = 0 |
paul@657 | 563 | |
paul@660 | 564 | if values: |
paul@660 | 565 | i = bisect_left(values, v) |
paul@660 | 566 | if i < len(values): |
paul@660 | 567 | v = values[i] |
paul@660 | 568 | else: |
paul@660 | 569 | v = values[0] |
paul@660 | 570 | carry = 1 |
paul@657 | 571 | |
paul@657 | 572 | corrected.append(v) |
paul@657 | 573 | |
paul@657 | 574 | return [carry] + corrected[::-1] |
paul@657 | 575 | |
paul@152 | 576 | # vim: tabstop=4 expandtab shiftwidth=4 |