Skip to content

Commit

Permalink
Fixed bug with conversion of invalid dates.
Browse files Browse the repository at this point in the history
  • Loading branch information
bdolinar authored and wdolinar committed Jul 22, 2022
1 parent 38e4df8 commit b8fe2cc
Show file tree
Hide file tree
Showing 7 changed files with 272 additions and 151 deletions.
9 changes: 7 additions & 2 deletions _package/tests/unit_tests/time_conversion_pyt.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@
class TimeConversionTests(unittest.TestCase):
"""Tests functions in time_conversion.py."""
def test_datetime_to_julian(self):
"""Test conversions between Julian to calendar date and times."""
"""Test datetime_to_julian."""
date_time = datetime.datetime(year=2004, month=6, day=3, hour=2, minute=8, second=32)
julian = datetime_to_julian(date_time)
self.assertEqual(2453159.5892592594, julian)

def test_julian_to_datetime(self):
"""Test conversions between Julian to calendar date and times."""
"""Test julian_to_datetime."""
date_time = julian_to_datetime(2453159.5892592594)
expected_date_time = datetime.datetime(year=2004, month=6, day=3, hour=2, minute=8, second=32)
self.assertEqual(expected_date_time, date_time)

def test_julian_to_datetime_invalid(self):
"""Test julian_to_datetime with invalid julian input."""
date_time = julian_to_datetime(-99999999.0)
self.assertIsNone(date_time)
2 changes: 1 addition & 1 deletion _package/xms/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
from . import misc # NOQA: F401
from . import time # NOQA: F401

__version__ = '6.1.1'
__version__ = '6.1.2'
7 changes: 5 additions & 2 deletions _package/xms/core/time/time_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ def julian_to_datetime(julian: float) -> Optional[datetime]:
Returns:
datetime.datetime: A Python datetime representing julian, or None if date is invalid.
"""
year, month, day, hour, minute, second = time_cpp.tmJulianToCalendar(julian)
result = time_cpp.tmJulianToCalendar(julian)
if result is None:
return None
year, month, day, hour, minute, second = result
return datetime(year=year, month=month, day=day,
hour=hour, minute=minute, second=second,
microsecond=0)


def datetime_to_julian(dt: datetime) -> float:
def datetime_to_julian(dt: datetime) -> Optional[float]:
"""Convert a Python datetime to a Julian double.
Args:
Expand Down
19 changes: 11 additions & 8 deletions xmscore/python/time/TimeConversion_py.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,22 @@ void initTimeConversion(py::module& geometry)
// function: tmCalendarToJulian
// ---------------------------------------------------------------------------
geometry.def("tmCalendarToJulian",
[](int a_yr, int a_mo, int a_day, int a_hr, int a_min, int a_sec) -> double {
[](int a_yr, int a_mo, int a_day, int a_hr, int a_min, int a_sec) -> py::object {
double julian = 0.0;
xms::tmCalendarToJulian(xms::ERA_CE, a_yr, a_mo, a_day, a_hr, a_min, a_sec,
&julian);
return julian;
bool success =
xms::tmCalendarToJulian(a_yr, a_mo, a_day, a_hr, a_min, a_sec, &julian);
if (success)
return py::cast(julian);
return py::none();
});
// ---------------------------------------------------------------------------
// function: tmJulianToCalendar
// ---------------------------------------------------------------------------
geometry.def("tmJulianToCalendar", [](double a_julian) -> py::tuple {
geometry.def("tmJulianToCalendar", [](double a_julian) -> py::object {
int year, month, day, hour, minute, second;
xms::TimeEra era; // ignored like in data_objects
xms::tmJulianToCalendar(&era, &year, &month, &day, &hour, &minute, &second, a_julian);
return py::make_tuple(year, month, day, hour, minute, second);
bool success = xms::tmJulianToCalendar(&year, &month, &day, &hour, &minute, &second, a_julian);
if (success)
return py::make_tuple(year, month, day, hour, minute, second);
return py::none();
});
}
Loading

0 comments on commit b8fe2cc

Please sign in to comment.