Skip to content

Commit

Permalink
feat: added anytime to date module
Browse files Browse the repository at this point in the history
  • Loading branch information
tonykcao committed Oct 4, 2024
1 parent 3aef81d commit bea3567
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
13 changes: 13 additions & 0 deletions include/faker-cxx/date.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
13 changes: 12 additions & 1 deletion src/modules/date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "date_data.h"
#include "faker-cxx/helper.h"
#include "faker-cxx/number.h"

#include <iostream>
namespace faker::date
{
std::string serializeTimePoint(const auto& timePoint, DateFormat dateFormat)
Expand Down Expand Up @@ -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};
Expand Down
30 changes: 30 additions & 0 deletions tests/modules/date_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::chrono::years>(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<std::chrono::years>(currentDate - generatedDate).count()
);

EXPECT_TRUE(durationInYears < 101);
}

TEST_F(DateTest, shouldGenerateSoonDateISO)
{
const auto currentDate = std::chrono::system_clock::now();
Expand Down

0 comments on commit bea3567

Please sign in to comment.