File: dates.py

package info (click to toggle)
imip-agent 0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,056 kB
  • sloc: python: 9,888; sh: 4,480; sql: 144; makefile: 8
file content (602 lines) | stat: -rw-r--r-- 17,025 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
#!/usr/bin/env python

"""
Date processing functions.

Copyright (C) 2014, 2015, 2016 Paul Boddie <paul@boddie.org.uk>

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 3 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
details.

You should have received a copy of the GNU General Public License along with
this program.  If not, see <http://www.gnu.org/licenses/>.
"""

from bisect import bisect_left
from datetime import date, datetime, timedelta
from os.path import exists
from pytz import timezone, UnknownTimeZoneError
import re

# iCalendar date and datetime parsing (from DateSupport in MoinSupport).

_date_icalendar_regexp_str = ur'(?P<year>[0-9]{4})(?P<month>[0-9]{2})(?P<day>[0-9]{2})'
date_icalendar_regexp_str = _date_icalendar_regexp_str + '$'

datetime_icalendar_regexp_str = _date_icalendar_regexp_str + \
    ur'(?:' \
    ur'T(?P<hour>[0-2][0-9])(?P<minute>[0-5][0-9])(?P<second>[0-6][0-9])' \
    ur'(?P<utc>Z)?' \
    ur')?$'

_duration_time_icalendar_regexp_str = \
    ur'T' \
    ur'(?:' \
    ur'([0-9]+H)(?:([0-9]+M)([0-9]+S)?)?' \
    ur'|' \
    ur'([0-9]+M)([0-9]+S)?' \
    ur'|' \
    ur'([0-9]+S)' \
    ur')'

duration_icalendar_regexp_str = ur'P' \
    ur'(?:' \
    ur'([0-9]+W)' \
    ur'|' \
    ur'(?:%s)' \
    ur'|' \
    ur'([0-9]+D)(?:%s)?' \
    ur')$' % (_duration_time_icalendar_regexp_str, _duration_time_icalendar_regexp_str)

match_date_icalendar = re.compile(date_icalendar_regexp_str, re.UNICODE).match
match_datetime_icalendar = re.compile(datetime_icalendar_regexp_str, re.UNICODE).match
match_duration_icalendar = re.compile(duration_icalendar_regexp_str, re.UNICODE).match

# Datetime formatting.

def format_datetime(dt):

    "Format 'dt' as an iCalendar-compatible string."

    if not dt:
        return None
    elif isinstance(dt, datetime):
        if dt.tzname() == "UTC":
            return dt.strftime("%Y%m%dT%H%M%SZ")
        else:
            return dt.strftime("%Y%m%dT%H%M%S")
    else:
        return dt.strftime("%Y%m%d")

def format_time(dt):

    "Format the time portion of 'dt' as an iCalendar-compatible string."

    if not dt:
        return None
    elif isinstance(dt, datetime):
        if dt.tzname() == "UTC":
            return dt.strftime("%H%M%SZ")
        else:
            return dt.strftime("%H%M%S")
    else:
        return None

def format_duration(td):

    "Format the timedelta 'td' as an iCalendar-compatible string."

    if not td:
        return None
    else:
        day_portion = td.days and "%dD" % td.days or ""
        time_portion = td.seconds and "T%dS" % td.seconds or ""
        if not day_portion and not time_portion:
            time_portion = "T0S"
        return "P%s%s" % (day_portion, time_portion)

# Parsing of datetime and related information.

def get_datetime(value, attr=None):

    """
    Return a datetime object from the given 'value' in iCalendar format, using
    the 'attr' mapping (if specified) to control the conversion.
    """

    if not value:
        return None

    if len(value) > 9 and (not attr or attr.get("VALUE") in (None, "DATE-TIME")):
        m = match_datetime_icalendar(value)
        if m:
            year, month, day, hour, minute, second = map(m.group, [
                "year", "month", "day", "hour", "minute", "second"
                ])

            if hour and minute and second:
                dt = datetime(
                    int(year), int(month), int(day), int(hour), int(minute), int(second)
                    )

                # Impose the indicated timezone.
                # NOTE: This needs an ambiguity policy for DST changes.

                return to_timezone(dt, m.group("utc") and "UTC" or attr and attr.get("TZID") or None)

        return None

    # Permit dates even if the VALUE is not set to DATE.

    if not attr or attr.get("VALUE") in (None, "DATE"):
        m = match_date_icalendar(value)
        if m:
            year, month, day = map(m.group, ["year", "month", "day"])
            return date(int(year), int(month), int(day))

    return None

