1.1 --- a/imiptools/content.py Tue Jan 13 00:18:53 2015 +0100
1.2 +++ b/imiptools/content.py Tue Jan 13 22:39:53 2015 +0100
1.3 @@ -20,33 +20,21 @@
1.4 this program. If not, see <http://www.gnu.org/licenses/>.
1.5 """
1.6
1.7 -from datetime import date, datetime, timedelta
1.8 +from datetime import datetime, timedelta
1.9 from email.mime.text import MIMEText
1.10 +from imiptools.dates import *
1.11 from imiptools.period import have_conflict, insert_period, remove_period
1.12 -from pytz import timezone, UnknownTimeZoneError
1.13 +from pytz import timezone
1.14 from vCalendar import parse, ParseError, to_dict
1.15 from vRecurrence import get_parameters, get_rule
1.16 import email.utils
1.17 import imip_store
1.18 -import re
1.19
1.20 try:
1.21 from cStringIO import StringIO
1.22 except ImportError:
1.23 from StringIO import StringIO
1.24
1.25 -# iCalendar date and datetime parsing (from DateSupport in MoinSupport).
1.26 -
1.27 -date_icalendar_regexp_str = ur'(?P<year>[0-9]{4})(?P<month>[0-9]{2})(?P<day>[0-9]{2})'
1.28 -datetime_icalendar_regexp_str = date_icalendar_regexp_str + \
1.29 - ur'(?:' \
1.30 - ur'T(?P<hour>[0-2][0-9])(?P<minute>[0-5][0-9])(?P<second>[0-6][0-9])' \
1.31 - ur'(?P<utc>Z)?' \
1.32 - ur')?'
1.33 -
1.34 -match_date_icalendar = re.compile(date_icalendar_regexp_str, re.UNICODE).match
1.35 -match_datetime_icalendar = re.compile(datetime_icalendar_regexp_str, re.UNICODE).match
1.36 -
1.37 # Content interpretation.
1.38
1.39 def get_items(d, name, all=True):
1.40 @@ -101,38 +89,6 @@
1.41 dt = get_datetime(value, attr)
1.42 return to_utc_datetime(dt)
1.43
1.44 -def to_utc_datetime(dt):
1.45 - if not dt:
1.46 - return None
1.47 - elif isinstance(dt, datetime):
1.48 - return dt.astimezone(timezone("UTC"))
1.49 - else:
1.50 - return dt
1.51 -
1.52 -def to_timezone(dt, name):
1.53 - try:
1.54 - tz = name and timezone(name) or None
1.55 - except UnknownTimeZoneError:
1.56 - tz = None
1.57 - if tz is not None:
1.58 - if not dt.tzinfo:
1.59 - return tz.localize(dt)
1.60 - else:
1.61 - return dt.astimezone(tz)
1.62 - else:
1.63 - return dt
1.64 -
1.65 -def format_datetime(dt):
1.66 - if not dt:
1.67 - return None
1.68 - elif isinstance(dt, datetime):
1.69 - if dt.tzname() == "UTC":
1.70 - return dt.strftime("%Y%m%dT%H%M%SZ")
1.71 - else:
1.72 - return dt.strftime("%Y%m%dT%H%M%S")
1.73 - else:
1.74 - return dt.strftime("%Y%m%d")
1.75 -
1.76 def get_addresses(values):
1.77 return [address for name, address in email.utils.getaddresses(values)]
1.78
1.79 @@ -151,34 +107,6 @@
1.80 def uri_items(items):
1.81 return [(get_uri(value), attr) for value, attr in items]
1.82
1.83 -def get_datetime(value, attr=None):
1.84 -
1.85 - """
1.86 - Return a datetime object from the given 'value' in iCalendar format, using
1.87 - the 'attr' mapping (if specified) to control the conversion.
1.88 - """
1.89 -
1.90 - if not attr or attr.get("VALUE") in (None, "DATE-TIME"):
1.91 - m = match_datetime_icalendar(value)
1.92 - if m:
1.93 - dt = datetime(
1.94 - int(m.group("year")), int(m.group("month")), int(m.group("day")),
1.95 - int(m.group("hour")), int(m.group("minute")), int(m.group("second"))
1.96 - )
1.97 -
1.98 - # Impose the indicated timezone.
1.99 - # NOTE: This needs an ambiguity policy for DST changes.
1.100 -
1.101 - return to_timezone(dt, m.group("utc") and "UTC" or attr and attr.get("TZID") or None)
1.102 -
1.103 - if not attr or attr.get("VALUE") == "DATE":
1.104 - m = match_date_icalendar(value)
1.105 - if m:
1.106 - return date(
1.107 - int(m.group("year")), int(m.group("month")), int(m.group("day"))
1.108 - )
1.109 - return None
1.110 -
1.111 # NOTE: Need to expose the 100 day window for recurring events in the
1.112 # NOTE: configuration.
1.113
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/imiptools/dates.py Tue Jan 13 22:39:53 2015 +0100
2.3 @@ -0,0 +1,101 @@
2.4 +#!/usr/bin/env python
2.5 +
2.6 +"""
2.7 +Date processing functions.
2.8 +
2.9 +Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk>
2.10 +
2.11 +This program is free software; you can redistribute it and/or modify it under
2.12 +the terms of the GNU General Public License as published by the Free Software
2.13 +Foundation; either version 3 of the License, or (at your option) any later
2.14 +version.
2.15 +
2.16 +This program is distributed in the hope that it will be useful, but WITHOUT
2.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
2.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
2.19 +details.
2.20 +
2.21 +You should have received a copy of the GNU General Public License along with
2.22 +this program. If not, see <http://www.gnu.org/licenses/>.
2.23 +"""
2.24 +
2.25 +from datetime import date, datetime
2.26 +from pytz import timezone, UnknownTimeZoneError
2.27 +import re
2.28 +
2.29 +# iCalendar date and datetime parsing (from DateSupport in MoinSupport).
2.30 +
2.31 +date_icalendar_regexp_str = ur'(?P<year>[0-9]{4})(?P<month>[0-9]{2})(?P<day>[0-9]{2})'
2.32 +datetime_icalendar_regexp_str = date_icalendar_regexp_str + \
2.33 + ur'(?:' \
2.34 + ur'T(?P<hour>[0-2][0-9])(?P<minute>[0-5][0-9])(?P<second>[0-6][0-9])' \
2.35 + ur'(?P<utc>Z)?' \
2.36 + ur')?'
2.37 +
2.38 +match_date_icalendar = re.compile(date_icalendar_regexp_str, re.UNICODE).match
2.39 +match_datetime_icalendar = re.compile(datetime_icalendar_regexp_str, re.UNICODE).match
2.40 +
2.41 +def to_utc_datetime(dt):
2.42 + if not dt:
2.43 + return None
2.44 + elif isinstance(dt, datetime):
2.45 + return dt.astimezone(timezone("UTC"))
2.46 + else:
2.47 + return dt
2.48 +
2.49 +def to_timezone(dt, name):
2.50 + try:
2.51 + tz = name and timezone(name) or None
2.52 + except UnknownTimeZoneError:
2.53 + tz = None
2.54 + if tz is not None:
2.55 + if not dt.tzinfo:
2.56 + return tz.localize(dt)
2.57 + else:
2.58 + return dt.astimezone(tz)
2.59 + else:
2.60 + return dt
2.61 +
2.62 +def format_datetime(dt):
2.63 + if not dt:
2.64 + return None
2.65 + elif isinstance(dt, datetime):
2.66 + if dt.tzname() == "UTC":
2.67 + return dt.strftime("%Y%m%dT%H%M%SZ")
2.68 + else:
2.69 + return dt.strftime("%Y%m%dT%H%M%S")
2.70 + else:
2.71 + return dt.strftime("%Y%m%d")
2.72 +
2.73 +def get_datetime(value, attr=None):
2.74 +
2.75 + """
2.76 + Return a datetime object from the given 'value' in iCalendar format, using
2.77 + the 'attr' mapping (if specified) to control the conversion.
2.78 + """
2.79 +
2.80 + if not attr or attr.get("VALUE") in (None, "DATE-TIME"):
2.81 + m = match_datetime_icalendar(value)
2.82 + if m:
2.83 + dt = datetime(
2.84 + int(m.group("year")), int(m.group("month")), int(m.group("day")),
2.85 + int(m.group("hour")), int(m.group("minute")), int(m.group("second"))
2.86 + )
2.87 +
2.88 + # Impose the indicated timezone.
2.89 + # NOTE: This needs an ambiguity policy for DST changes.
2.90 +
2.91 + return to_timezone(dt, m.group("utc") and "UTC" or attr and attr.get("TZID") or None)
2.92 +
2.93 + if not attr or attr.get("VALUE") == "DATE":
2.94 + m = match_date_icalendar(value)
2.95 + if m:
2.96 + return date(
2.97 + int(m.group("year")), int(m.group("month")), int(m.group("day"))
2.98 + )
2.99 + return None
2.100 +
2.101 +def get_start_of_day(dt):
2.102 + return datetime(dt.year, dt.month, dt.day, 0, 0, tzinfo=dt.tzinfo)
2.103 +
2.104 +# vim: tabstop=4 expandtab shiftwidth=4