From c129248a382279ddf8190f42380c3afd9370cef9 Mon Sep 17 00:00:00 2001 From: "xanthos@durruti" Date: Mon, 8 Apr 2024 12:07:02 +0300 Subject: [PATCH] add_seconds now accepts only FractionalSeconds --- src/tpdate.hpp | 27 ++++++++++++++++++------ test/precision/tpadd.cpp | 8 +++---- test/timer/test_tpdate_normalization.cpp | 2 +- test/unit_tests/dread2.cpp | 12 +++++------ test/unit_tests/dread3.cpp | 4 ++-- test/unit_tests/leapday.cpp | 12 +++++------ test/unit_tests/tpdate_add.cpp | 16 +++++++------- 7 files changed, 47 insertions(+), 34 deletions(-) diff --git a/src/tpdate.hpp b/src/tpdate.hpp index a267dce..8929127 100644 --- a/src/tpdate.hpp +++ b/src/tpdate.hpp @@ -61,6 +61,13 @@ class TwoPartDateUTC { : _mjd(mjd), _fsec(secday) { normalize(); } + + /** Add seconds to instance, taking into account leap seconds. + */ + void add_seconds(FDOUBLE sec) noexcept { + _fsec += sec; + this->normalize(); + } public: /** Constructor from datetime */ @@ -114,12 +121,11 @@ class TwoPartDateUTC { /** @brief Transform the (integral part of the) date to Year Month Day */ ymd_date to_ymd() const noexcept { return core::mjd2ymd((long)_mjd); } - + /** Add seconds to instance, taking into account leap seconds. */ - void add_seconds(FDOUBLE sec) noexcept { - _fsec += sec; - this->normalize(); + void add_seconds(FractionalSeconds fsec) noexcept { + this->add_seconds(fsec.fsec); } /** Add seconds to instance and return the "Kahan summation" error. @@ -276,6 +282,14 @@ class TwoPartDate { : _mjd(mjd), _fsec(secday) { normalize(); } + + /** Add seconds to instance. + * @warning Does not take into account leap seconds. + */ + void add_seconds(FDOUBLE sec) noexcept { + _fsec += sec; + this->normalize(); + } public: /** Constructor from datetime @@ -367,9 +381,8 @@ class TwoPartDate { /** Add seconds to instance. * @warning Does not take into account leap seconds. */ - void add_seconds(FDOUBLE sec) noexcept { - _fsec += sec; - this->normalize(); + void add_seconds(FractionalSeconds fsec) noexcept { + this->add_seconds(fsec.fsec); } /** Add seconds to instance and return the "Kahan summation" error. diff --git a/test/precision/tpadd.cpp b/test/precision/tpadd.cpp index 8678f9e..9ddc867 100644 --- a/test/precision/tpadd.cpp +++ b/test/precision/tpadd.cpp @@ -14,28 +14,28 @@ int main() { printf("Date: %d %.15f\n", d.imjd(), d.seconds()); for (long i = 0; i < 1'000'000'000; i++) { - d.add_seconds(1e-9); + d.add_seconds(FractionalSeconds(1e-9)); } printf("Date: %d %.15f Dsec=%.5e\n", d.imjd(), d.seconds(), std::abs(1e0 - d.seconds())); d = TwoPartDate(60224, FractionalSeconds(0e0)); for (long i = 0; i < 1'000'000; i++) { - d.add_seconds(1e-6); + d.add_seconds(FractionalSeconds(1e-6)); } printf("Date: %d %.15f Dsec=%.5e\n", d.imjd(), d.seconds(), std::abs(1e0 - d.seconds())); d = TwoPartDate(60224, FractionalSeconds(0e0)); for (long i = 0; i < 1'000; i++) { - d.add_seconds(1e-3); + d.add_seconds(FractionalSeconds(1e-3)); } printf("Date: %d %.15f Dsec=%.5e\n", d.imjd(), d.seconds(), std::abs(1e0 - d.seconds())); d = TwoPartDate(60224, FractionalSeconds(0e0)); for (long i = 0; i < 1; i++) { - d.add_seconds(1); + d.add_seconds(FractionalSeconds(1)); } printf("Date: %d %.15f Dsec=%.5e\n", d.imjd(), d.seconds(), std::abs(1e0 - d.seconds())); diff --git a/test/timer/test_tpdate_normalization.cpp b/test/timer/test_tpdate_normalization.cpp index f920a47..cc3bbd8 100644 --- a/test/timer/test_tpdate_normalization.cpp +++ b/test/timer/test_tpdate_normalization.cpp @@ -50,7 +50,7 @@ int main() { for (int j = 0; j < 10; j++) { dv.push_back(s + j); } - tpd1.add_seconds(s / SEC_PER_DAY); + tpd1.add_seconds(dso::FractionalSeconds(s / SEC_PER_DAY)); for (int j = 0; j < 10; j++) { if (dv[j] < 0e0) ++ok; diff --git a/test/unit_tests/dread2.cpp b/test/unit_tests/dread2.cpp index c66585b..2397623 100644 --- a/test/unit_tests/dread2.cpp +++ b/test/unit_tests/dread2.cpp @@ -44,9 +44,9 @@ int main() { for (auto const &d : leap_insertion_dates) { TwoPartDate tai(modified_julian_day(d).as_underlying_type()); - tai.add_seconds(86400 - 1); + tai.add_seconds(FractionalSeconds(86400 - 1)); TwoPartDateUTC utc(modified_julian_day(d).as_underlying_type()); - utc.add_seconds(86400 - 1); + utc.add_seconds(FractionalSeconds(86400 - 1)); /* we are now at 23:59:59 */ assert(tai.imjd() == utc.imjd()); @@ -66,8 +66,8 @@ int main() { const auto utc2359(utc); /* add one more second */ - tai.add_seconds(1e0); - utc.add_seconds(1e0); + tai.add_seconds(FractionalSeconds(1e0)); + utc.add_seconds(FractionalSeconds(1e0)); assert(tai.imjd() == utc.imjd() + 1); /* TAI seconds should be 0 */ assert(tai.sec_of_day() == 0e0 && @@ -86,8 +86,8 @@ int main() { } /* add one more seconds */ - tai.add_seconds(1e0); - utc.add_seconds(1e0); + tai.add_seconds(FractionalSeconds(1e0)); + utc.add_seconds(FractionalSeconds(1e0)); assert(tai.imjd() == utc.imjd()); /* UTC + 1[sec] = TAI */ assert(utc.sec_of_day() + S == tai.sec_of_day()); diff --git a/test/unit_tests/dread3.cpp b/test/unit_tests/dread3.cpp index ea38674..1f3edca 100644 --- a/test/unit_tests/dread3.cpp +++ b/test/unit_tests/dread3.cpp @@ -120,7 +120,7 @@ int main() { /* one seconds before midnight */ TwoPartDate tai(modified_julian_day(d).as_underlying_type(), FractionalSeconds(0e0)); - tai.add_seconds(86400 - 1); + tai.add_seconds(FractionalSeconds(86400 - 1)); { TwoPartDate d1; std::strcat(reset_buffer(buf1), leap_insertion_dates_str[it]); @@ -157,7 +157,7 @@ int main() { TwoPartDateUTC utc(modified_julian_day(d).as_underlying_type(), FractionalSeconds(0e0)); - utc.add_seconds(86400 - 1); + utc.add_seconds(FractionalSeconds(86400 - 1)); { TwoPartDateUTC d1; std::strcat(reset_buffer(buf1), leap_insertion_dates_str[it]); diff --git a/test/unit_tests/leapday.cpp b/test/unit_tests/leapday.cpp index 25d48be..5d4ce15 100644 --- a/test/unit_tests/leapday.cpp +++ b/test/unit_tests/leapday.cpp @@ -44,11 +44,11 @@ int main() { for (auto const &d : leap_insertion_dates) { TwoPartDate tai(modified_julian_day(d).as_underlying_type()); for (int i = 0; i < 86400 - 1; i++) { - tai.add_seconds(1e0); + tai.add_seconds(FractionalSeconds(1e0)); } TwoPartDateUTC utc(modified_julian_day(d).as_underlying_type()); for (int i = 0; i < 86400 - 1; i++) { - utc.add_seconds(1e0); + utc.add_seconds(FractionalSeconds(1e0)); } /* we are now at 23:59:59 */ @@ -60,8 +60,8 @@ int main() { const auto utc2359(utc); /* add one more seconds */ - tai.add_seconds(1e0); - utc.add_seconds(1e0); + tai.add_seconds(FractionalSeconds(1e0)); + utc.add_seconds(FractionalSeconds(1e0)); assert(tai.imjd() == utc.imjd() + 1); /* TAI seconds should be 0 */ assert(tai.sec_of_day() == 0e0 && @@ -76,8 +76,8 @@ int main() { // tai.sec_of_day(), buf2, utc.sec_of_day()); /* add one more seconds */ - tai.add_seconds(1e0); - utc.add_seconds(1e0); + tai.add_seconds(FractionalSeconds(1e0)); + utc.add_seconds(FractionalSeconds(1e0)); assert(tai.imjd() == utc.imjd()); /* UTC + 1[sec] = TAI */ assert(utc.sec_of_day() + S == tai.sec_of_day()); diff --git a/test/unit_tests/tpdate_add.cpp b/test/unit_tests/tpdate_add.cpp index acf9825..63660a4 100644 --- a/test/unit_tests/tpdate_add.cpp +++ b/test/unit_tests/tpdate_add.cpp @@ -20,7 +20,7 @@ int main() { TwoPartDate td1(d1); for (int i = 0; i < 86400; i++) { - td1.add_seconds(1e0); + td1.add_seconds(FractionalSeconds(1e0)); } assert(td1.imjd() - 1 == d1.imjd().as_underlying_type()); assert(td1.seconds() == 0e0); @@ -28,7 +28,7 @@ int main() { td1 = datetime(year(2023), month(10), day_of_month(24), nanoseconds(0)); for (int i = 0; i < 2 * 86400; i++) { - td1.add_seconds(1e0); + td1.add_seconds(FractionalSeconds(1e0)); } assert(td1.imjd() - 2 == d1.imjd().as_underlying_type()); assert(td1.seconds() == 0e0); @@ -36,7 +36,7 @@ int main() { td1 = datetime(year(2023), month(10), day_of_month(24), nanoseconds(0)); for (int i = 0; i < 86400; i++) { - td1.add_seconds(-1e0); + td1.add_seconds(FractionalSeconds(-1e0)); } assert(td1.imjd() + 1 == d1.imjd().as_underlying_type()); assert(td1.seconds() == 0e0); @@ -44,7 +44,7 @@ int main() { td1 = datetime(year(2023), month(10), day_of_month(24), nanoseconds(0)); for (int i = 0; i < 2 * 86400; i++) { - td1.add_seconds(-1e0); + td1.add_seconds(FractionalSeconds(-1e0)); } assert(td1.imjd() + 2 == d1.imjd().as_underlying_type()); assert(td1.seconds() == 0e0); @@ -52,8 +52,8 @@ int main() { td1 = datetime(year(2023), month(10), day_of_month(24), nanoseconds(0)); for (int i = 0; i < 86400; i++) { - td1.add_seconds(2e0); - td1.add_seconds(-1e0); + td1.add_seconds(FractionalSeconds(2e0)); + td1.add_seconds(FractionalSeconds(-1e0)); } assert(td1.imjd() - 1 == d1.imjd().as_underlying_type()); assert(td1.seconds() == 0e0); @@ -83,14 +83,14 @@ int main() { if (ok) { td = TwoPartDate(d); for (int i = 0; i < 86400; i++) { - td.add_seconds(-1e0); + td.add_seconds(FractionalSeconds(-1e0)); } assert(td.imjd() + 1 == d.imjd().as_underlying_type()); assert(std::abs(td.sec_of_day() - d.sec().as_underlying_type()) < 2e-2); td = TwoPartDate(d); for (int i = 0; i < 86400; i++) { - td.add_seconds(2e0); + td.add_seconds(FractionalSeconds(2e0)); } assert(td.imjd() - 2 == d.imjd().as_underlying_type()); assert(std::abs(td.sec_of_day() - d.sec().as_underlying_type()) <