From d436890d83e311cfd37a1b397982a2e6ea9f6b8f Mon Sep 17 00:00:00 2001 From: Michal Cieslar Date: Sun, 1 Oct 2023 14:48:34 +0200 Subject: [PATCH 1/6] fix parsing iso date in date tests --- src/modules/date/Date.cpp | 1 + src/modules/date/DateTest.cpp | 38 +++++++---------------------------- 2 files changed, 8 insertions(+), 31 deletions(-) diff --git a/src/modules/date/Date.cpp b/src/modules/date/Date.cpp index c2a1755d8..24f3472db 100644 --- a/src/modules/date/Date.cpp +++ b/src/modules/date/Date.cpp @@ -1,6 +1,7 @@ #include "faker-cxx/Date.h" #include +#include #include "data/MonthNames.h" #include "data/WeekdayNames.h" diff --git a/src/modules/date/DateTest.cpp b/src/modules/date/DateTest.cpp index e547aae19..13df71ece 100644 --- a/src/modules/date/DateTest.cpp +++ b/src/modules/date/DateTest.cpp @@ -1,12 +1,10 @@ #include "faker-cxx/Date.h" #include -#include #include #include "gtest/gtest.h" -#include "../../common/StringHelper.h" #include "data/MonthNames.h" #include "data/WeekdayNames.h" @@ -28,39 +26,17 @@ class DateTest : public Test { auto dateTime = parseISOFormattedStringToTm(isoString); - return std::chrono::system_clock::from_time_t(mktime(&dateTime)); + return std::chrono::system_clock::from_time_t(timegm(&dateTime)); } static tm parseISOFormattedStringToTm(const std::string& isoString) { - const auto isoStringDateTime = StringHelper::split(isoString, "T"); - - const auto& date = isoStringDateTime[0]; - const auto dateElements = StringHelper::split(date, "-"); - int year, month, day; - std::from_chars(dateElements[0].data(), dateElements[0].data() + dateElements[0].size(), year); - std::from_chars(dateElements[1].data(), dateElements[1].data() + dateElements[1].size(), month); - std::from_chars(dateElements[2].data(), dateElements[2].data() + dateElements[2].size(), day); - - const auto& time = isoStringDateTime[1]; - const auto timeElements = StringHelper::split(time, ":"); - - int hour, minutes, seconds; - std::from_chars(timeElements[0].data(), timeElements[0].data() + timeElements[0].size(), hour); - std::from_chars(timeElements[1].data(), timeElements[1].data() + timeElements[1].size(), minutes); - std::from_chars(timeElements[2].data(), timeElements[2].data() + timeElements[2].size(), seconds); - - tm dateTime{}; - - dateTime.tm_year = year - 1900; - dateTime.tm_mon = month - 1; - dateTime.tm_mday = day; - dateTime.tm_hour = hour; - dateTime.tm_min = minutes; - dateTime.tm_sec = seconds; - dateTime.tm_isdst = -1; - - return dateTime; + tm tm{}; + + std::istringstream iss(isoString); + iss >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%SZ"); + + return tm; } }; From c87304c76fad782b0ebe547d64e2157e7fa2c59a Mon Sep 17 00:00:00 2001 From: Michal Cieslar Date: Sun, 1 Oct 2023 14:50:04 +0200 Subject: [PATCH 2/6] merge --- src/modules/date/Date.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/date/Date.cpp b/src/modules/date/Date.cpp index 78edfa29d..24f3472db 100644 --- a/src/modules/date/Date.cpp +++ b/src/modules/date/Date.cpp @@ -85,7 +85,7 @@ std::string Date::birthdateByYear(int minYear, int maxYear) tm startDateTime{}; startDateTime.tm_year = minYear - 1900; startDateTime.tm_mon = 0; - startDateTime.tm_mday = 1; + startDateTime.tm_mday = 0; startDateTime.tm_hour = 0; startDateTime.tm_min = 0; startDateTime.tm_sec = 0; From 245b5e59060d7377d9aee628d92bb285f26a0b18 Mon Sep 17 00:00:00 2001 From: Michal Cieslar Date: Sun, 1 Oct 2023 14:53:23 +0200 Subject: [PATCH 3/6] merge v2 --- src/modules/date/Date.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/date/Date.cpp b/src/modules/date/Date.cpp index 24f3472db..78edfa29d 100644 --- a/src/modules/date/Date.cpp +++ b/src/modules/date/Date.cpp @@ -85,7 +85,7 @@ std::string Date::birthdateByYear(int minYear, int maxYear) tm startDateTime{}; startDateTime.tm_year = minYear - 1900; startDateTime.tm_mon = 0; - startDateTime.tm_mday = 0; + startDateTime.tm_mday = 1; startDateTime.tm_hour = 0; startDateTime.tm_min = 0; startDateTime.tm_sec = 0; From a41dc5368b8eda09ff9e915269209af1923a4efe Mon Sep 17 00:00:00 2001 From: Michal Cieslar Date: Sun, 1 Oct 2023 14:55:26 +0200 Subject: [PATCH 4/6] remove iostream header from date --- src/modules/date/Date.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/modules/date/Date.cpp b/src/modules/date/Date.cpp index 78edfa29d..3bea03f9b 100644 --- a/src/modules/date/Date.cpp +++ b/src/modules/date/Date.cpp @@ -1,7 +1,6 @@ #include "faker-cxx/Date.h" #include -#include #include "data/MonthNames.h" #include "data/WeekdayNames.h" From dfed28b428ae9094ae14e337ee9c7cdf86201970 Mon Sep 17 00:00:00 2001 From: Michal Cieslar Date: Sun, 1 Oct 2023 15:08:55 +0200 Subject: [PATCH 5/6] add ctime header> --- src/modules/date/Date.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/modules/date/Date.cpp b/src/modules/date/Date.cpp index 3bea03f9b..08e5378e9 100644 --- a/src/modules/date/Date.cpp +++ b/src/modules/date/Date.cpp @@ -1,6 +1,7 @@ #include "faker-cxx/Date.h" #include +#include #include "data/MonthNames.h" #include "data/WeekdayNames.h" From a5b1c983f0ca8d8040d7e85a540df66510918e75 Mon Sep 17 00:00:00 2001 From: Michal Cieslar Date: Sun, 1 Oct 2023 15:15:05 +0200 Subject: [PATCH 6/6] add gmtime msvc macro --- src/modules/date/DateTest.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/modules/date/DateTest.cpp b/src/modules/date/DateTest.cpp index 13df71ece..5f3b68630 100644 --- a/src/modules/date/DateTest.cpp +++ b/src/modules/date/DateTest.cpp @@ -8,6 +8,10 @@ #include "data/MonthNames.h" #include "data/WeekdayNames.h" +#ifdef _WIN32 +#define timegm _mkgmtime +#endif + using namespace ::testing; using namespace faker;