1.1 --- a/imiptools/data.py Tue Jul 28 19:27:27 2015 +0200
1.2 +++ b/imiptools/data.py Wed Jul 29 00:18:59 2015 +0200
1.3 @@ -22,9 +22,9 @@
1.4 from bisect import bisect_left
1.5 from datetime import date, datetime, timedelta
1.6 from email.mime.text import MIMEText
1.7 -from imiptools.dates import format_datetime, get_datetime, get_duration, \
1.8 - get_freebusy_period, get_period, get_tzid, \
1.9 - to_timezone, to_utc_datetime
1.10 +from imiptools.dates import format_datetime, get_datetime, get_datetime_tzid, \
1.11 + get_duration, get_freebusy_period, get_period, \
1.12 + get_tzid, to_timezone, to_utc_datetime
1.13 from imiptools.period import Period, RecurringPeriod, period_overlaps
1.14 from vCalendar import iterwrite, parse, ParseError, to_dict, to_node
1.15 from vRecurrence import get_parameters, get_rule
1.16 @@ -367,8 +367,9 @@
1.17 else:
1.18 value, attr = t
1.19 dt = get_datetime(value, attr)
1.20 - if dt.tzname() == "UTC":
1.21 - attr["TZID"] = "UTC"
1.22 + tzid = get_datetime_tzid(dt)
1.23 + if tzid:
1.24 + attr["TZID"] = tzid
1.25 return dt, attr
1.26
1.27 # Conversion functions.
2.1 --- a/imiptools/dates.py Tue Jul 28 19:27:27 2015 +0200
2.2 +++ b/imiptools/dates.py Wed Jul 29 00:18:59 2015 +0200
2.3 @@ -260,6 +260,19 @@
2.4 else:
2.5 return dt + timedelta(1)
2.6
2.7 +def get_datetime_tzid(dt):
2.8 +
2.9 + "Return the time zone identifier from 'dt' or None if unknown."
2.10 +
2.11 + if not isinstance(dt, datetime):
2.12 + return None
2.13 + elif dt.tzname() == "UTC":
2.14 + return "UTC"
2.15 + elif dt.tzinfo and hasattr(dt.tzinfo, "zone"):
2.16 + return dt.tzinfo.zone
2.17 + else:
2.18 + return None
2.19 +
2.20 def to_date(dt):
2.21
2.22 "Return the date of 'dt'."
2.23 @@ -295,15 +308,15 @@
2.24 else:
2.25 return dt
2.26
2.27 -def to_timezone(dt, name):
2.28 +def to_timezone(dt, tzid):
2.29
2.30 """
2.31 Return a datetime corresponding to 'dt' in the time regime having the given
2.32 - 'name'.
2.33 + 'tzid'.
2.34 """
2.35
2.36 try:
2.37 - tz = name and timezone(name) or None
2.38 + tz = tzid and timezone(tzid) or None
2.39 except UnknownTimeZoneError:
2.40 tz = None
2.41 return to_tz(dt, tz)
2.42 @@ -328,6 +341,9 @@
2.43 (start, end) tuple containing datetimes in the UTC time zone, where dates
2.44 are converted to points in time so that each day has a specific start and
2.45 end point defined in UTC.
2.46 +
2.47 + Here, 'tzid' is used to "project" dates into a time zone of interest before
2.48 + then being converted into UTC start and end datetimes.
2.49 """
2.50
2.51 start = to_utc_datetime(start, tzid)
2.52 @@ -362,14 +378,16 @@
2.53
2.54 def get_datetime_attributes(dt, tzid=None):
2.55
2.56 - "Return attributes for 'dt' and 'tzid'."
2.57 + """
2.58 + Return attributes for the 'dt' date or datetime object with 'tzid'
2.59 + indicating the time zone if not otherwise defined.
2.60 + """
2.61
2.62 if isinstance(dt, datetime):
2.63 attr = {"VALUE" : "DATE-TIME"}
2.64 + tzid = get_datetime_tzid(dt) or tzid
2.65 if tzid:
2.66 attr["TZID"] = tzid
2.67 - elif dt.tzname() == "UTC":
2.68 - attr["TZID"] = "UTC"
2.69 return attr
2.70 else:
2.71 return {"VALUE" : "DATE"}
2.72 @@ -380,12 +398,13 @@
2.73
2.74 """
2.75 Return an iCalendar-compatible string and attributes for 'dt' using any
2.76 - specified 'tzid' to assert a particular time zone.
2.77 + specified 'tzid' to assert a particular time zone if not otherwise defined.
2.78 """
2.79
2.80 if not dt:
2.81 return None, None
2.82 - dt = to_timezone(dt, tzid)
2.83 + if not get_datetime_tzid(dt):
2.84 + dt = to_timezone(dt, tzid)
2.85 value = format_datetime(dt)
2.86 attr = get_datetime_attributes(dt, tzid)
2.87 return value, attr
3.1 --- a/imiptools/period.py Tue Jul 28 19:27:27 2015 +0200
3.2 +++ b/imiptools/period.py Wed Jul 29 00:18:59 2015 +0200
3.3 @@ -83,10 +83,10 @@
3.4 return self.tzid
3.5
3.6 def get_start_item(self):
3.7 - return self.start, get_datetime_attributes(self.start)
3.8 + return self.start, get_datetime_attributes(self.start, self.get_tzid())
3.9
3.10 def get_end_item(self):
3.11 - return self.end, get_datetime_attributes(self.end)
3.12 + return self.end, get_datetime_attributes(self.end, self.get_tzid())
3.13
3.14 def get_start_point(self):
3.15 return to_utc_datetime(self.get_start(), self.get_tzid())