Skip to content

Commit

Permalink
add_seconds now accepts only FractionalSeconds
Browse files Browse the repository at this point in the history
  • Loading branch information
xanthospap committed Apr 8, 2024
1 parent 7f264c6 commit c129248
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 34 deletions.
27 changes: 20 additions & 7 deletions src/tpdate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> */
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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<T>
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions test/precision/tpadd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
2 changes: 1 addition & 1 deletion test/timer/test_tpdate_normalization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions test/unit_tests/dread2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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<nanoseconds>() == 0e0 &&
Expand All @@ -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<nanoseconds>() + S == tai.sec_of_day<nanoseconds>());
Expand Down
4 changes: 2 additions & 2 deletions test/unit_tests/dread3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down Expand Up @@ -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]);
Expand Down
12 changes: 6 additions & 6 deletions test/unit_tests/leapday.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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<nanoseconds>() == 0e0 &&
Expand All @@ -76,8 +76,8 @@ int main() {
// tai.sec_of_day<nanoseconds>(), buf2, utc.sec_of_day<nanoseconds>());

/* 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<nanoseconds>() + S == tai.sec_of_day<nanoseconds>());
Expand Down
16 changes: 8 additions & 8 deletions test/unit_tests/tpdate_add.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,40 @@ 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);

td1 = datetime<nanoseconds>(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);

td1 = datetime<nanoseconds>(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);

td1 = datetime<nanoseconds>(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);

td1 = datetime<nanoseconds>(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);
Expand Down Expand Up @@ -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<nsec>() - 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<nsec>() - d.sec().as_underlying_type()) <
Expand Down

0 comments on commit c129248

Please sign in to comment.