1.1 --- a/imiptools/dates.py Tue Feb 03 18:19:59 2015 +0100
1.2 +++ b/imiptools/dates.py Tue Feb 03 18:28:57 2015 +0100
1.3 @@ -112,19 +112,62 @@
1.4 return None
1.5
1.6 def get_start_of_day(dt, tzid):
1.7 +
1.8 + """
1.9 + Get the start of the day in which 'dt' is positioned, using the given 'tzid'
1.10 + to obtain a datetime in the appropriate time zone. Where time zone
1.11 + transitions occur within a day, the zone of 'dt' may not be the eventual
1.12 + zone of the returned object.
1.13 + """
1.14 +
1.15 start = datetime(dt.year, dt.month, dt.day, 0, 0)
1.16 return to_timezone(start, tzid)
1.17
1.18 def get_end_of_day(dt, tzid):
1.19 +
1.20 + """
1.21 + Get the end of the day in which 'dt' is positioned, using the given 'tzid'
1.22 + to obtain a datetime in the appropriate time zone. Where time zone
1.23 + transitions occur within a day, the zone of 'dt' may not be the eventual
1.24 + zone of the returned object.
1.25 + """
1.26 +
1.27 return get_start_of_day(dt + timedelta(1), tzid)
1.28
1.29 def get_start_of_next_day(dt, tzid):
1.30 +
1.31 + """
1.32 + Get the start of the day after the day in which 'dt' is positioned. This
1.33 + function is intended to extend either dates or datetimes to the end of a
1.34 + day for the purpose of generating a missing end date or datetime for an
1.35 + event.
1.36 +
1.37 + If 'dt' is a date and not a datetime, a plain date object for the next day
1.38 + will be returned.
1.39 +
1.40 + If 'dt' is a datetime, the given 'tzid' is used to obtain a datetime in the
1.41 + appropriate time zone. Where time zone transitions occur within a day, the
1.42 + zone of 'dt' may not be the eventual zone of the returned object.
1.43 + """
1.44 +
1.45 if isinstance(dt, datetime):
1.46 return get_end_of_day(dt, tzid)
1.47 else:
1.48 return dt + timedelta(1)
1.49
1.50 def ends_on_same_day(dt, end, tzid):
1.51 +
1.52 + """
1.53 + Return whether 'dt' ends on the same day as 'end', testing the date
1.54 + components of 'dt' and 'end' against each other, but also testing whether
1.55 + 'end' is the actual end of the day in which 'dt' is positioned.
1.56 +
1.57 + Since time zone transitions may occur within a day, 'tzid' is required to
1.58 + determine the end of the day in which 'dt' is positioned, using the zone
1.59 + appropriate at that point in time, not necessarily the zone applying to
1.60 + 'dt'.
1.61 + """
1.62 +
1.63 return (
1.64 dt.date() == end.date() or
1.65 end == get_end_of_day(dt, tzid)