Skip to content
This repository has been archived by the owner on Mar 24, 2021. It is now read-only.

Commit

Permalink
Starts for each quarter
Browse files Browse the repository at this point in the history
  • Loading branch information
phss committed Dec 4, 2013
1 parent 304d599 commit 0a6539c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
6 changes: 5 additions & 1 deletion backdrop/core/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
21 changes: 17 additions & 4 deletions tests/core/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
assert_that(QUARTER.start(some_datetime), is_(d(2013, 10, 1, 0, 0, 0)))

0 comments on commit 0a6539c

Please sign in to comment.