Skip to content

Commit

Permalink
Add tests of second, millis, and micros ranges in time math
Browse files Browse the repository at this point in the history
  • Loading branch information
spenczar committed Aug 28, 2023
1 parent ffc382c commit 155a987
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
45 changes: 45 additions & 0 deletions adam_core/time/tests/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,51 @@ def test_add_nanos_out_of_range(self):
with pytest.raises(ValueError):
self.t1.add_nanos([0, 0, MAX_VAL + 1])

def test_add_seconds_out_of_range(self):
MIN_VAL = -86400
MAX_VAL = 86400 - 1
# Scalars:
with pytest.raises(ValueError):
self.t1.add_seconds(MIN_VAL - 1)
with pytest.raises(ValueError):
self.t1.add_seconds(MAX_VAL + 1)

# Arrays:
with pytest.raises(ValueError):
self.t1.add_seconds([MIN_VAL - 1, 0, 0])
with pytest.raises(ValueError):
self.t1.add_seconds([0, MAX_VAL + 1, 0])

def test_add_millis_out_of_range(self):
MIN_VAL = -86400_000
MAX_VAL = 86400_000 - 1
# Scalars:
with pytest.raises(ValueError):
self.t1.add_millis(MIN_VAL - 1)
with pytest.raises(ValueError):
self.t1.add_millis(MAX_VAL + 1)

# Arrays:
with pytest.raises(ValueError):
self.t1.add_millis([MIN_VAL - 1, 0, 0])
with pytest.raises(ValueError):
self.t1.add_millis([0, MAX_VAL + 1, 0])

def test_add_micros_out_of_range(self):
MIN_VAL = -86400_000_000
MAX_VAL = 86400_000_000 - 1
# Scalars:
with pytest.raises(ValueError):
self.t1.add_micros(MIN_VAL - 1)
with pytest.raises(ValueError):
self.t1.add_micros(MAX_VAL + 1)

# Arrays:
with pytest.raises(ValueError):
self.t1.add_micros([MIN_VAL - 1, 0, 0])
with pytest.raises(ValueError):
self.t1.add_micros([0, MAX_VAL + 1, 0])

def test_add_nanos_scalar(self):
have = self.t1.add_nanos(1)
assert have.days.to_pylist() == [0, 50000, 60001]
Expand Down
7 changes: 7 additions & 0 deletions adam_core/time/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,13 @@ def add_nanos(
return v2

def add_seconds(self, seconds: pa.lib.Int64Array | int) -> Timestamp:
"""
Add seconds to the timestamp.
Args:
seconds: The seconds to add. Can be a scalar or an array of
the same length as the timestamp. Must be in the range [-86400, 86400).
"""
return self.add_nanos(pc.multiply(seconds, 1_000_000_000))

def add_millis(self, millis: pa.lib.Int64Array | int) -> Timestamp:
Expand Down

0 comments on commit 155a987

Please sign in to comment.