From e2a118cb9910595d4cfdaffa58f5ab3dfd963a7a Mon Sep 17 00:00:00 2001 From: Dominic Dirkx Date: Sat, 28 Oct 2023 15:08:11 +0200 Subject: [PATCH] Time conversion modifications/corrections --- tests/test_time_conversions.py | 11 +++++- .../expose_astro/expose_time_conversion.cpp | 38 ++++++++++++++++++- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/tests/test_time_conversions.py b/tests/test_time_conversions.py index 1240bb10..17175ab3 100644 --- a/tests/test_time_conversions.py +++ b/tests/test_time_conversions.py @@ -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 @@ -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') diff --git a/tudatpy/kernel/expose_astro/expose_time_conversion.cpp b/tudatpy/kernel/expose_astro/expose_time_conversion.cpp index 1b575fe1..0a7b8b6f 100644 --- a/tudatpy/kernel/expose_astro/expose_time_conversion.cpp +++ b/tudatpy/kernel/expose_astro/expose_time_conversion.cpp @@ -41,7 +41,7 @@ tba::DateTime timePointToDateTime(const std::chrono::system_clock::time_point da using namespace std::chrono; microseconds timeInMicroSeconds = duration_cast(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 ) + @@ -91,13 +91,30 @@ namespace tudat namespace earth_orientation { -std::shared_ptr< TerrestrialTimeScaleConverter > createDefaultTimeConverterPy( ) +std::shared_ptr 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 { @@ -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",