1.1 --- a/imiptools/dates.py Mon Mar 23 00:39:26 2015 +0100
1.2 +++ b/imiptools/dates.py Tue Mar 24 01:10:28 2015 +0100
1.3 @@ -137,20 +137,61 @@
1.4 else:
1.5 return None
1.6
1.7 +def get_datetime_attributes(dt, tzid=None):
1.8 +
1.9 + "Return attributes for 'dt' and 'tzid'."
1.10 +
1.11 + if isinstance(dt, datetime):
1.12 + attr = {"VALUE" : "DATE-TIME"}
1.13 + if tzid:
1.14 + attr["TZID"] = tzid
1.15 + return attr
1.16 + else:
1.17 + return {"VALUE" : "DATE"}
1.18 +
1.19 + return {}
1.20 +
1.21 +def get_period_attributes(tzid=None):
1.22 +
1.23 + "Return attributes for 'tzid'."
1.24 +
1.25 + attr = {"VALUE" : "PERIOD"}
1.26 + if tzid:
1.27 + attr["TZID"] = tzid
1.28 + return attr
1.29 +
1.30 def get_datetime_item(dt, tzid=None):
1.31
1.32 "Return an iCalendar-compatible string and attributes for 'dt' and 'tzid'."
1.33
1.34 if not dt:
1.35 return None, None
1.36 + dt = to_timezone(dt, tzid)
1.37 value = format_datetime(dt)
1.38 - if isinstance(dt, datetime):
1.39 - attr = {"VALUE" : "DATE-TIME"}
1.40 - if tzid:
1.41 - attr["TZID"] = tzid
1.42 + attr = get_datetime_attributes(dt, tzid)
1.43 + return value, attr
1.44 +
1.45 +def get_period_item(start, end, tzid=None):
1.46 +
1.47 + """
1.48 + Return an iCalendar-compatible string and attributes for 'start', 'end' and
1.49 + 'tzid'.
1.50 + """
1.51 +
1.52 + start = start and to_timezone(start, tzid)
1.53 + end = end and to_timezone(end, tzid)
1.54 +
1.55 + start_value = start and format_datetime(start) or None
1.56 + end_value = end and format_datetime(end) or None
1.57 +
1.58 + if start and end:
1.59 + attr = get_period_attributes(tzid)
1.60 + return "%s/%s" % (start_value, end_value), attr
1.61 + elif start:
1.62 + attr = get_datetime_attributes(start, tzid)
1.63 + return start_value, attr
1.64 else:
1.65 - attr = {"VALUE" : "DATE"}
1.66 - return value, attr
1.67 + return None, None
1.68
1.69 def get_datetime(value, attr=None):
1.70
1.71 @@ -243,12 +284,24 @@
1.72 else:
1.73 return None
1.74
1.75 -def get_date(dt):
1.76 +def to_date(dt):
1.77
1.78 "Return the date of 'dt'."
1.79
1.80 return date(dt.year, dt.month, dt.day)
1.81
1.82 +def to_datetime(dt, tzid):
1.83 +
1.84 + """
1.85 + Return a datetime for 'dt', using the start of day for dates, and using the
1.86 + 'tzid' for the conversion.
1.87 + """
1.88 +
1.89 + if isinstance(dt, datetime):
1.90 + return dt
1.91 + else:
1.92 + return get_start_of_day(dt, tzid)
1.93 +
1.94 def get_start_of_day(dt, tzid):
1.95
1.96 """