def get_duration(value):

    """
    Return a duration for the given 'value' as a timedelta object.
    Where no valid duration is specified, None is returned.
    """

    if not value:
        return None

    m = match_duration_icalendar(value)
    if m:
        weeks, days, hours, minutes, seconds = 0, 0, 0, 0, 0
        for s in m.groups():
            if not s: continue
            if s[-1] == "W": weeks += int(s[:-1])
            elif s[-1] == "D": days += int(s[:-1])
            elif s[-1] == "H": hours += int(s[:-1])
            elif s[-1] == "M": minutes += int(s[:-1])
            elif s[-1] == "S": seconds += int(s[:-1])
        return timedelta(
            int(weeks) * 7 + int(days),
            (int(hours) * 60 + int(minutes)) * 60 + int(seconds)
            )
    else:
        return None

def get_period(value, attr=None):

    """
    Return a tuple of the form (start, end) for the given 'value' in iCalendar
    format, using the 'attr' mapping (if specified) to control the conversion.
    """

    if not value or attr and attr.get("VALUE") and attr.get("VALUE") != "PERIOD":
        return None

    t = value.split("/")
    if len(t) != 2:
        return None

    dtattr = {}
    if attr:
        dtattr.update(attr)
        if dtattr.has_key("VALUE"):
            del dtattr["VALUE"]

    start = get_datetime(t[0], dtattr)
    if t[1].startswith("P"):
        end = start + get_duration(t[1])
    else:
        end = get_datetime(t[1], dtattr)

    return start, end

# Time zone conversions and retrieval.

def ends_on_same_day(dt, end, tzid):

    """
    Return whether 'dt' ends on the same day as 'end', testing the date
    components of 'dt' and 'end' against each other, but also testing whether
    'end' is the actual end of the day in which 'dt' is positioned.

    Since time zone transitions may occur within a day, 'tzid' is required to
    determine the end of the day in which 'dt' is positioned, using the zone
    appropriate at that point in time, not necessarily the zone applying to
    'dt'.
    """

    return (
        to_timezone(dt, tzid).date() == to_timezone(end, tzid).date() or
        end == get_end_of_day(dt, tzid)
        )

def get_default_timezone():

    "Return the system time regime."

    filename = "/etc/timezone"

    if exists(filename):
        f = open(filename)
        try:
            return f.read().strip()
        finally:
            f.close()
    else:
        return None

def get_end_of_day(dt, tzid):

    """
    Get the end of the day in which 'dt' is positioned, using the given 'tzid'
    to obtain a datetime in the appropriate time zone. Where time zone
    transitions occur within a day, the zone of 'dt' may not be the eventual
    zone of the returned object.
    """

    return get_start_of_day(dt + timedelta(1), tzid)

def get_start_of_day(dt, tzid):

    """
    Get the start of the day in which 'dt' is positioned, using the given 'tzid'
    to obtain a datetime in the appropriate time zone. Where time zone
    transitions occur within a day, the zone of 'dt' may not be the eventual
    zone of the returned object.
    """

    start = datetime(dt.year, dt.month, dt.day, 0, 0)
    return to_timezone(start, tzid)

def get_start_of_next_day(dt, tzid):

    """
    Get the start of the day after the day in which 'dt' is positioned. This
    function is intended to extend either dates or datetimes to the end of a
    day for the purpose of generating a missing end date or datetime for an
    event.

    If 'dt' is a date and not a datetime, a plain date object for the next day
    will be returned.

    If 'dt' is a datetime, the given 'tzid' is used to obtain a datetime in the
    appropriate time zone. Where time zone transitions occur within a day, the
    zone of 'dt' may not be the eventual zone of the returned object.
    """

    if isinstance(dt, datetime):
        return get_end_of_day(dt, tzid)
    else:
        return dt + timedelta(1)

