def datetime_mod(dt, period, start=None):
if start is None:<EOL><INDENT>start = datetime.datetime.combine(dt.date(), datetime.time())<EOL><DEDENT>delta = dt - start<EOL>def get_time_delta_microseconds(td):<EOL><INDENT>return (td.days * seconds_per_day + td.seconds) * <NUM_LIT> + td.microseconds<EOL><DEDENT>delta, period = map(get_time_delta_microseconds, (delta, period))<EOL>offset = datetime.timedelta(microseconds=delta % period)<EOL>result = dt - offset<EOL>return result<EOL>
Find the time which is the specified date/time truncated to the time delta
relative to the start date/time.
By default, the start time is midnight of the same day as the specified
date/time.

>>> datetime_mod(datetime.datetime(2004, 1, 2, 3),
...     datetime.timedelta(days = 1.5),
...     start = datetime.datetime(2004, 1, 1))
datetime.datetime(2004, 1, 1, 0, 0)
>>> datetime_mod(datetime.datetime(2004, 1, 2, 13),
...     datetime.timedelta(days = 1.5),
...     start = datetime.datetime(2004, 1, 1))
datetime.datetime(2004, 1, 2, 12, 0)
>>> datetime_mod(datetime.datetime(2004, 1, 2, 13),
...     datetime.timedelta(days = 7),
...     start = datetime.datetime(2004, 1, 1))
datetime.datetime(2004, 1, 1, 0, 0)
>>> datetime_mod(datetime.datetime(2004, 1, 10, 13),
...     datetime.timedelta(days = 7),
...     start = datetime.datetime(2004, 1, 1))
datetime.datetime(2004, 1, 8, 0, 0)