Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/python-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Python package

on: [push]

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.10"]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
# This case we are only updating pip, but you could add other dependencies needed.
run: |
python -m pip install --upgrade pip
python -m pip install pytest
python -m pip install pytest-cov
- name: Test with pytest
run: |
pytest
pytest --cov="."
44 changes: 44 additions & 0 deletions test_times.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from times import *
import pytest

def test_given_input():
"""Standard results"""
large = time_range("2010-01-12 10:00:00", "2010-01-12 12:00:00")
short = time_range("2010-01-12 10:30:00", "2010-01-12 10:45:00", 2, 60)
result = compute_overlap_time(large, short)
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

def test_no_overlap():
"""Two time ranges that do not overlap"""
large = time_range("2010-01-12 10:00:00", "2010-01-12 12:00:00")
short = time_range("2010-01-12 12:30:00", "2010-01-12 12:45:00")
result = compute_overlap_time(large, short)
expected = []
assert result == expected

def test_several_intervals():
"""Two time ranges that both contain several intervals each"""
large = time_range("2010-01-12 10:00:00", "2010-01-12 10:31:00", 2, 60) #10-15 16-31
short = time_range("2010-01-12 10:15:00", "2010-01-12 10:46:00", 2, 60) #15-30 31-46
result = compute_overlap_time(large, short)
expected = [('2010-01-12 10:15:00', '2010-01-12 10:15:00'), ('2010-01-12 10:16:00', '2010-01-12 10:30:00'), ('2010-01-12 10:31:00', '2010-01-12 10:31:00')]
assert result == expected

def test_end_to_end():
"""Two time ranges that end exactly at the same time when the other starts"""
large = time_range("2010-01-12 10:00:00", "2010-01-12 10:31:00")
short = time_range("2010-01-12 10:31:00", "2010-01-12 10:46:00")
result = compute_overlap_time(large, short)
expected = [('2010-01-12 10:31:00', '2010-01-12 10:31:00')]
assert result == expected

def test_time_range_backwards():
with pytest.raises(ValueError, match=r"End time: .* is before start time: .*\."):
time_range("2010-01-12 10:45:00", "2010-01-12 10:31:00")

"""
For test coverage, run in terminal:
pytest --cov="." --cov-report html
python -m http.server -d htmlcov
"""
7 changes: 5 additions & 2 deletions times.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import datetime
import datetime


def time_range(start_time, end_time, number_of_intervals=1, gap_between_intervals_s=0):
start_time_s = datetime.datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S")
end_time_s = datetime.datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S")
if (start_time_s > end_time_s):
raise ValueError(f"End time: {end_time} is before start time: {start_time}.")
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))
Expand All @@ -17,7 +19,8 @@ def compute_overlap_time(range1, range2):
for start2, end2 in range2:
low = max(start1, start2)
high = min(end1, end2)
overlap_time.append((low, high))
if (low <= high):
overlap_time.append((low, high))
return overlap_time

if __name__ == "__main__":
Expand Down