1.1 --- a/imiptools/data.py Mon Mar 23 00:39:26 2015 +0100
1.2 +++ b/imiptools/data.py Tue Mar 24 01:10:28 2015 +0100
1.3 @@ -23,8 +23,8 @@
1.4 from datetime import datetime, timedelta
1.5 from email.mime.text import MIMEText
1.6 from imiptools.dates import format_datetime, get_datetime, get_duration, \
1.7 - get_freebusy_period, get_period, to_timezone, \
1.8 - to_utc_datetime
1.9 + get_freebusy_period, get_period, to_datetime, \
1.10 + to_timezone, to_utc_datetime
1.11 from imiptools.period import period_overlaps
1.12 from pytz import timezone
1.13 from vCalendar import iterwrite, parse, ParseError, to_dict, to_node
1.14 @@ -426,8 +426,12 @@
1.15 origin_value = origin and ("RDATE",) or ()
1.16 for rdate in rdates:
1.17 if isinstance(rdate, tuple):
1.18 - periods.add(rdate + origin_value)
1.19 + start, end = rdate
1.20 + start = to_datetime(start, tzid)
1.21 + end = to_datetime(end, tzid)
1.22 + periods.add((start, end) + origin_value)
1.23 else:
1.24 + rdate = to_datetime(rdate, tzid)
1.25 periods.add((rdate, rdate + duration) + origin_value)
1.26
1.27 # Return a sorted list of the periods.
1.28 @@ -442,8 +446,12 @@
1.29 if exdates:
1.30 for exdate in exdates:
1.31 if isinstance(exdate, tuple):
1.32 - period = exdate
1.33 + start, end = exdate
1.34 + start = to_datetime(start, tzid)
1.35 + end = to_datetime(end, tzid)
1.36 + period = start, end
1.37 else:
1.38 + exdate = to_datetime(exdate, tzid)
1.39 period = (exdate, exdate + duration)
1.40 i = bisect_left(periods, period)
1.41 while i < len(periods) and periods[i][:2] == period:
2.1 --- a/imiptools/dates.py Mon Mar 23 00:39:26 2015 +0100
2.2 +++ b/imiptools/dates.py Tue Mar 24 01:10:28 2015 +0100
2.3 @@ -137,20 +137,61 @@
2.4 else:
2.5 return None
2.6
2.7 +def get_datetime_attributes(dt, tzid=None):
2.8 +
2.9 + "Return attributes for 'dt' and 'tzid'."
2.10 +
2.11 + if isinstance(dt, datetime):
2.12 + attr = {"VALUE" : "DATE-TIME"}
2.13 + if tzid:
2.14 + attr["TZID"] = tzid
2.15 + return attr
2.16 + else:
2.17 + return {"VALUE" : "DATE"}
2.18 +
2.19 + return {}
2.20 +
2.21 +def get_period_attributes(tzid=None):
2.22 +
2.23 + "Return attributes for 'tzid'."
2.24 +
2.25 + attr = {"VALUE" : "PERIOD"}
2.26 + if tzid:
2.27 + attr["TZID"] = tzid
2.28 + return attr
2.29 +
2.30 def get_datetime_item(dt, tzid=None):
2.31
2.32 "Return an iCalendar-compatible string and attributes for 'dt' and 'tzid'."
2.33
2.34 if not dt:
2.35 return None, None
2.36 + dt = to_timezone(dt, tzid)
2.37 value = format_datetime(dt)
2.38 - if isinstance(dt, datetime):
2.39 - attr = {"VALUE" : "DATE-TIME"}
2.40 - if tzid:
2.41 - attr["TZID"] = tzid
2.42 + attr = get_datetime_attributes(dt, tzid)
2.43 + return value, attr
2.44 +
2.45 +def get_period_item(start, end, tzid=None):
2.46 +
2.47 + """
2.48 + Return an iCalendar-compatible string and attributes for 'start', 'end' and
2.49 + 'tzid'.
2.50 + """
2.51 +
2.52 + start = start and to_timezone(start, tzid)
2.53 + end = end and to_timezone(end, tzid)
2.54 +
2.55 + start_value = start and format_datetime(start) or None
2.56 + end_value = end and format_datetime(end) or None
2.57 +
2.58 + if start and end:
2.59 + attr = get_period_attributes(tzid)
2.60 + return "%s/%s" % (start_value, end_value), attr
2.61 + elif start:
2.62 + attr = get_datetime_attributes(start, tzid)
2.63 + return start_value, attr
2.64 else:
2.65 - attr = {"VALUE" : "DATE"}
2.66 - return value, attr
2.67 + return None, None
2.68
2.69 def get_datetime(value, attr=None):
2.70
2.71 @@ -243,12 +284,24 @@
2.72 else:
2.73 return None
2.74
2.75 -def get_date(dt):
2.76 +def to_date(dt):
2.77
2.78 "Return the date of 'dt'."
2.79
2.80 return date(dt.year, dt.month, dt.day)
2.81
2.82 +def to_datetime(dt, tzid):
2.83 +
2.84 + """
2.85 + Return a datetime for 'dt', using the start of day for dates, and using the
2.86 + 'tzid' for the conversion.
2.87 + """
2.88 +
2.89 + if isinstance(dt, datetime):
2.90 + return dt
2.91 + else:
2.92 + return get_start_of_day(dt, tzid)
2.93 +
2.94 def get_start_of_day(dt, tzid):
2.95
2.96 """