Commit e5c016b9 authored by Cédric Traizet's avatar Cédric Traizet
Browse files

ENH: add date part getters (year, month etc) to TimePoint

parent 791e2042
......@@ -127,6 +127,24 @@ public:
/** Return the modified julian day corresponding to the time point */
double GetModifiedJulianDay() const;
/** Return the year corresponding to the time point */
int GetYear() const;
/** Return the month of the year corresponding to the time point (from 1 to 12) */
unsigned int GetMonth() const;
/** Return the calendar day corresponding to the time point (from 1 to 31) */
unsigned int GetDay() const;
/** Return the hour of the day corresponding to the time point (from 0 to 23) */
unsigned int GetHour() const;
/** Return the minute of the hour of the day corresponding to the time point (from 0 to 59) */
unsigned int GetMinute() const;
/** Return the fractional second of the minute of the hour of the day corresponding to the time point (from 0 and inferior to 60) */
double GetSecond() const;
friend TimePoint& operator+=(TimePoint & u, Duration const& v);
friend TimePoint& operator-=(TimePoint & u, Duration const& v);
......
......@@ -70,6 +70,48 @@ double TimePoint::GetModifiedJulianDay() const
return (m_Time.time_since_epoch().count() * details::internalPeriod) / 86400.0 + 40587;
}
date::year_month_day ToYMD(details::InternalTimePointType in)
{
auto dp = date::floor<date::days>(in);
return date::year_month_day(dp);
}
date::hh_mm_ss<details::InternalDurationType> ToHMS(details::InternalTimePointType in)
{
auto dp = date::floor<date::days>(in);
return date::make_time(std::chrono::duration_cast<details::InternalDurationType>(in-dp));
}
int TimePoint::GetYear() const
{
return static_cast<int>(ToYMD(m_Time).year());
}
unsigned int TimePoint::GetMonth() const
{
return static_cast<unsigned int>(ToYMD(m_Time).month());
}
unsigned int TimePoint::GetDay() const
{
return static_cast<unsigned int>(ToYMD(m_Time).day());
}
unsigned int TimePoint::GetHour() const
{
return ToHMS(m_Time).hours().count();
}
unsigned int TimePoint::GetMinute() const
{
return ToHMS(m_Time).minutes().count();
}
double TimePoint::GetSecond() const
{
return ToHMS(m_Time).seconds().count() + ToHMS(m_Time).subseconds().count() * details::internalPeriod;
}
double Duration::TotalSeconds() const
{
return m_Duration.count() * details::internalPeriod;
......
......@@ -57,6 +57,15 @@ BOOST_AUTO_TEST_CASE(Duration)
BOOST_AUTO_TEST_CASE(TimePoint)
{
auto date1 = otb::MetaData::ReadFormattedDate("2021-06-22T00:01:04.42578987");
// Test date part getters
BOOST_TEST(date1.GetYear() == 2021);
BOOST_TEST(date1.GetMonth() == 06);
BOOST_TEST(date1.GetDay() == 22);
BOOST_TEST(date1.GetHour() == 0);
BOOST_TEST(date1.GetMinute() == 1);
BOOST_TEST(date1.GetSecond() == 4.42578987);
auto date2 = otb::MetaData::ReadFormattedDate("2021-06-21T00:01:04.42578987");
BOOST_TEST(date1 > date2);
......@@ -67,7 +76,6 @@ BOOST_AUTO_TEST_CASE(TimePoint)
// Test Parsing with a different format
otb::MetaData::ReadFormattedDate("2021-06-22 00:01:04.42578987", "%Y-%m-%d %H:%M:%S");
// Test some invalid cases
BOOST_REQUIRE_THROW(otb::MetaData::ReadFormattedDate("2021-06-21 00:01:04.42578987"),
otb::MissingMetadataException);
......@@ -78,7 +86,6 @@ BOOST_AUTO_TEST_CASE(TimePoint)
BOOST_REQUIRE_THROW(otb::MetaData::ReadFormattedDate("2021-6-21 00:1:4.42578987"),
otb::MissingMetadataException);
}
BOOST_AUTO_TEST_CASE(TimePointPrecision)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment