From bea3567a2282a04a0a0f5286b4ae55e178afa22a Mon Sep 17 00:00:00 2001 From: Tony Cao Date: Fri, 4 Oct 2024 15:27:57 -0400 Subject: [PATCH] feat: added anytime to date module --- include/faker-cxx/date.h | 13 +++++++++++++ src/modules/date.cpp | 13 ++++++++++++- tests/modules/date_test.cpp | 30 ++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/include/faker-cxx/date.h b/include/faker-cxx/date.h index f8eec56bc..250c21e9a 100644 --- a/include/faker-cxx/date.h +++ b/include/faker-cxx/date.h @@ -13,6 +13,19 @@ enum class DateFormat Timestamp, }; +/** + * @brief Generates a random date in +- 100 years from current date + * + * @returns ISO formatted string. + * + * @code + * faker::date::anytime() // "2023-12-08T19:31:32Z" + * faker::date::anytime(DateFormat::ISO) // "2020-06-16T15:24:09Z" + * faker::date::anytime(DateFormat::Timestamp) // "1592321049" + * @endcode + */ +FAKER_CXX_EXPORT std::string anytime(DateFormat dateFormat = DateFormat::ISO); + /** * @brief Generates a random date in the past. * diff --git a/src/modules/date.cpp b/src/modules/date.cpp index d2ddcba98..593a57a9a 100644 --- a/src/modules/date.cpp +++ b/src/modules/date.cpp @@ -11,7 +11,7 @@ #include "date_data.h" #include "faker-cxx/helper.h" #include "faker-cxx/number.h" - +#include namespace faker::date { std::string serializeTimePoint(const auto& timePoint, DateFormat dateFormat) @@ -46,6 +46,17 @@ std::string betweenDate(const auto& from, const auto& to, DateFormat dateFormat) const auto numberOfHoursInDay = 24; const auto numberOfDaysInYear = 365; +std::string anytime(DateFormat dateFormat) +{ + const auto startDate = + std::chrono::system_clock::now() - std::chrono::hours{numberOfHoursInDay * numberOfDaysInYear * 100}; + + const auto endDate = + std::chrono::system_clock::now() + std::chrono::hours{numberOfHoursInDay * numberOfDaysInYear * 100}; + + return betweenDate(startDate, endDate, dateFormat); +} + std::string futureDate(int years, DateFormat dateFormat) { const auto startDate = std::chrono::system_clock::now() + std::chrono::hours{1}; diff --git a/tests/modules/date_test.cpp b/tests/modules/date_test.cpp index 7ae2b4e26..a966cadbb 100644 --- a/tests/modules/date_test.cpp +++ b/tests/modules/date_test.cpp @@ -127,6 +127,36 @@ TEST_F(DateTest, shouldGenerateFutureDateTimestamp) EXPECT_TRUE(futureDate > currentDate); } +TEST_F(DateTest, shouldGenerateAnytimeISO) +{ + const auto currentDate = std::chrono::system_clock::now(); + + const auto anytimeISO = anytime(); + + const auto generatedDate = parseISOFormattedStringToTimePoint(anytimeISO); + + const auto durationInYears = std::abs( + std::chrono::duration_cast(currentDate - generatedDate).count() + ); + + EXPECT_TRUE(durationInYears < 101); +} + +TEST_F(DateTest, shouldGenerateAnytimeTimestamp) +{ + const auto currentDate = std::chrono::system_clock::now(); + + const auto anytimeTimestamp = anytime(DateFormat::Timestamp); + + const auto generatedDate = std::chrono::system_clock::from_time_t(std::stoi(anytimeTimestamp)); + + const auto durationInYears = std::abs( + std::chrono::duration_cast(currentDate - generatedDate).count() + ); + + EXPECT_TRUE(durationInYears < 101); +} + TEST_F(DateTest, shouldGenerateSoonDateISO) { const auto currentDate = std::chrono::system_clock::now();