1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/imiptools/dates.py Tue Jan 13 22:39:53 2015 +0100
1.3 @@ -0,0 +1,101 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Date processing functions.
1.8 +
1.9 +Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This program is free software; you can redistribute it and/or modify it under
1.12 +the terms of the GNU General Public License as published by the Free Software
1.13 +Foundation; either version 3 of the License, or (at your option) any later
1.14 +version.
1.15 +
1.16 +This program is distributed in the hope that it will be useful, but WITHOUT
1.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.19 +details.
1.20 +
1.21 +You should have received a copy of the GNU General Public License along with
1.22 +this program. If not, see <http://www.gnu.org/licenses/>.
1.23 +"""
1.24 +
1.25 +from datetime import date, datetime
1.26 +from pytz import timezone, UnknownTimeZoneError
1.27 +import re
1.28 +
1.29 +# iCalendar date and datetime parsing (from DateSupport in MoinSupport).
1.30 +
1.31 +date_icalendar_regexp_str = ur'(?P<year>[0-9]{4})(?P<month>[0-9]{2})(?P<day>[0-9]{2})'
1.32 +datetime_icalendar_regexp_str = date_icalendar_regexp_str + \
1.33 + ur'(?:' \
1.34 + ur'T(?P<hour>[0-2][0-9])(?P<minute>[0-5][0-9])(?P<second>[0-6][0-9])' \
1.35 + ur'(?P<utc>Z)?' \
1.36 + ur')?'
1.37 +
1.38 +match_date_icalendar = re.compile(date_icalendar_regexp_str, re.UNICODE).match
1.39 +match_datetime_icalendar = re.compile(datetime_icalendar_regexp_str, re.UNICODE).match
1.40 +
1.41 +def to_utc_datetime(dt):
1.42 + if not dt:
1.43 + return None
1.44 + elif isinstance(dt, datetime):
1.45 + return dt.astimezone(timezone("UTC"))
1.46 + else:
1.47 + return dt
1.48 +
1.49 +def to_timezone(dt, name):
1.50 + try:
1.51 + tz = name and timezone(name) or None
1.52 + except UnknownTimeZoneError:
1.53 + tz = None
1.54 + if tz is not None:
1.55 + if not dt.tzinfo:
1.56 + return tz.localize(dt)
1.57 + else:
1.58 + return dt.astimezone(tz)
1.59 + else:
1.60 + return dt
1.61 +
1.62 +def format_datetime(dt):
1.63 + if not dt:
1.64 + return None
1.65 + elif isinstance(dt, datetime):
1.66 + if dt.tzname() == "UTC":
1.67 + return dt.strftime("%Y%m%dT%H%M%SZ")
1.68 + else:
1.69 + return dt.strftime("%Y%m%dT%H%M%S")
1.70 + else:
1.71 + return dt.strftime("%Y%m%d")
1.72 +
1.73 +def get_datetime(value, attr=None):
1.74 +
1.75 + """
1.76 + Return a datetime object from the given 'value' in iCalendar format, using
1.77 + the 'attr' mapping (if specified) to control the conversion.
1.78 + """
1.79 +
1.80 + if not attr or attr.get("VALUE") in (None, "DATE-TIME"):
1.81 + m = match_datetime_icalendar(value)
1.82 + if m:
1.83 + dt = datetime(
1.84 + int(m.group("year")), int(m.group("month")), int(m.group("day")),
1.85 + int(m.group("hour")), int(m.group("minute")), int(m.group("second"))
1.86 + )
1.87 +
1.88 + # Impose the indicated timezone.
1.89 + # NOTE: This needs an ambiguity policy for DST changes.
1.90 +
1.91 + return to_timezone(dt, m.group("utc") and "UTC" or attr and attr.get("TZID") or None)
1.92 +
1.93 + if not attr or attr.get("VALUE") == "DATE":
1.94 + m = match_date_icalendar(value)
1.95 + if m:
1.96 + return date(
1.97 + int(m.group("year")), int(m.group("month")), int(m.group("day"))
1.98 + )
1.99 + return None
1.100 +
1.101 +def get_start_of_day(dt):
1.102 + return datetime(dt.year, dt.month, dt.day, 0, 0, tzinfo=dt.tzinfo)
1.103 +
1.104 +# vim: tabstop=4 expandtab shiftwidth=4