Skip to content

Commit a65e032

Browse files
authored
Merge pull request #18 from gisce/fix-add-time-hours
Fix add_time_unit add hours
2 parents 964ffb8 + 4e15546 commit a65e032

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

Diff for: ooui/graph/timerange.py

+2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ def add_time_unit(start_date, interval, units):
7474
return start_date + relativedelta(months=interval)
7575
elif units == 'years':
7676
return start_date + relativedelta(years=interval)
77+
elif units == 'hours':
78+
return start_date + timedelta(hours=interval)
7779
else:
7880
raise ValueError("Unsupported units: {}".format(units))
7981

Diff for: spec/graph/timerange_spec.py

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from mamba import description, context, it
22
from expects import *
3+
from datetime import datetime
34

45
from ooui.graph.timerange import (
56
get_unique_values_grouped_by, get_format_for_units, check_dates_consecutive,
67
get_date_format, convert_date_to_time_range_adjusted,
78
adjust_x_values_for_time_range, combine_values_for_timerange,
89
get_missing_consecutive_dates, fill_gaps_in_timerange_data,
9-
process_timerange_data
10+
process_timerange_data, add_time_unit
1011
)
1112

1213

@@ -340,3 +341,35 @@
340341
{'stacked': None, 'operator': '+', 'x': '2024-06',
341342
'type': 'Revenue', 'value': 300.0},
342343
))
344+
345+
with description('add_time_unit function'):
346+
with context('when adding different time units'):
347+
with it('adds days correctly'):
348+
start_date = datetime(2021, 1, 1)
349+
result = add_time_unit(start_date, 10, 'days')
350+
expect(result).to(equal(datetime(2021, 1, 11)))
351+
352+
with it('adds weeks correctly'):
353+
start_date = datetime(2021, 1, 1)
354+
result = add_time_unit(start_date, 2, 'weeks')
355+
expect(result).to(equal(datetime(2021, 1, 15)))
356+
357+
with it('adds months correctly'):
358+
start_date = datetime(2021, 1, 1)
359+
result = add_time_unit(start_date, 1, 'months')
360+
expect(result).to(equal(datetime(2021, 2, 1)))
361+
362+
with it('adds years correctly'):
363+
start_date = datetime(2021, 1, 1)
364+
result = add_time_unit(start_date, 1, 'years')
365+
expect(result).to(equal(datetime(2022, 1, 1)))
366+
367+
with it('adds hours correctly'):
368+
start_date = datetime(2021, 1, 1, 12, 0)
369+
result = add_time_unit(start_date, 5, 'hours')
370+
expect(result).to(equal(datetime(2021, 1, 1, 17, 0)))
371+
372+
with it('raises an error for unsupported units'):
373+
start_date = datetime(2021, 1, 1)
374+
expect(lambda: add_time_unit(start_date, 10, 'minutes')).to(
375+
raise_error(ValueError))

0 commit comments

Comments
 (0)