From 0a6539c21c041914e2ac72809ef6c6422f603ddb Mon Sep 17 00:00:00 2001 From: Paulo Schneider Date: Wed, 4 Dec 2013 11:53:31 +0000 Subject: [PATCH] Starts for each quarter --- backdrop/core/timeseries.py | 6 +++++- tests/core/test_timeseries.py | 21 +++++++++++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/backdrop/core/timeseries.py b/backdrop/core/timeseries.py index 3c821307..3211bf5c 100644 --- a/backdrop/core/timeseries.py +++ b/backdrop/core/timeseries.py @@ -89,9 +89,13 @@ class Quarter(Period): def __init__(self): self.name = "month" self._delta = relativedelta(months=3) + self.quarter_starts = [10, 7, 4, 1] def start(self, timestamp): - return timestamp.replace(month=10, day=1, hour=0, minute=0, + quarter_month = next(quarter for quarter in self.quarter_starts + if timestamp.month >= quarter) + + return timestamp.replace(month=quarter_month, day=1, hour=0, minute=0, second=0, microsecond=0) def valid_start_at(self, timestamp): diff --git a/tests/core/test_timeseries.py b/tests/core/test_timeseries.py index 22998e15..034e7ed8 100644 --- a/tests/core/test_timeseries.py +++ b/tests/core/test_timeseries.py @@ -293,9 +293,22 @@ def test_that_a_range_of_five_hours_gives_us_five_data_points(self): )) class TestQuarter(TestCase): - def test_that_returns_the_beginning_of_the_current_quarter(self): - some_datetime = d(2013, 12, 4, 10, 23, 43) + def test_that_returns_the_beginning_of_the_first_quarter(self): + some_datetime = d(2013, 1, 20, 0, 23, 43) + + assert_that(QUARTER.start(some_datetime), is_(d(2013, 1, 1, 0, 0, 0))) + + def test_that_returns_the_beginning_of_the_second_quarter(self): + some_datetime = d(2013, 5, 20, 0, 23, 43) + + assert_that(QUARTER.start(some_datetime), is_(d(2013, 4, 1, 0, 0, 0))) - start = QUARTER.start(some_datetime) + def test_that_returns_the_beginning_of_the_third_quarter(self): + some_datetime = d(2013, 9, 20, 0, 23, 43) + + assert_that(QUARTER.start(some_datetime), is_(d(2013, 7, 1, 0, 0, 0))) + + def test_that_returns_the_beginning_of_the_fourth_quarter(self): + some_datetime = d(2013, 12, 4, 10, 23, 43) - assert_that(start, is_(d(2013, 10, 1, 0, 0, 0))) \ No newline at end of file + assert_that(QUARTER.start(some_datetime), is_(d(2013, 10, 1, 0, 0, 0))) \ No newline at end of file