Adding support for partial dates (only year or month), fixes #477
This commit is contained in:
@@ -12,6 +12,7 @@ add_library(lmscore SHARED
|
||||
impl/IOContextRunner.cpp
|
||||
impl/Logger.cpp
|
||||
impl/NetAddress.cpp
|
||||
impl/PartialDateTime.cpp
|
||||
impl/Path.cpp
|
||||
impl/Random.cpp
|
||||
impl/RecursiveSharedMutex.cpp
|
||||
|
||||
@@ -0,0 +1,155 @@
|
||||
/*
|
||||
* Copyright (C) 2025 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include "core/PartialDateTime.hpp"
|
||||
|
||||
#include <iomanip>
|
||||
#include <limits>
|
||||
#include <sstream>
|
||||
|
||||
namespace lms::core
|
||||
{
|
||||
PartialDateTime::PartialDateTime(int year)
|
||||
: _year{ static_cast<std::int16_t>(year) }
|
||||
, _precision{ Precision::Year }
|
||||
{
|
||||
}
|
||||
|
||||
PartialDateTime::PartialDateTime(int year, unsigned month)
|
||||
: _year{ static_cast<std::int16_t>(year) }
|
||||
, _month{ static_cast<std::uint8_t>(month) }
|
||||
, _precision{ Precision::Month }
|
||||
{
|
||||
}
|
||||
|
||||
PartialDateTime::PartialDateTime(int year, unsigned month, unsigned day)
|
||||
: _year{ static_cast<std::int16_t>(year) }
|
||||
, _month{ static_cast<std::uint8_t>(month) }
|
||||
, _day{ static_cast<std::uint8_t>(day) }
|
||||
, _precision{ Precision::Day }
|
||||
{
|
||||
}
|
||||
|
||||
PartialDateTime::PartialDateTime(int year, unsigned month, unsigned day, unsigned hour, unsigned min, unsigned sec)
|
||||
: _year{ static_cast<std::int16_t>(year) }
|
||||
, _month{ static_cast<std::uint8_t>(month) }
|
||||
, _day{ static_cast<std::uint8_t>(day) }
|
||||
, _hour{ static_cast<std::uint8_t>(hour) }
|
||||
, _min{ static_cast<std::uint8_t>(min) }
|
||||
, _sec{ static_cast<std::uint8_t>(sec) }
|
||||
, _precision{ Precision::Sec }
|
||||
{
|
||||
}
|
||||
|
||||
PartialDateTime PartialDateTime::fromString(std::string_view str)
|
||||
{
|
||||
PartialDateTime res;
|
||||
|
||||
const std::string dateTimeStr{ str };
|
||||
|
||||
static constexpr const char* formats[]{
|
||||
"%Y-%m-%dT%H:%M:%S",
|
||||
"%Y-%m-%d %H:%M:%S",
|
||||
"%Y/%m/%d %H:%M:%S",
|
||||
};
|
||||
|
||||
for (const char* format : formats)
|
||||
{
|
||||
PartialDateTime candidate;
|
||||
|
||||
std::tm tm{};
|
||||
tm.tm_year = std::numeric_limits<decltype(tm.tm_year)>::min();
|
||||
tm.tm_mon = std::numeric_limits<decltype(tm.tm_mon)>::min();
|
||||
tm.tm_mday = std::numeric_limits<decltype(tm.tm_mday)>::min();
|
||||
tm.tm_hour = std::numeric_limits<decltype(tm.tm_hour)>::min();
|
||||
tm.tm_min = std::numeric_limits<decltype(tm.tm_min)>::min();
|
||||
tm.tm_sec = std::numeric_limits<decltype(tm.tm_sec)>::min();
|
||||
|
||||
std::istringstream ss{ dateTimeStr };
|
||||
ss >> std::get_time(&tm, format);
|
||||
if (ss.fail())
|
||||
continue;
|
||||
|
||||
if (tm.tm_sec != std::numeric_limits<decltype(tm.tm_sec)>::min())
|
||||
{
|
||||
candidate._sec = tm.tm_sec;
|
||||
candidate._precision = Precision::Sec;
|
||||
}
|
||||
if (tm.tm_min != std::numeric_limits<decltype(tm.tm_min)>::min())
|
||||
{
|
||||
candidate._min = tm.tm_min;
|
||||
if (candidate._precision == Precision::Invalid)
|
||||
candidate._precision = Precision::Min;
|
||||
}
|
||||
if (tm.tm_hour != std::numeric_limits<decltype(tm.tm_hour)>::min())
|
||||
{
|
||||
candidate._hour = tm.tm_hour;
|
||||
if (candidate._precision == Precision::Invalid)
|
||||
candidate._precision = Precision::Hour;
|
||||
}
|
||||
if (tm.tm_mday != std::numeric_limits<decltype(tm.tm_mday)>::min())
|
||||
{
|
||||
candidate._day = tm.tm_mday;
|
||||
if (candidate._precision == Precision::Invalid)
|
||||
candidate._precision = Precision::Day;
|
||||
}
|
||||
if (tm.tm_mon != std::numeric_limits<decltype(tm.tm_mon)>::min())
|
||||
{
|
||||
candidate._month = tm.tm_mon + 1; // tm.tm_mon is [0, 11]
|
||||
if (candidate._precision == Precision::Invalid)
|
||||
candidate._precision = Precision::Month;
|
||||
}
|
||||
if (tm.tm_year != std::numeric_limits<decltype(tm.tm_year)>::min())
|
||||
{
|
||||
candidate._year = tm.tm_year + 1900; // tm.tm_year is years since 1900
|
||||
if (candidate._precision == Precision::Invalid)
|
||||
candidate._precision = Precision::Year;
|
||||
}
|
||||
|
||||
if (candidate > res)
|
||||
res = candidate;
|
||||
|
||||
if (res._precision == Precision::Sec)
|
||||
break;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
std::string PartialDateTime::toISO8601String() const
|
||||
{
|
||||
if (_precision == Precision::Invalid)
|
||||
return "";
|
||||
|
||||
std::ostringstream ss;
|
||||
|
||||
ss << std::setfill('0') << std::setw(4) << _year;
|
||||
if (_precision >= Precision::Month)
|
||||
ss << "-" << std::setw(2) << static_cast<int>(_month);
|
||||
if (_precision >= Precision::Day)
|
||||
ss << "-" << std::setw(2) << static_cast<int>(_day);
|
||||
if (_precision >= Precision::Hour)
|
||||
ss << 'T' << std::setw(2) << static_cast<int>(_hour);
|
||||
if (_precision >= Precision::Min)
|
||||
ss << ':' << std::setw(2) << static_cast<int>(_min);
|
||||
if (_precision >= Precision::Sec)
|
||||
ss << ':' << std::setw(2) << static_cast<int>(_sec);
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
} // namespace lms::core
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright (C) 2025 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
namespace lms::core
|
||||
{
|
||||
class PartialDateTime
|
||||
{
|
||||
public:
|
||||
constexpr PartialDateTime() = default;
|
||||
PartialDateTime(int year);
|
||||
PartialDateTime(int year, unsigned month);
|
||||
PartialDateTime(int year, unsigned month, unsigned day);
|
||||
PartialDateTime(int year, unsigned month, unsigned day, unsigned hour, unsigned min, unsigned sec);
|
||||
|
||||
static PartialDateTime fromString(std::string_view str);
|
||||
std::string toISO8601String() const;
|
||||
|
||||
bool isValid() const { return _precision != Precision::Invalid; }
|
||||
|
||||
constexpr std::optional<int> getYear() const { return (_precision >= Precision::Year ? std::make_optional(_year) : std::nullopt); }
|
||||
constexpr std::optional<int> getMonth() const { return (_precision >= Precision::Month ? std::make_optional(_month) : std::nullopt); }
|
||||
constexpr std::optional<int> getDay() const { return (_precision >= Precision::Day ? std::make_optional(_day) : std::nullopt); }
|
||||
|
||||
constexpr auto operator<=>(const PartialDateTime& other) const = default;
|
||||
|
||||
private:
|
||||
std::int16_t _year{};
|
||||
std::uint8_t _month{}; // 1 to 12
|
||||
std::uint8_t _day{}; // 1 to 31
|
||||
std::uint8_t _hour{}; // 0 to 23
|
||||
std::uint8_t _min{}; // 0 to 59
|
||||
std::uint8_t _sec{}; // 0 to 59
|
||||
enum class Precision : std::uint8_t
|
||||
{
|
||||
Invalid,
|
||||
Year,
|
||||
Month,
|
||||
Day,
|
||||
Hour,
|
||||
Min,
|
||||
Sec,
|
||||
};
|
||||
Precision _precision{ Precision::Invalid };
|
||||
};
|
||||
} // namespace lms::core
|
||||
@@ -3,6 +3,7 @@ include(GoogleTest)
|
||||
add_executable(test-core
|
||||
EnumSet.cpp
|
||||
LiteralString.cpp
|
||||
PartialDateTime.cpp
|
||||
Path.cpp
|
||||
RecursiveSharedMutex.cpp
|
||||
Service.cpp
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
* Copyright (C) 2025 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "core/PartialDateTime.hpp"
|
||||
|
||||
namespace lms::core::stringUtils::tests
|
||||
{
|
||||
TEST(PartialDateTime, year)
|
||||
{
|
||||
EXPECT_EQ(PartialDateTime{}.getYear(), std::nullopt);
|
||||
EXPECT_EQ(PartialDateTime{ 1992 }.getYear(), 1992);
|
||||
}
|
||||
|
||||
TEST(PartialDateTime, month)
|
||||
{
|
||||
EXPECT_EQ(PartialDateTime{}.getMonth(), std::nullopt);
|
||||
EXPECT_EQ((PartialDateTime{ 1992, 3 }.getMonth()), std::optional<int>{ 3 });
|
||||
}
|
||||
TEST(PartialDateTime, day)
|
||||
{
|
||||
EXPECT_EQ(PartialDateTime{}.getDay(), std::nullopt);
|
||||
EXPECT_EQ((PartialDateTime{ 1992, 3, 27 }.getDay()), std::optional<int>{ 27 });
|
||||
}
|
||||
|
||||
TEST(PartialDateTime, comparison)
|
||||
{
|
||||
EXPECT_EQ((PartialDateTime{ 1992, 3, 27 }), (PartialDateTime{ 1992, 3, 27 }));
|
||||
EXPECT_EQ((PartialDateTime{ 1992, 3 }), (PartialDateTime{ 1992, 3 }));
|
||||
EXPECT_EQ(PartialDateTime{ 1992 }, PartialDateTime{ 1992 });
|
||||
EXPECT_NE((PartialDateTime{ 1992, 3, 27 }), (PartialDateTime{ 1992, 3 }));
|
||||
EXPECT_NE((PartialDateTime{ 1992, 3, 27 }), (PartialDateTime{ 1992 }));
|
||||
EXPECT_NE((PartialDateTime{ 1992, 3 }), (PartialDateTime{ 1992 }));
|
||||
EXPECT_NE((PartialDateTime{ 1992, 3, 27 }), (PartialDateTime{ 1992, 3, 28 }));
|
||||
EXPECT_NE((PartialDateTime{ 1992, 3, 27 }), (PartialDateTime{ 1992, 4, 27 }));
|
||||
EXPECT_NE((PartialDateTime{ 1992, 3, 27 }), (PartialDateTime{ 1993, 3, 27 }));
|
||||
EXPECT_GT((PartialDateTime{ 1993, 3, 28 }), (PartialDateTime{ 1993, 3, 27 }));
|
||||
EXPECT_GT((PartialDateTime{ 1993, 4 }), (PartialDateTime{ 1993, 3, 27 }));
|
||||
EXPECT_GT((PartialDateTime{ 1994 }), (PartialDateTime{ 1993, 3, 27 }));
|
||||
EXPECT_LT((PartialDateTime{ 1993, 3, 27 }), (PartialDateTime{ 1993, 3, 28 }));
|
||||
EXPECT_LT((PartialDateTime{ 1993, 3, 27 }), (PartialDateTime{ 1993, 4 }));
|
||||
EXPECT_LT((PartialDateTime{ 1993, 3, 27 }), (PartialDateTime{ 1994 }));
|
||||
}
|
||||
|
||||
TEST(PartialDateTime, stringComparison)
|
||||
{
|
||||
EXPECT_EQ((PartialDateTime{ 1992, 3, 27 }.toISO8601String()), (PartialDateTime{ 1992, 3, 27 }.toISO8601String()));
|
||||
EXPECT_EQ((PartialDateTime{ 1992, 3 }.toISO8601String()), (PartialDateTime{ 1992, 3 }.toISO8601String()));
|
||||
EXPECT_EQ(PartialDateTime{ 1992 }.toISO8601String(), PartialDateTime{ 1992 }.toISO8601String());
|
||||
EXPECT_NE((PartialDateTime{ 1992, 3, 27 }.toISO8601String()), (PartialDateTime{ 1992, 3 }.toISO8601String()));
|
||||
EXPECT_NE((PartialDateTime{ 1992, 3, 27 }.toISO8601String()), (PartialDateTime{ 1992 }.toISO8601String()));
|
||||
EXPECT_NE((PartialDateTime{ 1992, 3 }.toISO8601String()), (PartialDateTime{ 1992 }.toISO8601String()));
|
||||
EXPECT_NE((PartialDateTime{ 1992, 3, 27 }.toISO8601String()), (PartialDateTime{ 1992, 3, 28 }.toISO8601String()));
|
||||
EXPECT_NE((PartialDateTime{ 1992, 3, 27 }.toISO8601String()), (PartialDateTime{ 1992, 4, 27 }.toISO8601String()));
|
||||
EXPECT_NE((PartialDateTime{ 1992, 3, 27 }.toISO8601String()), (PartialDateTime{ 1993, 3, 27 }.toISO8601String()));
|
||||
EXPECT_GT((PartialDateTime{ 1993, 3, 28 }.toISO8601String()), (PartialDateTime{ 1993, 3, 27 }.toISO8601String()));
|
||||
EXPECT_GT((PartialDateTime{ 1993, 4 }.toISO8601String()), (PartialDateTime{ 1993, 3, 27 }.toISO8601String()));
|
||||
EXPECT_GT((PartialDateTime{ 1994 }.toISO8601String()), (PartialDateTime{ 1993, 3, 27 }.toISO8601String()));
|
||||
EXPECT_LT((PartialDateTime{ 1993, 3, 27 }.toISO8601String()), (PartialDateTime{ 1993, 3, 28 }.toISO8601String()));
|
||||
EXPECT_LT((PartialDateTime{ 1993, 3, 27 }.toISO8601String()), (PartialDateTime{ 1993, 4 }.toISO8601String()));
|
||||
EXPECT_LT((PartialDateTime{ 1993, 3, 27 }.toISO8601String()), (PartialDateTime{ 1994 }.toISO8601String()));
|
||||
}
|
||||
|
||||
TEST(PartialDateTime, stringConversions)
|
||||
{
|
||||
struct TestCase
|
||||
{
|
||||
std::string_view input;
|
||||
std::string_view expectedOutput;
|
||||
};
|
||||
|
||||
constexpr TestCase tests[]{
|
||||
{ "1992", "1992" },
|
||||
{ "1992-03", "1992-03" },
|
||||
{ "1992-03-27", "1992-03-27" },
|
||||
{ "1992-03-27T15", "1992-03-27T15" },
|
||||
{ "1992-03-27T15:08", "1992-03-27T15:08" },
|
||||
{ "1992-03-27T15:08:57", "1992-03-27T15:08:57" },
|
||||
|
||||
{ "1992-03-27 15", "1992-03-27T15" },
|
||||
{ "1992-03-27 15:08", "1992-03-27T15:08" },
|
||||
{ "1992-03-27 15:08:57", "1992-03-27T15:08:57" },
|
||||
|
||||
{ "1992", "1992" },
|
||||
{ "1992/03", "1992-03" },
|
||||
{ "1992/03/27", "1992-03-27" },
|
||||
{ "1992/03/27 15", "1992-03-27T15" },
|
||||
{ "1992/03/27 15:08", "1992-03-27T15:08" },
|
||||
{ "1992/03/27 15:08:57", "1992-03-27T15:08:57" },
|
||||
};
|
||||
|
||||
for (const TestCase& test : tests)
|
||||
{
|
||||
const PartialDateTime dateTime{ PartialDateTime::fromString(test.input) };
|
||||
EXPECT_EQ(dateTime.toISO8601String(), test.expectedOutput) << "Input = '" << test.input;
|
||||
}
|
||||
}
|
||||
} // namespace lms::core::stringUtils::tests
|
||||
Reference in New Issue
Block a user