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
26 changes: 26 additions & 0 deletions .github/workflows/python-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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
python3 -m pip install pytest
- name: Test with pytest
run: |
python3 -m pytest
18 changes: 18 additions & 0 deletions test_times.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from times import time_range, compute_overlap_time

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the way you chose to import the 2 methods is great.

from pytest import raises
def test_given_input():
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_time_ranges_no_overlap():
large = time_range("2010-01-12 10:00:00", "2010-01-12 11:00:00")
short = time_range("2010-01-12 11:30:00", "2010-01-12 11:45:00")
result = compute_overlap_time(large, short)
assert result == []

def test_backwards_time_range():
with raises(ValueError):
time_range("2010-01-12 12:00:00", "2010-01-12 11:00:00")
7 changes: 7 additions & 0 deletions times.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
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 > end_time:
raise ValueError("Start time is after end 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,6 +21,9 @@ def compute_overlap_time(range1, range2):
for start2, end2 in range2:
low = max(start1, start2)
high = min(end1, end2)
if low>=high:
return overlap_time

overlap_time.append((low, high))
return overlap_time

Expand Down