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@291 | 23 | from os.path import exists |
paul@152 | 24 | from pytz import timezone, UnknownTimeZoneError |
paul@152 | 25 | import re |
paul@152 | 26 | |
paul@152 | 27 | # iCalendar date and datetime parsing (from DateSupport in MoinSupport). |
paul@152 | 28 | |
paul@152 | 29 | date_icalendar_regexp_str = ur'(?P<year>[0-9]{4})(?P<month>[0-9]{2})(?P<day>[0-9]{2})' |
paul@152 | 30 | datetime_icalendar_regexp_str = date_icalendar_regexp_str + \ |
paul@152 | 31 | ur'(?:' \ |
paul@152 | 32 | ur'T(?P<hour>[0-2][0-9])(?P<minute>[0-5][0-9])(?P<second>[0-6][0-9])' \ |
paul@152 | 33 | ur'(?P<utc>Z)?' \ |
paul@152 | 34 | ur')?' |
paul@152 | 35 | |
paul@152 | 36 | match_date_icalendar = re.compile(date_icalendar_regexp_str, re.UNICODE).match |
paul@152 | 37 | match_datetime_icalendar = re.compile(datetime_icalendar_regexp_str, re.UNICODE).match |
paul@152 | 38 | |
paul@152 | 39 | def to_utc_datetime(dt): |
paul@247 | 40 | |
paul@247 | 41 | "Return a datetime corresponding to 'dt' in the UTC time zone." |
paul@247 | 42 | |
paul@152 | 43 | if not dt: |
paul@152 | 44 | return None |
paul@152 | 45 | elif isinstance(dt, datetime): |
paul@157 | 46 | return to_timezone(dt, "UTC") |
paul@152 | 47 | else: |
paul@152 | 48 | return dt |
paul@152 | 49 | |
paul@291 | 50 | def get_default_timezone(): |
paul@291 | 51 | |
paul@291 | 52 | "Return the system time regime." |
paul@291 | 53 | |
paul@291 | 54 | filename = "/etc/timezone" |
paul@291 | 55 | |
paul@291 | 56 | if exists(filename): |
paul@291 | 57 | f = open(filename) |
paul@291 | 58 | try: |
paul@291 | 59 | return f.read().strip() |
paul@291 | 60 | finally: |
paul@291 | 61 | f.close() |
paul@291 | 62 | else: |
paul@291 | 63 | return None |
paul@291 | 64 | |
paul@152 | 65 | def to_timezone(dt, name): |
paul@247 | 66 | |
paul@247 | 67 | """ |
paul@247 | 68 | Return a datetime corresponding to 'dt' in the time regime having the given |
paul@247 | 69 | 'name'. |
paul@247 | 70 | """ |
paul@247 | 71 | |
paul@152 | 72 | try: |
paul@152 | 73 | tz = name and timezone(name) or None |
paul@152 | 74 | except UnknownTimeZoneError: |
paul@152 | 75 | tz = None |
paul@157 | 76 | return to_tz(dt, tz) |
paul@157 | 77 | |
paul@157 | 78 | def to_tz(dt, tz): |
paul@247 | 79 | |
paul@247 | 80 | "Return a datetime corresponding to 'dt' employing the pytz.timezone 'tz'." |
paul@247 | 81 | |
paul@232 | 82 | if tz is not None and isinstance(dt, datetime): |
paul@152 | 83 | if not dt.tzinfo: |
paul@152 | 84 | return tz.localize(dt) |
paul@152 | 85 | else: |
paul@152 | 86 | return dt.astimezone(tz) |
paul@152 | 87 | else: |
paul@152 | 88 | return dt |
paul@152 | 89 | |
paul@152 | 90 | def format_datetime(dt): |
paul@247 | 91 | |
paul@247 | 92 | "Format 'dt' as an iCalendar-compatible string." |
paul@247 | 93 | |
paul@152 | 94 | if not dt: |
paul@152 | 95 | return None |
paul@152 | 96 | elif isinstance(dt, datetime): |
paul@152 | 97 | if dt.tzname() == "UTC": |
paul@152 | 98 | return dt.strftime("%Y%m%dT%H%M%SZ") |
paul@152 | 99 | else: |
paul@152 | 100 | return dt.strftime("%Y%m%dT%H%M%S") |
paul@152 | 101 | else: |
paul@152 | 102 | return dt.strftime("%Y%m%d") |
paul@152 | 103 | |
paul@285 | 104 | def format_time(dt): |
paul@285 | 105 | |
paul@285 | 106 | "Format the time portion of 'dt' as an iCalendar-compatible string." |
paul@285 | 107 | |
paul@285 | 108 | if not dt: |
paul@285 | 109 | return None |
paul@285 | 110 | elif isinstance(dt, datetime): |
paul@285 | 111 | if dt.tzname() == "UTC": |
paul@285 | 112 | return dt.strftime("%H%M%SZ") |
paul@285 | 113 | else: |
paul@285 | 114 | return dt.strftime("%H%M%S") |
paul@285 | 115 | else: |
paul@285 | 116 | return None |
paul@285 | 117 | |
paul@300 | 118 | def get_datetime_item(dt, tzid=None): |
paul@247 | 119 | |
paul@252 | 120 | "Return an iCalendar-compatible string and attributes for 'dt' and 'tzid'." |
paul@247 | 121 | |
paul@239 | 122 | if not dt: |
paul@239 | 123 | return None, None |
paul@239 | 124 | value = format_datetime(dt) |
paul@300 | 125 | if isinstance(dt, datetime): |
paul@300 | 126 | attr = {"VALUE" : "DATE-TIME"} |
paul@300 | 127 | if tzid: |
paul@300 | 128 | attr["TZID"] = tzid |
paul@300 | 129 | else: |
paul@300 | 130 | attr = {"VALUE" : "DATE"} |
paul@239 | 131 | return value, attr |
paul@239 | 132 | |
paul@152 | 133 | def get_datetime(value, attr=None): |
paul@152 | 134 | |
paul@152 | 135 | """ |
paul@152 | 136 | Return a datetime object from the given 'value' in iCalendar format, using |
paul@152 | 137 | the 'attr' mapping (if specified) to control the conversion. |
paul@152 | 138 | """ |
paul@152 | 139 | |
paul@295 | 140 | if not value: |
paul@295 | 141 | return None |
paul@295 | 142 | |
paul@285 | 143 | if len(value) > 9 and (not attr or attr.get("VALUE") in (None, "DATE-TIME")): |
paul@152 | 144 | m = match_datetime_icalendar(value) |
paul@152 | 145 | if m: |
paul@232 | 146 | year, month, day, hour, minute, second = map(m.group, [ |
paul@232 | 147 | "year", "month", "day", "hour", "minute", "second" |
paul@232 | 148 | ]) |
paul@152 | 149 | |
paul@232 | 150 | if hour and minute and second: |
paul@232 | 151 | dt = datetime( |
paul@232 | 152 | int(year), int(month), int(day), int(hour), int(minute), int(second) |
paul@232 | 153 | ) |
paul@152 | 154 | |
paul@232 | 155 | # Impose the indicated timezone. |
paul@232 | 156 | # NOTE: This needs an ambiguity policy for DST changes. |
paul@232 | 157 | |
paul@232 | 158 | return to_timezone(dt, m.group("utc") and "UTC" or attr and attr.get("TZID") or None) |
paul@152 | 159 | |
paul@285 | 160 | return None |
paul@285 | 161 | |
paul@239 | 162 | # Permit dates even if the VALUE is not set to DATE. |
paul@239 | 163 | |
paul@239 | 164 | if not attr or attr.get("VALUE") in (None, "DATE"): |
paul@152 | 165 | m = match_date_icalendar(value) |
paul@152 | 166 | if m: |
paul@232 | 167 | year, month, day = map(m.group, ["year", "month", "day"]) |
paul@232 | 168 | return date(int(year), int(month), int(day)) |
paul@232 | 169 | |
paul@152 | 170 | return None |
paul@152 | 171 | |
paul@285 | 172 | def get_date(dt): |
paul@285 | 173 | |
paul@285 | 174 | "Return the date of 'dt'." |
paul@285 | 175 | |
paul@285 | 176 | return date(dt.year, dt.month, dt.day) |
paul@285 | 177 | |
paul@244 | 178 | def get_start_of_day(dt, tzid): |
paul@245 | 179 | |
paul@245 | 180 | """ |
paul@245 | 181 | Get the start of the day in which 'dt' is positioned, using the given 'tzid' |
paul@245 | 182 | to obtain a datetime in the appropriate time zone. Where time zone |
paul@245 | 183 | transitions occur within a day, the zone of 'dt' may not be the eventual |
paul@245 | 184 | zone of the returned object. |
paul@245 | 185 | """ |
paul@245 | 186 | |
paul@244 | 187 | start = datetime(dt.year, dt.month, dt.day, 0, 0) |
paul@244 | 188 | return to_timezone(start, tzid) |
paul@152 | 189 | |
paul@244 | 190 | def get_end_of_day(dt, tzid): |
paul@245 | 191 | |
paul@245 | 192 | """ |
paul@245 | 193 | Get the end of the day in which 'dt' is positioned, using the given 'tzid' |
paul@245 | 194 | to obtain a datetime in the appropriate time zone. Where time zone |
paul@245 | 195 | transitions occur within a day, the zone of 'dt' may not be the eventual |
paul@245 | 196 | zone of the returned object. |
paul@245 | 197 | """ |
paul@245 | 198 | |
paul@232 | 199 | return get_start_of_day(dt + timedelta(1), tzid) |
paul@195 | 200 | |
paul@244 | 201 | def get_start_of_next_day(dt, tzid): |
paul@245 | 202 | |
paul@245 | 203 | """ |
paul@245 | 204 | Get the start of the day after the day in which 'dt' is positioned. This |
paul@245 | 205 | function is intended to extend either dates or datetimes to the end of a |
paul@245 | 206 | day for the purpose of generating a missing end date or datetime for an |
paul@245 | 207 | event. |
paul@245 | 208 | |
paul@245 | 209 | If 'dt' is a date and not a datetime, a plain date object for the next day |
paul@245 | 210 | will be returned. |
paul@245 | 211 | |
paul@245 | 212 | If 'dt' is a datetime, the given 'tzid' is used to obtain a datetime in the |
paul@245 | 213 | appropriate time zone. Where time zone transitions occur within a day, the |
paul@245 | 214 | zone of 'dt' may not be the eventual zone of the returned object. |
paul@245 | 215 | """ |
paul@245 | 216 | |
paul@239 | 217 | if isinstance(dt, datetime): |
paul@239 | 218 | return get_end_of_day(dt, tzid) |
paul@239 | 219 | else: |
paul@239 | 220 | return dt + timedelta(1) |
paul@239 | 221 | |
paul@244 | 222 | def ends_on_same_day(dt, end, tzid): |
paul@245 | 223 | |
paul@245 | 224 | """ |
paul@245 | 225 | Return whether 'dt' ends on the same day as 'end', testing the date |
paul@245 | 226 | components of 'dt' and 'end' against each other, but also testing whether |
paul@245 | 227 | 'end' is the actual end of the day in which 'dt' is positioned. |
paul@245 | 228 | |
paul@245 | 229 | Since time zone transitions may occur within a day, 'tzid' is required to |
paul@245 | 230 | determine the end of the day in which 'dt' is positioned, using the zone |
paul@245 | 231 | appropriate at that point in time, not necessarily the zone applying to |
paul@245 | 232 | 'dt'. |
paul@245 | 233 | """ |
paul@245 | 234 | |
paul@195 | 235 | return ( |
paul@195 | 236 | dt.date() == end.date() or |
paul@244 | 237 | end == get_end_of_day(dt, tzid) |
paul@195 | 238 | ) |
paul@195 | 239 | |
paul@222 | 240 | def get_timestamp(): |
paul@247 | 241 | |
paul@247 | 242 | "Return the current time as an iCalendar-compatible string." |
paul@247 | 243 | |
paul@222 | 244 | return format_datetime(to_timezone(datetime.utcnow(), "UTC")) |
paul@222 | 245 | |
paul@291 | 246 | def get_freebusy_period(start, end, tzid): |
paul@291 | 247 | |
paul@291 | 248 | """ |
paul@291 | 249 | For the given 'start' datetime, together with the given 'end' datetime, and |
paul@291 | 250 | given a 'tzid' either from the datetimes or provided for the user, return a |
paul@291 | 251 | (start, end) tuple containing datetimes in the UTC time zone, where dates |
paul@291 | 252 | are converted to points in time so that each day has a specific start and |
paul@291 | 253 | end point defined in UTC. |
paul@291 | 254 | """ |
paul@291 | 255 | |
paul@291 | 256 | start = to_utc_datetime_only(start, tzid) |
paul@291 | 257 | end = to_utc_datetime_only(end, tzid) |
paul@291 | 258 | return start, end |
paul@291 | 259 | |
paul@291 | 260 | def to_utc_datetime_only(dt, tzid): |
paul@291 | 261 | |
paul@291 | 262 | """ |
paul@291 | 263 | Return the datetime 'dt' as a point in time in the UTC time zone, given the |
paul@291 | 264 | 'tzid' defined for the datetime. Where 'dt' is a date, the start of the |
paul@291 | 265 | indicated day is returned, defined in UTC. |
paul@291 | 266 | """ |
paul@291 | 267 | |
paul@291 | 268 | if not isinstance(dt, datetime): |
paul@291 | 269 | return to_timezone(get_start_of_day(dt, tzid), "UTC") |
paul@291 | 270 | else: |
paul@291 | 271 | return to_timezone(dt, "UTC") |
paul@291 | 272 | |
paul@152 | 273 | # vim: tabstop=4 expandtab shiftwidth=4 |