1.1 --- a/imiptools/dates.py Mon Feb 02 18:46:59 2015 +0100
1.2 +++ b/imiptools/dates.py Mon Feb 02 19:43:14 2015 +0100
1.3 @@ -51,7 +51,7 @@
1.4 return to_tz(dt, tz)
1.5
1.6 def to_tz(dt, tz):
1.7 - if tz is not None:
1.8 + if tz is not None and isinstance(dt, datetime):
1.9 if not dt.tzinfo:
1.10 return tz.localize(dt)
1.11 else:
1.12 @@ -80,29 +80,33 @@
1.13 if not attr or attr.get("VALUE") in (None, "DATE-TIME"):
1.14 m = match_datetime_icalendar(value)
1.15 if m:
1.16 - dt = datetime(
1.17 - int(m.group("year")), int(m.group("month")), int(m.group("day")),
1.18 - int(m.group("hour")), int(m.group("minute")), int(m.group("second"))
1.19 - )
1.20 + year, month, day, hour, minute, second = map(m.group, [
1.21 + "year", "month", "day", "hour", "minute", "second"
1.22 + ])
1.23
1.24 - # Impose the indicated timezone.
1.25 - # NOTE: This needs an ambiguity policy for DST changes.
1.26 + if hour and minute and second:
1.27 + dt = datetime(
1.28 + int(year), int(month), int(day), int(hour), int(minute), int(second)
1.29 + )
1.30
1.31 - return to_timezone(dt, m.group("utc") and "UTC" or attr and attr.get("TZID") or None)
1.32 + # Impose the indicated timezone.
1.33 + # NOTE: This needs an ambiguity policy for DST changes.
1.34 +
1.35 + return to_timezone(dt, m.group("utc") and "UTC" or attr and attr.get("TZID") or None)
1.36
1.37 if not attr or attr.get("VALUE") == "DATE":
1.38 m = match_date_icalendar(value)
1.39 if m:
1.40 - return date(
1.41 - int(m.group("year")), int(m.group("month")), int(m.group("day"))
1.42 - )
1.43 + year, month, day = map(m.group, ["year", "month", "day"])
1.44 + return date(int(year), int(month), int(day))
1.45 +
1.46 return None
1.47
1.48 -def get_start_of_day(dt):
1.49 - return datetime(dt.year, dt.month, dt.day, 0, 0, tzinfo=dt.tzinfo)
1.50 +def get_start_of_day(dt, tzid=None):
1.51 + return datetime(dt.year, dt.month, dt.day, 0, 0, tzinfo=(tzid and timezone(tzid) or dt.tzinfo))
1.52
1.53 -def get_end_of_day(dt):
1.54 - return get_start_of_day(dt + timedelta(1))
1.55 +def get_end_of_day(dt, tzid=None):
1.56 + return get_start_of_day(dt + timedelta(1), tzid)
1.57
1.58 def ends_on_same_day(dt, end):
1.59 return (