def get_datetime_tzid(dt):

    "Return the time zone identifier from 'dt' or None if unknown."

    if not isinstance(dt, datetime):
        return None
    elif dt.tzname() == "UTC":
        return "UTC"
    elif dt.tzinfo and hasattr(dt.tzinfo, "zone"):
        return dt.tzinfo.zone
    else:
        return None

def get_period_tzid(start, end):

    "Return the time zone identifier for 'start' and 'end' or None if unknown."

    if isinstance(start, datetime) or isinstance(end, datetime):
        return get_datetime_tzid(start) or get_datetime_tzid(end)
    else:
        return None

def to_date(dt):

    "Return the date of 'dt'."

    return date(dt.year, dt.month, dt.day)

def to_datetime(dt, tzid):

    """
    Return a datetime for 'dt', using the start of day for dates, and using the
    'tzid' for the conversion.
    """

    if isinstance(dt, datetime):
        return to_timezone(dt, tzid)
    else:
        return get_start_of_day(dt, tzid)

def to_utc_datetime(dt, tzid=None):

    """
    Return a datetime corresponding to 'dt' in the UTC time zone. If 'tzid'
    is specified, dates and floating datetimes are converted to UTC datetimes
    using the time zone information; otherwise, such dates and datetimes remain
    unconverted.
    """

    if not dt:
        return None
    elif get_datetime_tzid(dt):
        return to_timezone(dt, "UTC")
    elif tzid:
        return to_timezone(to_datetime(dt, tzid), "UTC")
    else:
        return dt

def to_timezone(dt, tzid):

    """
    Return a datetime corresponding to 'dt' in the time regime having the given
    'tzid'.
    """

    try:
        tz = tzid and timezone(tzid) or None
    except UnknownTimeZoneError:
        tz = None
    return to_tz(dt, tz)

def to_tz(dt, tz):

    "Return a datetime corresponding to 'dt' employing the pytz.timezone 'tz'."

    if tz is not None and isinstance(dt, datetime):
        if not dt.tzinfo:
            return tz.localize(dt)
        else:
            return dt.astimezone(tz)
    else:
        return dt

# iCalendar-related conversions.

def end_date_from_calendar(dt):

    """
    Change end dates to refer to the actual dates, not the iCalendar "next day"
    dates.
    """

    if not isinstance(dt, datetime):
        return dt - timedelta(1)
    else:
        return dt

def end_date_to_calendar(dt):

    """
    Change end dates to refer to the iCalendar "next day" dates, not the actual
    dates.
    """

    if not isinstance(dt, datetime):
        return dt + timedelta(1)
    else:
        return dt

def get_datetime_attributes(dt, tzid=None):

    """
    Return attributes for the 'dt' date or datetime object with 'tzid'
    indicating the time zone if not otherwise defined.
    """

    if isinstance(dt, datetime):
        attr = {"VALUE" : "DATE-TIME"}
        tzid = get_datetime_tzid(dt) or tzid
        if tzid:
            attr["TZID"] = tzid
        return attr
    else:
        return {"VALUE" : "DATE"}

def get_datetime_item(dt, tzid=None):

    """
    Return an iCalendar-compatible string and attributes for 'dt' using any
    specified 'tzid' to assert a particular time zone if not otherwise defined.
    """

    if not dt:
        return None, None
    if not get_datetime_tzid(dt):
        dt = to_timezone(dt, tzid)
    value = format_datetime(dt)
    attr = get_datetime_attributes(dt, tzid)
    return value, attr

def get_period_attributes(start, end, tzid=None):

    """
    Return attributes for the 'start' and 'end' datetime objects with 'tzid'
    indicating the time zone if not otherwise defined.
    """

    attr = {"VALUE" : "PERIOD"}
    tzid = get_period_tzid(start, end) or tzid
    if tzid:
        attr["TZID"] = tzid
    return attr

