Skip to content

Commit

Permalink
==[ Minor fix ]==
Browse files Browse the repository at this point in the history
- Added typename to template
  • Loading branch information
cantordust committed Sep 29, 2023
1 parent 5e0eecf commit f458bbc
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 201 deletions.
15 changes: 13 additions & 2 deletions include/kep3/epoch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ class kep3_DLL_PUBLIC epoch {
epoch();

// Constructor for days (as a floating-point value)
template <class FP, std::enable_if<std::is_floating_point<FP>::value,
/**
* Constructs an epoch from a non-gregorian date.
* \param[in] epoch_in A double indicating the non-gregorian date
* \param[in] epoch_type epoch::julian_type
*/
template <class FP, typename std::enable_if<std::is_floating_point<FP>::value,
FP>::type * = nullptr>
epoch(const FP epoch_in = 0.0,
const julian_type epoch_type = julian_type::MJD2000)
Expand All @@ -101,6 +106,12 @@ class kep3_DLL_PUBLIC epoch {
}

// Constructor for const duration&)

/**
* Constructs an epoch from a std::chrono::duration.
* The reference point is assumed to be MJD 0.
* \param[in] time The time as a duration
*/
template <lint Num, lint Den>
epoch(const dur<Num, Den> &duration)
: tp{kep_clock::time_point{} + duration} {}
Expand All @@ -115,7 +126,7 @@ class kep3_DLL_PUBLIC epoch {

// Constructor for microseconds
template <class Int,
std::enable_if<std::is_integral<Int>::value, Int>::type * = nullptr>
typename std::enable_if<std::is_integral<Int>::value, Int>::type * = nullptr>
epoch(const Int us)
: tp{ kep_clock::time_point{chr::microseconds(us)} }
{
Expand Down
29 changes: 0 additions & 29 deletions src/epoch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,7 @@ namespace kep3
: tp{}
{}

/// Constructor.
/**
* Constructs an epoch from a non-gregorian date.
* \param[in] epoch_in A double indicating the non-gregorian date
* \param[in] epoch_type One of [epoch::julian_type::MJD2000,
* \param[in] epoch_type epoch::julian_type::MJD2000
*/
// template<class FP, std::enable_if<std::is_floating_point<FP>::value, FP>::type*>
// epoch::epoch( const FP epoch_in, const julian_type epoch_type )
// : tp{ make_tp( epoch_in, epoch_type ) }

// {
// }

/// Constructor.
/**
Expand All @@ -59,17 +47,6 @@ namespace kep3
{
}

/// Constructor.
/**
* Constructs an epoch from a std::chrono::duration.
* The reference point is assumed to be MJD 0.
* \param[in] time The time as a duration
*/
// template <lint Num, lint Den>
// epoch::epoch( const dur<Num, Den>& time )
// : tp{ kep_clock::time_point( T( time ) ) }
// {
// }

epoch::epoch( const kep_clock::time_point& time_point )
: tp{ time_point }
Expand All @@ -81,12 +58,6 @@ namespace kep3
{
}

// template<class Int, std::enable_if<std::is_integral<Int>::value, Int>::type*>
// epoch::epoch(const Int us)
// : tp{ kep_clock::time_point{chr::microseconds(us)} }
// {
// }

kep_clock::time_point
epoch::make_tp(const int y, const int d, const int h, const int min,
const int s, const int ms, const int us)
Expand Down
4 changes: 1 addition & 3 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ function(ADD_kep3_TESTCASE arg1)
endfunction()

ADD_kep3_TESTCASE(convert_anomalies_test)

# ADD_kep3_TESTCASE(epoch_test)
ADD_kep3_TESTCASE(epoch_test_chrono)
ADD_kep3_TESTCASE(epoch_test)
ADD_kep3_TESTCASE(planet_test)
ADD_kep3_TESTCASE(planet_keplerian_test)
ADD_kep3_TESTCASE(ic2par2ic_test)
Expand Down
143 changes: 86 additions & 57 deletions test/epoch_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,74 +8,103 @@
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <boost/date_time/gregorian/gregorian.hpp>
#include <chrono>
#include <ctime>
#include <iostream>
#include <random>
#include <string>

#include <kep3/epoch.hpp>
#include "kep3/epoch.hpp"

#include "catch.hpp"

using kep3::epoch;
using kep3::epoch_from_iso_string;
using kep3::epoch_from_string;
using boost::gregorian::date;
using boost::posix_time::ptime;
using kep3::kep_clock;
using namespace std::literals;
namespace chr = std::chrono;

TEST_CASE("construct") {
// test syntax
REQUIRE_NOTHROW(epoch());
REQUIRE_NOTHROW(epoch(123.456));
REQUIRE_NOTHROW(epoch(123.456, epoch::MJD2000));
REQUIRE_NOTHROW(epoch(123.456, epoch::JD));
REQUIRE_NOTHROW(epoch(123.456, epoch::MJD));
REQUIRE_NOTHROW(epoch(31, 12, 2034));
// > 2000
boost::posix_time::ptime posix_time_test(date(2034, 12, 31));
REQUIRE_NOTHROW(epoch(posix_time_test));
REQUIRE(epoch(posix_time_test).get_posix_time() == posix_time_test);
// < 2000
boost::posix_time::ptime posix_time_test2(date(1980, 12, 31));
REQUIRE_NOTHROW(epoch(posix_time_test2));
REQUIRE(epoch(posix_time_test2).get_posix_time() == posix_time_test2);
TEST_CASE( "construct" )
{
// test syntax
// // > 2000
REQUIRE_NOTHROW( epoch() );
REQUIRE_NOTHROW( epoch( 123.456 ) );
REQUIRE_NOTHROW( epoch( 123.456, epoch::julian_type::MJD2000 ) );
REQUIRE_NOTHROW( epoch( 0.0, epoch::julian_type::JD ) );
REQUIRE_NOTHROW( epoch( 123.456, epoch::julian_type::JD ) );
REQUIRE_NOTHROW( epoch( 0.0, epoch::julian_type::MJD ) );
REQUIRE_NOTHROW( epoch( 123.456, epoch::julian_type::MJD ) );
REQUIRE_NOTHROW( epoch( 34, 364, 11, 36, 21, 121, 841 ) );
// // > 2000
// boost::posix_time::ptime posix_time_test( date( 2034, 12, 31 ) );
// REQUIRE_NOTHROW( epoch( posix_time_test ) );
// REQUIRE( epoch( posix_time_test ).get_posix_time() == posix_time_test );
// std::cout << "Chrono1: " << epoch(posix_time_test).get_posix_time() <<
// "\n"; std::cout << "Boost1: " << posix_time_test << "\n";

// test conversions
REQUIRE(epoch(123.456).mjd2000() == epoch(123.456, epoch::MJD2000).mjd2000());
REQUIRE(epoch(0.).mjd() == epoch(51544, epoch::MJD).mjd());
REQUIRE(epoch(0.).jd() == epoch(2451544.5, epoch::JD).jd());
REQUIRE(epoch(31, 12, 2034).jd() == epoch(posix_time_test).jd());
}
// // < 2000
// boost::posix_time::ptime posix_time_test2(date(1980, 12, 31));
// REQUIRE_NOTHROW(epoch(posix_time_test2));
// REQUIRE(epoch(posix_time_test2).get_posix_time() == posix_time_test2);
// std::cout << "Chrono2: " << epoch(posix_time_test2).get_posix_time() <<
// "\n"; std::cout << "Boost2: " << posix_time_test2 << "\n";

TEST_CASE("epoch_operators") {
REQUIRE(epoch(0.) == epoch(0.));
REQUIRE(epoch(0.) != epoch(1.));
REQUIRE(epoch(1.) > epoch(0.));
REQUIRE(epoch(1.) >= epoch(1.));
REQUIRE(epoch(1.) >= epoch(0.));
REQUIRE(epoch(0.) < epoch(1.));
REQUIRE(epoch(1.) <= epoch(1.));
epoch today(0.);
today += 100.;
REQUIRE(today == epoch(100.));
today -= 100.;
REQUIRE(today == epoch(0.));
auto yesterday = today - 1.;
REQUIRE(yesterday == epoch(-1.));
today = yesterday + 1;
REQUIRE(today == epoch(0.));
REQUIRE(today - yesterday == 1.);
REQUIRE_NOTHROW((std::cout << epoch()));
// // test conversions
// REQUIRE(epoch(123.456).mjd2000() ==
// epoch(123.456, epoch::julian_type::MJD2000).mjd2000());
// REQUIRE(epoch(0.).mjd() == epoch(51544, epoch::julian_type::MJD).mjd());
// REQUIRE(epoch(0.).jd() == epoch(2451544.5, epoch::julian_type::JD).jd());
// std::cout << "Chrono3: "
// << kep3::epoch::as_utc_string(epoch(posix_time_test).jd()) <<
// "\n";
// std::cout << "Chrono3-1: "
// << kep3::epoch::as_utc_string(epoch(2034, 12, 31).jd()) <<
// "\n";
// REQUIRE(epoch(2034, 12, 31) == epoch(posix_time_test));
}

TEST_CASE("conversions_from_string") {
{
std::string ts("20020131T000000");
epoch e(epoch_from_iso_string(ts));
REQUIRE(e == epoch(boost::posix_time::ptime(date(2002, 01, 31))));
}
{
std::string ts("2002-01-20 00:00:00.000");
epoch e(epoch_from_string(ts));
REQUIRE(e == epoch(boost::posix_time::ptime(date(2002, 01, 20))));
}
TEST_CASE( "epoch_operators" )
{
REQUIRE( epoch( 34, 10 ) == epoch( 34, 10 ) );
REQUIRE( epoch( 34, 10 ) != epoch( 34, 11 ) );
// Testing us precision
REQUIRE( epoch( 34, 10 ) != epoch( 34, 10, 0, 0, 0, 0, 1 ) );
// Check that ns precision is not supported
REQUIRE( epoch( 0, 10 ) ==
epoch( 0, 10, 0, 0, 0, 0, 0 ) + 100ns );

// Conversion from double (defaults to days)
REQUIRE( epoch( 1. ) > epoch( 0. ) );
REQUIRE( epoch( 1. ) >= epoch( 1. ) );
REQUIRE( epoch( 1. ) >= epoch( 0. ) );
REQUIRE( epoch( 0. ) < epoch( 1. ) );
REQUIRE( epoch( 1. ) <= epoch( 1. ) );
epoch today( 0. );
today += chr::days( 100 );
REQUIRE( today == epoch( 0, 100 ) );
today -= chr::duration_cast<kep_clock::duration>( chr::days( 100 ) );
REQUIRE( today == epoch() );
auto oneday = chr::days( 1 );
auto yesterday = today - chr::duration_cast<kep_clock::duration>( oneday );
auto yesterday1 = today - oneday;

REQUIRE( yesterday == epoch( 0, -1 ) );
today = yesterday + chr::duration_cast<kep_clock::duration>( chr::days( 1 ) );
REQUIRE( today == epoch() );
auto diff{ today - yesterday };
REQUIRE( diff == chr::duration_cast<kep_clock::duration>( chr::days( 1 ) ) );
REQUIRE_NOTHROW( ( std::cout << epoch() ) );
}

// TEST_CASE("conversions_from_string") {
// {
// std::string ts("20020131T000000");
// epoch e(epoch_from_iso_string(ts));
// REQUIRE(e == epoch(boost::posix_time::ptime(date(2002, 01, 31))));
// }
// {
// std::string ts("2002-01-20 00:00:00.000");
// epoch e(epoch_from_string(ts));
// REQUIRE(e == epoch(boost::posix_time::ptime(date(2002, 01, 20))));
// }
// }
110 changes: 0 additions & 110 deletions test/epoch_test_chrono.cpp

This file was deleted.

0 comments on commit f458bbc

Please sign in to comment.