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

Commit

Permalink
Hour validate if timestamp is at start of hour
Browse files Browse the repository at this point in the history
  • Loading branch information
phss committed Oct 18, 2013
1 parent 9b7c2d1 commit 71bfe39
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions backdrop/core/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,16 @@ def range(self, start, end):


class Hour(Period):
def __init__(self):
self.name = "hour"
self._delta = timedelta(hours=1)

def start(self, timestamp):
return timestamp.replace(minute=0, second=0, microsecond=0)

def valid_start_at(self, timestamp):
return timestamp.time() == time(timestamp.hour, 0, 0, 0)


class Day(Period):
def __init__(self):
Expand Down
16 changes: 16 additions & 0 deletions tests/core/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,20 @@ def test_that_returns_the_beginning_of_the_current_hour(self):

assert_that(start, is_(d(2013, 10, 4, 10, 0, 0)))

def test_that_middle_of_the_hour_is_not_a_valid_start_at(self):
middle_of_the_hour = d(2013, 10, 18, 12, 31)

assert_that(HOUR.valid_start_at(middle_of_the_hour), is_(False))

def test_that_beginning_of_the_hour_is_a_valid_start_at(self):
beginning_of_the_hour = d(2013, 10, 18, 12, 0)

assert_that(HOUR.valid_start_at(beginning_of_the_hour), is_(True))

def test_that_returns_the_end_of_the_current_hour(self):
some_datetime = d(2013, 10, 4, 10, 23, 43)

end = HOUR.end(some_datetime)

assert_that(end, is_(d(2013, 10, 4, 11, 0, 0)))

0 comments on commit 71bfe39

Please sign in to comment.