This repository has been archived by the owner on Oct 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3bc7eb6
commit 6043a70
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) |