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@195 | 22 | from datetime import date, datetime, timedelta |
paul@152 | 23 | from pytz import timezone, UnknownTimeZoneError |
paul@152 | 24 | import re |
paul@152 | 25 | |
paul@152 | 26 | # iCalendar date and datetime parsing (from DateSupport in MoinSupport). |
paul@152 | 27 | |
paul@152 | 28 | date_icalendar_regexp_str = ur'(?P<year>[0-9]{4})(?P<month>[0-9]{2})(?P<day>[0-9]{2})' |
paul@152 | 29 | datetime_icalendar_regexp_str = date_icalendar_regexp_str + \ |
paul@152 | 30 | ur'(?:' \ |
paul@152 | 31 | ur'T(?P<hour>[0-2][0-9])(?P<minute>[0-5][0-9])(?P<second>[0-6][0-9])' \ |
paul@152 | 32 | ur'(?P<utc>Z)?' \ |
paul@152 | 33 | ur')?' |
paul@152 | 34 | |
paul@152 | 35 | match_date_icalendar = re.compile(date_icalendar_regexp_str, re.UNICODE).match |
paul@152 | 36 | match_datetime_icalendar = re.compile(datetime_icalendar_regexp_str, re.UNICODE).match |
paul@152 | 37 | |
paul@152 | 38 | def to_utc_datetime(dt): |
paul@152 | 39 | if not dt: |
paul@152 | 40 | return None |
paul@152 | 41 | elif isinstance(dt, datetime): |
paul@157 | 42 | return to_timezone(dt, "UTC") |
paul@152 | 43 | else: |
paul@152 | 44 | return dt |
paul@152 | 45 | |
paul@152 | 46 | def to_timezone(dt, name): |
paul@152 | 47 | try: |
paul@152 | 48 | tz = name and timezone(name) or None |
paul@152 | 49 | except UnknownTimeZoneError: |
paul@152 | 50 | tz = None |
paul@157 | 51 | return to_tz(dt, tz) |
paul@157 | 52 | |
paul@157 | 53 | def to_tz(dt, tz): |
paul@232 | 54 | if tz is not None and isinstance(dt, datetime): |
paul@152 | 55 | if not dt.tzinfo: |
paul@152 | 56 | return tz.localize(dt) |
paul@152 | 57 | else: |
paul@152 | 58 | return dt.astimezone(tz) |
paul@152 | 59 | else: |
paul@152 | 60 | return dt |
paul@152 | 61 | |
paul@152 | 62 | def format_datetime(dt): |
paul@152 | 63 | if not dt: |
paul@152 | 64 | return None |
paul@152 | 65 | elif isinstance(dt, datetime): |
paul@152 | 66 | if dt.tzname() == "UTC": |
paul@152 | 67 | return dt.strftime("%Y%m%dT%H%M%SZ") |
paul@152 | 68 | else: |
paul@152 | 69 | return dt.strftime("%Y%m%dT%H%M%S") |
paul@152 | 70 | else: |
paul@152 | 71 | return dt.strftime("%Y%m%d") |
paul@152 | 72 | |
paul@152 | 73 | def get_datetime(value, attr=None): |
paul@152 | 74 | |
paul@152 | 75 | """ |
paul@152 | 76 | Return a datetime object from the given 'value' in iCalendar format, using |
paul@152 | 77 | the 'attr' mapping (if specified) to control the conversion. |
paul@152 | 78 | """ |
paul@152 | 79 | |
paul@152 | 80 | if not attr or attr.get("VALUE") in (None, "DATE-TIME"): |
paul@152 | 81 | m = match_datetime_icalendar(value) |
paul@152 | 82 | if m: |
paul@232 | 83 | year, month, day, hour, minute, second = map(m.group, [ |
paul@232 | 84 | "year", "month", "day", "hour", "minute", "second" |
paul@232 | 85 | ]) |
paul@152 | 86 | |
paul@232 | 87 | if hour and minute and second: |
paul@232 | 88 | dt = datetime( |
paul@232 | 89 | int(year), int(month), int(day), int(hour), int(minute), int(second) |
paul@232 | 90 | ) |
paul@152 | 91 | |
paul@232 | 92 | # Impose the indicated timezone. |
paul@232 | 93 | # NOTE: This needs an ambiguity policy for DST changes. |
paul@232 | 94 | |
paul@232 | 95 | return to_timezone(dt, m.group("utc") and "UTC" or attr and attr.get("TZID") or None) |
paul@152 | 96 | |
paul@152 | 97 | if not attr or attr.get("VALUE") == "DATE": |
paul@152 | 98 | m = match_date_icalendar(value) |
paul@152 | 99 | if m: |
paul@232 | 100 | year, month, day = map(m.group, ["year", "month", "day"]) |
paul@232 | 101 | return date(int(year), int(month), int(day)) |
paul@232 | 102 | |
paul@152 | 103 | return None |
paul@152 | 104 | |
paul@232 | 105 | def get_start_of_day(dt, tzid=None): |
paul@232 | 106 | return datetime(dt.year, dt.month, dt.day, 0, 0, tzinfo=(tzid and timezone(tzid) or dt.tzinfo)) |
paul@152 | 107 | |
paul@232 | 108 | def get_end_of_day(dt, tzid=None): |
paul@232 | 109 | return get_start_of_day(dt + timedelta(1), tzid) |
paul@195 | 110 | |
paul@195 | 111 | def ends_on_same_day(dt, end): |
paul@195 | 112 | return ( |
paul@195 | 113 | dt.date() == end.date() or |
paul@195 | 114 | end == get_end_of_day(dt) |
paul@195 | 115 | ) |
paul@195 | 116 | |
paul@222 | 117 | def get_timestamp(): |
paul@222 | 118 | return format_datetime(to_timezone(datetime.utcnow(), "UTC")) |
paul@222 | 119 | |
paul@152 | 120 | # vim: tabstop=4 expandtab shiftwidth=4 |