Skip to content

Commit

Permalink
Merge pull request #76 from lsst/tickets/DM-37080
Browse files Browse the repository at this point in the history
DM-37080: Raise if toPython is called on invalid DateTime.
  • Loading branch information
ktlim committed Nov 24, 2022
2 parents 5c4744a + eec769b commit 4fd1a49
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
7 changes: 7 additions & 0 deletions python/lsst/daf/base/dateTime/dateTimeContinued.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,15 @@ def toPython(self, timescale=None):
-------
datetime : `datetime.datetime`
The resultant Python `datetime.datetime` object.
Raises
------
ValueError
Raised if the DateTime is invalid (uninitialized).
"""
import datetime
if not self.isValid():
raise RuntimeError("DateTime not valid")
nsecs = self.nsecs(timescale) if timescale is not None else self.nsecs()
return datetime.datetime.utcfromtimestamp(nsecs/10**9)

Expand Down
2 changes: 2 additions & 0 deletions tests/test_dateTime.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ def testInvalid(self):
ts.timeval(scale)
with self.assertRaises(RuntimeError):
ts.toString(scale)
with self.assertRaises(RuntimeError):
ts.toPython()
self.assertEqual(repr(ts), "DateTime()")

def testNegative(self):
Expand Down

0 comments on commit 4fd1a49

Please sign in to comment.