def get_period_item(start, end, tzid=None):

    """
    Return an iCalendar-compatible string and attributes for 'start', 'end' and
    'tzid'.
    """

    if start and end:
        attr = get_period_attributes(start, end, tzid)
        start_value = format_datetime(to_timezone(start, attr.get("TZID")))
        end_value = format_datetime(to_timezone(end, attr.get("TZID")))
        return "%s/%s" % (start_value, end_value), attr
    elif start:
        attr = get_datetime_attributes(start, tzid)
        start_value = format_datetime(to_timezone(start, attr.get("TZID")))
        return start_value, attr
    else:
        return None, None

def get_timestamp(offset=None):

    "Return the current time as an iCalendar-compatible string."

    offset = offset or timedelta(0)
    return format_datetime(to_timezone(datetime.utcnow(), "UTC") + offset)

def get_date(offset=None):

    """
    Return the current date, offset by the given timedelta 'offset' if
    specified. The returned date will not be positioned in any time zone.
    """

    offset = offset or timedelta(0)
    return date.today() + offset

def get_time(offset=None):

    """
    Return the current time, offset by the given timedelta 'offset' if
    specified. The returned time will be in the UTC time zone.
    """

    offset = offset or timedelta(0)
    return to_timezone(datetime.utcnow(), "UTC") + offset

def get_tzid(dtstart_attr, dtend_attr):

    """
    Return any time regime details from the given 'dtstart_attr' and
    'dtend_attr' attribute collections.
    """

    return dtstart_attr and dtstart_attr.get("TZID") or dtend_attr and dtend_attr.get("TZID") or None

def get_recurrence_start(recurrenceid):

    """
    Return 'recurrenceid' in a form suitable for comparison with period start
    dates or datetimes. The 'recurrenceid' should be an identifier normalised to
    a UTC datetime or employing a date or floating datetime representation where
    no time zone information was originally provided.
    """

    return get_datetime(recurrenceid)

def get_recurrence_start_point(recurrenceid, tzid):

    """
    Return 'recurrenceid' in a form suitable for comparison with free/busy start
    datetimes, using 'tzid' to convert recurrence identifiers that are dates.
    The 'recurrenceid' should be an identifier normalised to a UTC datetime or
    employing a date or floating datetime representation where no time zone
    information was originally provided.
    """

    return to_utc_datetime(get_datetime(recurrenceid), tzid)

# Time corrections.

class ValidityError(Exception):
    pass

def check_permitted_values(dt, permitted_values):

    "Check the datetime 'dt' against the 'permitted_values' list."

    if not isinstance(dt, datetime):
        raise ValidityError

    hours, minutes, seconds = permitted_values
    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, permitted_values):

    "Correct 'dt' using the given 'permitted_values' details."

    carry, hour, minute, second = correct_value((dt.hour, dt.minute, dt.second), permitted_values)
    return datetime(dt.year, dt.month, dt.day, hour, minute, second, dt.microsecond, dt.tzinfo) + \
           (carry and timedelta(1) or timedelta(0))

def correct_value(value, permitted_values):

    """
    Correct the given (hour, minute, second) tuple 'value' according to the
    'permitted_values' details.
    """

    limits = 23, 59, 59

    corrected = []
    reset = False

    # Find invalid values and reset all following values.

    for v, values, limit in zip(value, permitted_values, limits):
        if reset:
            if values:
                v = values[0]
            else:
                v = 0

        elif values and v not in values:
            reset = True

        corrected.append(v)

    value = corrected
    corrected = []
    carry = 0

    # Find invalid values and update them to the next valid value, updating more
    # significant values if the next valid value is the first in the appropriate
    # series.

    for v, values, limit in zip(value, permitted_values, limits)[::-1]:
        if carry:
            v += 1
            if v > limit:
                if values:
                    v = values[0]
                else:
                    v = 0
                corrected.append(v)
                continue
            else:
                carry = 0

        if values:
            i = bisect_left(values, v)
            if i < len(values):
                v = values[i]
            else:
                v = values[0]
                carry = 1

        corrected.append(v)

    return [carry] + corrected[::-1]

# vim: tabstop=4 expandtab shiftwidth=4