1.1 --- a/imiptools/dates.py Tue Jul 28 19:27:27 2015 +0200
1.2 +++ b/imiptools/dates.py Wed Jul 29 00:18:59 2015 +0200
1.3 @@ -260,6 +260,19 @@
1.4 else:
1.5 return dt + timedelta(1)
1.6
1.7 +def get_datetime_tzid(dt):
1.8 +
1.9 + "Return the time zone identifier from 'dt' or None if unknown."
1.10 +
1.11 + if not isinstance(dt, datetime):
1.12 + return None
1.13 + elif dt.tzname() == "UTC":
1.14 + return "UTC"
1.15 + elif dt.tzinfo and hasattr(dt.tzinfo, "zone"):
1.16 + return dt.tzinfo.zone
1.17 + else:
1.18 + return None
1.19 +
1.20 def to_date(dt):
1.21
1.22 "Return the date of 'dt'."
1.23 @@ -295,15 +308,15 @@
1.24 else:
1.25 return dt
1.26
1.27 -def to_timezone(dt, name):
1.28 +def to_timezone(dt, tzid):
1.29
1.30 """
1.31 Return a datetime corresponding to 'dt' in the time regime having the given
1.32 - 'name'.
1.33 + 'tzid'.
1.34 """
1.35
1.36 try:
1.37 - tz = name and timezone(name) or None
1.38 + tz = tzid and timezone(tzid) or None
1.39 except UnknownTimeZoneError:
1.40 tz = None
1.41 return to_tz(dt, tz)
1.42 @@ -328,6 +341,9 @@
1.43 (start, end) tuple containing datetimes in the UTC time zone, where dates
1.44 are converted to points in time so that each day has a specific start and
1.45 end point defined in UTC.
1.46 +
1.47 + Here, 'tzid' is used to "project" dates into a time zone of interest before
1.48 + then being converted into UTC start and end datetimes.
1.49 """
1.50
1.51 start = to_utc_datetime(start, tzid)
1.52 @@ -362,14 +378,16 @@
1.53
1.54 def get_datetime_attributes(dt, tzid=None):
1.55
1.56 - "Return attributes for 'dt' and 'tzid'."
1.57 + """
1.58 + Return attributes for the 'dt' date or datetime object with 'tzid'
1.59 + indicating the time zone if not otherwise defined.
1.60 + """
1.61
1.62 if isinstance(dt, datetime):
1.63 attr = {"VALUE" : "DATE-TIME"}
1.64 + tzid = get_datetime_tzid(dt) or tzid
1.65 if tzid:
1.66 attr["TZID"] = tzid
1.67 - elif dt.tzname() == "UTC":
1.68 - attr["TZID"] = "UTC"
1.69 return attr
1.70 else:
1.71 return {"VALUE" : "DATE"}
1.72 @@ -380,12 +398,13 @@
1.73
1.74 """
1.75 Return an iCalendar-compatible string and attributes for 'dt' using any
1.76 - specified 'tzid' to assert a particular time zone.
1.77 + specified 'tzid' to assert a particular time zone if not otherwise defined.
1.78 """
1.79
1.80 if not dt:
1.81 return None, None
1.82 - dt = to_timezone(dt, tzid)
1.83 + if not get_datetime_tzid(dt):
1.84 + dt = to_timezone(dt, tzid)
1.85 value = format_datetime(dt)
1.86 attr = get_datetime_attributes(dt, tzid)
1.87 return value, attr