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@247 | 39 | |
paul@247 | 40 | "Return a datetime corresponding to 'dt' in the UTC time zone." |
paul@247 | 41 | |
paul@152 | 42 | if not dt: |
paul@152 | 43 | return None |
paul@152 | 44 | elif isinstance(dt, datetime): |
paul@157 | 45 | return to_timezone(dt, "UTC") |
paul@152 | 46 | else: |
paul@152 | 47 | return dt |
paul@152 | 48 | |
paul@152 | 49 | def to_timezone(dt, name): |
paul@247 | 50 | |
paul@247 | 51 | """ |
paul@247 | 52 | Return a datetime corresponding to 'dt' in the time regime having the given |
paul@247 | 53 | 'name'. |
paul@247 | 54 | """ |
paul@247 | 55 | |
paul@152 | 56 | try: |
paul@152 | 57 | tz = name and timezone(name) or None |
paul@152 | 58 | except UnknownTimeZoneError: |
paul@152 | 59 | tz = None |
paul@157 | 60 | return to_tz(dt, tz) |
paul@157 | 61 | |
paul@157 | 62 | def to_tz(dt, tz): |
paul@247 | 63 | |
paul@247 | 64 | "Return a datetime corresponding to 'dt' employing the pytz.timezone 'tz'." |
paul@247 | 65 | |
paul@232 | 66 | if tz is not None and isinstance(dt, datetime): |
paul@152 | 67 | if not dt.tzinfo: |
paul@152 | 68 | return tz.localize(dt) |
paul@152 | 69 | else: |
paul@152 | 70 | return dt.astimezone(tz) |
paul@152 | 71 | else: |
paul@152 | 72 | return dt |
paul@152 | 73 | |
paul@152 | 74 | def format_datetime(dt): |
paul@247 | 75 | |
paul@247 | 76 | "Format 'dt' as an iCalendar-compatible string." |
paul@247 | 77 | |
paul@152 | 78 | if not dt: |
paul@152 | 79 | return None |
paul@152 | 80 | elif isinstance(dt, datetime): |
paul@152 | 81 | if dt.tzname() == "UTC": |
paul@152 | 82 | return dt.strftime("%Y%m%dT%H%M%SZ") |
paul@152 | 83 | else: |
paul@152 | 84 | return dt.strftime("%Y%m%dT%H%M%S") |
paul@152 | 85 | else: |
paul@152 | 86 | return dt.strftime("%Y%m%d") |
paul@152 | 87 | |
paul@252 | 88 | def get_datetime_item(dt, tzid): |
paul@247 | 89 | |
paul@252 | 90 | "Return an iCalendar-compatible string and attributes for 'dt' and 'tzid'." |
paul@247 | 91 | |
paul@239 | 92 | if not dt: |
paul@239 | 93 | return None, None |
paul@239 | 94 | value = format_datetime(dt) |
paul@252 | 95 | attr = isinstance(dt, datetime) and {"TZID" : tzid, "VALUE" : "DATE-TIME"} or {"VALUE" : "DATE"} |
paul@239 | 96 | return value, attr |
paul@239 | 97 | |
paul@152 | 98 | def get_datetime(value, attr=None): |
paul@152 | 99 | |
paul@152 | 100 | """ |
paul@152 | 101 | Return a datetime object from the given 'value' in iCalendar format, using |
paul@152 | 102 | the 'attr' mapping (if specified) to control the conversion. |
paul@152 | 103 | """ |
paul@152 | 104 | |
paul@152 | 105 | if not attr or attr.get("VALUE") in (None, "DATE-TIME"): |
paul@152 | 106 | m = match_datetime_icalendar(value) |
paul@152 | 107 | if m: |
paul@232 | 108 | year, month, day, hour, minute, second = map(m.group, [ |
paul@232 | 109 | "year", "month", "day", "hour", "minute", "second" |
paul@232 | 110 | ]) |
paul@152 | 111 | |
paul@232 | 112 | if hour and minute and second: |
paul@232 | 113 | dt = datetime( |
paul@232 | 114 | int(year), int(month), int(day), int(hour), int(minute), int(second) |
paul@232 | 115 | ) |
paul@152 | 116 | |
paul@232 | 117 | # Impose the indicated timezone. |
paul@232 | 118 | # NOTE: This needs an ambiguity policy for DST changes. |
paul@232 | 119 | |
paul@232 | 120 | return to_timezone(dt, m.group("utc") and "UTC" or attr and attr.get("TZID") or None) |
paul@152 | 121 | |
paul@239 | 122 | # Permit dates even if the VALUE is not set to DATE. |
paul@239 | 123 | |
paul@239 | 124 | if not attr or attr.get("VALUE") in (None, "DATE"): |
paul@152 | 125 | m = match_date_icalendar(value) |
paul@152 | 126 | if m: |
paul@232 | 127 | year, month, day = map(m.group, ["year", "month", "day"]) |
paul@232 | 128 | return date(int(year), int(month), int(day)) |
paul@232 | 129 | |
paul@152 | 130 | return None |
paul@152 | 131 | |
paul@244 | 132 | def get_start_of_day(dt, tzid): |
paul@245 | 133 | |
paul@245 | 134 | """ |
paul@245 | 135 | Get the start of the day in which 'dt' is positioned, using the given 'tzid' |
paul@245 | 136 | to obtain a datetime in the appropriate time zone. Where time zone |
paul@245 | 137 | transitions occur within a day, the zone of 'dt' may not be the eventual |
paul@245 | 138 | zone of the returned object. |
paul@245 | 139 | """ |
paul@245 | 140 | |
paul@244 | 141 | start = datetime(dt.year, dt.month, dt.day, 0, 0) |
paul@244 | 142 | return to_timezone(start, tzid) |
paul@152 | 143 | |
paul@244 | 144 | def get_end_of_day(dt, tzid): |
paul@245 | 145 | |
paul@245 | 146 | """ |
paul@245 | 147 | Get the end of the day in which 'dt' is positioned, using the given 'tzid' |
paul@245 | 148 | to obtain a datetime in the appropriate time zone. Where time zone |
paul@245 | 149 | transitions occur within a day, the zone of 'dt' may not be the eventual |
paul@245 | 150 | zone of the returned object. |
paul@245 | 151 | """ |
paul@245 | 152 | |
paul@232 | 153 | return get_start_of_day(dt + timedelta(1), tzid) |
paul@195 | 154 | |
paul@244 | 155 | def get_start_of_next_day(dt, tzid): |
paul@245 | 156 | |
paul@245 | 157 | """ |
paul@245 | 158 | Get the start of the day after the day in which 'dt' is positioned. This |
paul@245 | 159 | function is intended to extend either dates or datetimes to the end of a |
paul@245 | 160 | day for the purpose of generating a missing end date or datetime for an |
paul@245 | 161 | event. |
paul@245 | 162 | |
paul@245 | 163 | If 'dt' is a date and not a datetime, a plain date object for the next day |
paul@245 | 164 | will be returned. |
paul@245 | 165 | |
paul@245 | 166 | If 'dt' is a datetime, the given 'tzid' is used to obtain a datetime in the |
paul@245 | 167 | appropriate time zone. Where time zone transitions occur within a day, the |
paul@245 | 168 | zone of 'dt' may not be the eventual zone of the returned object. |
paul@245 | 169 | """ |
paul@245 | 170 | |
paul@239 | 171 | if isinstance(dt, datetime): |
paul@239 | 172 | return get_end_of_day(dt, tzid) |
paul@239 | 173 | else: |
paul@239 | 174 | return dt + timedelta(1) |
paul@239 | 175 | |
paul@244 | 176 | def ends_on_same_day(dt, end, tzid): |
paul@245 | 177 | |
paul@245 | 178 | """ |
paul@245 | 179 | Return whether 'dt' ends on the same day as 'end', testing the date |
paul@245 | 180 | components of 'dt' and 'end' against each other, but also testing whether |
paul@245 | 181 | 'end' is the actual end of the day in which 'dt' is positioned. |
paul@245 | 182 | |
paul@245 | 183 | Since time zone transitions may occur within a day, 'tzid' is required to |
paul@245 | 184 | determine the end of the day in which 'dt' is positioned, using the zone |
paul@245 | 185 | appropriate at that point in time, not necessarily the zone applying to |
paul@245 | 186 | 'dt'. |
paul@245 | 187 | """ |
paul@245 | 188 | |
paul@195 | 189 | return ( |
paul@195 | 190 | dt.date() == end.date() or |
paul@244 | 191 | end == get_end_of_day(dt, tzid) |
paul@195 | 192 | ) |
paul@195 | 193 | |
paul@222 | 194 | def get_timestamp(): |
paul@247 | 195 | |
paul@247 | 196 | "Return the current time as an iCalendar-compatible string." |
paul@247 | 197 | |
paul@222 | 198 | return format_datetime(to_timezone(datetime.utcnow(), "UTC")) |
paul@222 | 199 | |
paul@248 | 200 | def next_date(s): |
paul@248 | 201 | |
paul@248 | 202 | """ |
paul@248 | 203 | Return an iCalendar-compatible string indicating the next date after the one |
paul@248 | 204 | provided by 's'. |
paul@248 | 205 | """ |
paul@248 | 206 | |
paul@248 | 207 | return format_datetime(get_datetime(s) + timedelta(1)) |
paul@248 | 208 | |
paul@152 | 209 | # vim: tabstop=4 expandtab shiftwidth=4 |