Skip to content

Commit

Permalink
Time conversion modifications/corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
DominicDirkx committed Oct 28, 2023
1 parent 69392ff commit e2a118c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
11 changes: 10 additions & 1 deletion tests/test_time_conversions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# import sys
# sys.path.insert(0, "/home/dominic/Tudat/tudat-bundle/tudat-bundle/cmake-build-default/tudatpy")

from datetime import datetime
import math
from datetime import datetime
from tudatpy.kernel.astro import time_conversion


Expand All @@ -25,4 +26,12 @@ def test_datetime_conversions():
tudat_datettime = time_conversion.datetime_to_tudat(time_conversion.julian_day_to_calendar_date(julian_day))
assert julian_day == tudat_datettime.julian_day( )

pre_1970_date = datetime(1960,1,1,13,7,23,891234)
julian_day_to_check = time_conversion.calendar_date_to_julian_day(pre_1970_date)

assert math.floor( julian_day_to_check ) == 2436935
seconds_in_day_to_check = ( julian_day_to_check - math.floor( julian_day_to_check ) ) * 86400.0
manual_seconds_in_day = 1 * 3600 + 7 * 60 + 23.891234
assert math.fabs( manual_seconds_in_day - seconds_in_day_to_check ) < 1.0E-4
print('DATE TIME TEST SUCCESFUL')

38 changes: 36 additions & 2 deletions tudatpy/kernel/expose_astro/expose_time_conversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ tba::DateTime timePointToDateTime(const std::chrono::system_clock::time_point da

using namespace std::chrono;
microseconds timeInMicroSeconds = duration_cast<microseconds>(datetime.time_since_epoch());
auto fractional_seconds = timeInMicroSeconds.count() % 1000000;
long long fractional_seconds = timeInMicroSeconds.count() % 1000000LL;

return tba::DateTime( local_tm.tm_year + 1900, local_tm.tm_mon + 1, local_tm.tm_mday,
local_tm.tm_hour, local_tm.tm_min, static_cast< long double >( local_tm.tm_sec ) +
Expand Down Expand Up @@ -91,13 +91,30 @@ namespace tudat

namespace earth_orientation
{
std::shared_ptr< TerrestrialTimeScaleConverter > createDefaultTimeConverterPy( )
std::shared_ptr<TerrestrialTimeScaleConverter> createDefaultTimeConverterPy( )
{
return createDefaultTimeConverter( );
}

}

namespace basic_astrodynamics
{

template< typename TimeType >
DateTime addSecondsToDateTime( const DateTime& dateTime, const TimeType timeToAdd )
{
return getCalendarDateFromTime< Time >( dateTime.epoch< Time >( ) + timeToAdd );
}

template< typename TimeType >
DateTime addDaysToDateTime( const DateTime& dateTime, const TimeType daysToAdd )
{
return getCalendarDateFromTime< Time >( dateTime.epoch< Time >( ) + daysToAdd * mathematical_constants::getFloatingInteger< TimeType >( 86400 ) );
}

}

}

namespace tudatpy {
Expand All @@ -122,6 +139,23 @@ void expose_time_conversion(py::module &m) {
get_docstring("datetime_to_python").c_str()
);

m.def("add_seconds_to_datetime",
&tba::addSecondsToDateTime< TIME_TYPE >,
py::arg("datetime"),
py::arg("seconds_to_add"),
get_docstring("add_seconds_to_datetime").c_str()
);

m.def("add_days_to_datetime",
&tba::addDaysToDateTime< TIME_TYPE >,
py::arg("datetime"),
py::arg("days_to_add"),
get_docstring("add_days_to_datetime").c_str()
);






m.def("calendar_date_to_julian_day",
Expand Down

0 comments on commit e2a118c

Please sign in to comment.