Skip to content

Commit

Permalink
fix windows build
Browse files Browse the repository at this point in the history
  • Loading branch information
cieslarmichal committed Oct 5, 2023
1 parent f95bec1 commit b475fbd
Showing 1 changed file with 10 additions and 19 deletions.
29 changes: 10 additions & 19 deletions src/modules/date/Date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,33 @@
#include "data/WeekdayNames.h"
#include "faker-cxx/Helper.h"
#include "faker-cxx/Number.h"
#include "fmt/format.h"

namespace faker
{
namespace
{
std::string serializeTimePoint(
std::chrono::time_point<std::chrono::system_clock, std::chrono::duration<int64_t, std::ratio<1, 1000000000>>>
timePoint)
{
time_t timePointTimeT = std::chrono::system_clock::to_time_t(timePoint);

std::tm utcTime = *std::gmtime(&timePointTimeT);

std::stringstream ss;

ss << std::put_time(&utcTime, "%FT%TZ");

return ss.str();
}

std::string betweenDate(
std::chrono::time_point<std::chrono::system_clock, std::chrono::duration<int64_t, std::ratio<1, 1000000000>>> from,
std::chrono::time_point<std::chrono::system_clock, std::chrono::duration<int64_t, std::ratio<1, 1000000000>>> to)
{
if (from > to)
{
throw std::runtime_error{fmt::format("Start date is greater than end date. {{from: {}, to: {}}}",
serializeTimePoint(from), serializeTimePoint(to))};
throw std::runtime_error{"Start date is greater than end date."};
}

const auto size = std::chrono::duration_cast<std::chrono::seconds>(to - from).count();

const auto randomDateWithinRange = from + std::chrono::seconds{Number::integer(size - 1)};

return serializeTimePoint(std::chrono::floor<std::chrono::seconds>(randomDateWithinRange));
time_t timePointTimeT = std::chrono::system_clock::to_time_t(randomDateWithinRange);

std::tm utcTime = *std::gmtime(&timePointTimeT);

std::stringstream ss;

ss << std::put_time(&utcTime, "%FT%TZ");

return ss.str();
}

const auto numberOfHoursInDay = 24;
Expand Down

0 comments on commit b475fbd

Please sign in to comment.