diff --git a/week05-testing/test_times.py b/week05-testing/test_times.py new file mode 100644 index 0000000..40de8b6 --- /dev/null +++ b/week05-testing/test_times.py @@ -0,0 +1,10 @@ +from times import compute_overlap_time, time_range + +def test_given_input(): + + range1 = time_range("2010-01-12 10:00:00", "2010-01-12 12:00:00") + range2 = time_range("2010-01-12 10:30:00", "2010-01-12 10:45:00", 2, 60) + result = compute_overlap_time(range1, range2) + expected = [('2010-01-12 10:30:00', '2010-01-12 10:37:00'), ('2010-01-12 10:38:00', '2010-01-12 10:45:00')] + assert result == expected + diff --git a/week05-testing/times.py b/week05-testing/times.py new file mode 100644 index 0000000..3226477 --- /dev/null +++ b/week05-testing/times.py @@ -0,0 +1,33 @@ +import datetime + + +# gives the time range between start time and end time [t1, t2] and [t3,t3] +# d is the time difference +# sec_range gives the difference interval in seconds +# number of intervals [t2,t3] +def time_range(start_time, end_time, number_of_intervals=1, gap_between_intervals_s=0): # what's intervals? + start_time_s = datetime.datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S") # from date string to datetime objects + end_time_s = datetime.datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S") # from date string to datetime objects + d = (end_time_s - start_time_s).total_seconds() / number_of_intervals + gap_between_intervals_s * (1 / number_of_intervals - 1) + sec_range = [(start_time_s + datetime.timedelta(seconds=i * d + i * gap_between_intervals_s), + start_time_s + datetime.timedelta(seconds=(i + 1) * d + i * gap_between_intervals_s)) + for i in range(number_of_intervals)] + return [(ta.strftime("%Y-%m-%d %H:%M:%S"), tb.strftime("%Y-%m-%d %H:%M:%S")) for ta, tb in sec_range] + # from object datetime to string - output: 7200sec + +# gives the overlap time between range 1 [1,t2] and range 2 [t3,t4] +def compute_overlap_time(range1, range2): + overlap_time = [] + for start1, end1 in range1: + for start2, end2 in range2: + low = max(start1, start2) + high = min(end1, end2) + overlap_time.append((low, high)) + return overlap_time + +if __name__ == "__main__": + large = time_range("2010-01-12 10:00:00", "2010-01-12 12:00:00") # range 1 [t1,t2] + short = time_range("2010-01-12 10:30:00", "2010-01-12 10:45:00", 2, 60) # range 2 [t3,t4] + print(large) + print(short) + print(compute_overlap_time(large, short)) \ No newline at end of file