Skip to content

Commit

Permalink
Fix #160 (#161)
Browse files Browse the repository at this point in the history
Always use `date.h` for C++ `yardl::Date`
  • Loading branch information
naegelejd authored Jul 2, 2024
1 parent 39a6273 commit 863223d
Show file tree
Hide file tree
Showing 46 changed files with 1,138 additions and 180 deletions.
12 changes: 0 additions & 12 deletions cpp/evolution/v0/generated/yardl/detail/binary/serializers.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
#include "../../yardl.h"
#include "coded_stream.h"

#if __cplusplus >= 202002L
#include <bit>
#endif

namespace yardl::binary {

/**
Expand Down Expand Up @@ -177,11 +173,7 @@ inline void WriteDate(CodedOutputStream& stream, yardl::Date const& value) {
inline void ReadDate(CodedInputStream& stream, yardl::Date& value) {
int64_t days;
ReadInteger(stream, days);
#if __cplusplus < 202002L
value = yardl::Date(date::days(days));
#else
value = yardl::Date{std::chrono::days{days}};
#endif
}

inline void WriteTime(CodedOutputStream& stream, yardl::Time const& value) {
Expand All @@ -202,13 +194,9 @@ inline void WriteDateTime(CodedOutputStream& stream, yardl::DateTime const& valu
inline void ReadDateTime(CodedInputStream& stream, yardl::DateTime& value) {
int64_t ns;
ReadInteger(stream, ns);
#if __cplusplus < 202002L
value = yardl::DateTime(date::sys_time<std::chrono::nanoseconds>(std::chrono::nanoseconds(ns)));
#else
value = yardl::DateTime{
std::chrono::time_point<std::chrono::system_clock,
std::chrono::nanoseconds>(std::chrono::nanoseconds(ns))};
#endif
}

template <typename T, Writer<T> WriteElement>
Expand Down
6 changes: 0 additions & 6 deletions cpp/evolution/v0/generated/yardl/detail/hdf5/ddl.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,9 @@ static inline H5::PredType const& SizeTypeDdl() {
* of days since the epoch.
*/
static inline H5::DataType DateTypeDdl() {
#if __cplusplus < 202002L
static_assert(sizeof(yardl::Date) == sizeof(int32_t));
static_assert(std::is_same_v<yardl::Date::rep, int32_t>);
return H5::PredType::NATIVE_INT32;
#else
static_assert(sizeof(yardl::Date) == sizeof(int64_t));
static_assert(std::is_same_v<yardl::Date::rep, int64_t>);
return H5::PredType::NATIVE_INT64;
#endif
}

/**
Expand Down
13 changes: 0 additions & 13 deletions cpp/evolution/v0/generated/yardl/detail/ndjson/serializers.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,25 +125,12 @@ struct adl_serializer<std::monostate> {
template <>
struct adl_serializer<yardl::Date> {
static void to_json(ordered_json& j, yardl::Date const& value) {
#if __cplusplus < 202002L
j = date::format("%F", value);
#else
date::local_days ld(value.time_since_epoch());
j = date::format("%F", ld);
#endif
}

static void from_json(ordered_json const& j, yardl::Date& value) {
std::stringstream ss{j.get<std::string>()};

#if __cplusplus < 202002L
ss >> date::parse("%F", value);
#else
date::local_days ld;
ss >> date::parse("%F", ld);
value = yardl::Date(ld.time_since_epoch());
#endif

if (ss.fail()) {
throw std::runtime_error("invalid date format");
}
Expand Down
14 changes: 1 addition & 13 deletions cpp/evolution/v0/generated/yardl/yardl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@

#include <chrono>

#if __cplusplus < 202002L
// This functionality is part of the standard library as of C++20
#include <date/date.h>
#endif

#include <xtensor/xarray.hpp>
#include <xtensor/xfixed.hpp>
#include <xtensor/xtensor.hpp>
Expand Down Expand Up @@ -46,17 +42,10 @@ using NDArray = xt::xtensor<T, N, xt::layout_type::row_major>;
template <typename T>
using DynamicNDArray = xt::xarray<T, xt::layout_type::row_major>;

#if __cplusplus < 202002L
/**
* @brief Represents a date as a number of days since the epoch.
*/
using Date = date::local_days;
#else
/**
* @brief Represents a date as a number of days since the epoch.
*/
using Date = std::chrono::local_days;
#endif

/**
* @brief Represents a time of day as the number of nanoseconds since midnight.
Expand All @@ -66,8 +55,7 @@ using Time = std::chrono::duration<int64_t, std::nano>;
/**
* @brief Represents a datetime as the number of nanoseconds since the epoch.
*/
using DateTime = std::chrono::time_point<std::chrono::system_clock,
std::chrono::duration<int64_t, std::nano>>;
using DateTime = std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>;

/**
* @brief The same as size_t when it is 64 bits, otherwise uint64_t.
Expand Down
12 changes: 0 additions & 12 deletions cpp/evolution/v1/generated/yardl/detail/binary/serializers.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
#include "../../yardl.h"
#include "coded_stream.h"

#if __cplusplus >= 202002L
#include <bit>
#endif

namespace yardl::binary {

/**
Expand Down Expand Up @@ -177,11 +173,7 @@ inline void WriteDate(CodedOutputStream& stream, yardl::Date const& value) {
inline void ReadDate(CodedInputStream& stream, yardl::Date& value) {
int64_t days;
ReadInteger(stream, days);
#if __cplusplus < 202002L
value = yardl::Date(date::days(days));
#else
value = yardl::Date{std::chrono::days{days}};
#endif
}

inline void WriteTime(CodedOutputStream& stream, yardl::Time const& value) {
Expand All @@ -202,13 +194,9 @@ inline void WriteDateTime(CodedOutputStream& stream, yardl::DateTime const& valu
inline void ReadDateTime(CodedInputStream& stream, yardl::DateTime& value) {
int64_t ns;
ReadInteger(stream, ns);
#if __cplusplus < 202002L
value = yardl::DateTime(date::sys_time<std::chrono::nanoseconds>(std::chrono::nanoseconds(ns)));
#else
value = yardl::DateTime{
std::chrono::time_point<std::chrono::system_clock,
std::chrono::nanoseconds>(std::chrono::nanoseconds(ns))};
#endif
}

template <typename T, Writer<T> WriteElement>
Expand Down
6 changes: 0 additions & 6 deletions cpp/evolution/v1/generated/yardl/detail/hdf5/ddl.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,9 @@ static inline H5::PredType const& SizeTypeDdl() {
* of days since the epoch.
*/
static inline H5::DataType DateTypeDdl() {
#if __cplusplus < 202002L
static_assert(sizeof(yardl::Date) == sizeof(int32_t));
static_assert(std::is_same_v<yardl::Date::rep, int32_t>);
return H5::PredType::NATIVE_INT32;
#else
static_assert(sizeof(yardl::Date) == sizeof(int64_t));
static_assert(std::is_same_v<yardl::Date::rep, int64_t>);
return H5::PredType::NATIVE_INT64;
#endif
}

/**
Expand Down
13 changes: 0 additions & 13 deletions cpp/evolution/v1/generated/yardl/detail/ndjson/serializers.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,25 +125,12 @@ struct adl_serializer<std::monostate> {
template <>
struct adl_serializer<yardl::Date> {
static void to_json(ordered_json& j, yardl::Date const& value) {
#if __cplusplus < 202002L
j = date::format("%F", value);
#else
date::local_days ld(value.time_since_epoch());
j = date::format("%F", ld);
#endif
}

static void from_json(ordered_json const& j, yardl::Date& value) {
std::stringstream ss{j.get<std::string>()};

#if __cplusplus < 202002L
ss >> date::parse("%F", value);
#else
date::local_days ld;
ss >> date::parse("%F", ld);
value = yardl::Date(ld.time_since_epoch());
#endif

if (ss.fail()) {
throw std::runtime_error("invalid date format");
}
Expand Down
14 changes: 1 addition & 13 deletions cpp/evolution/v1/generated/yardl/yardl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@

#include <chrono>

#if __cplusplus < 202002L
// This functionality is part of the standard library as of C++20
#include <date/date.h>
#endif

#include <xtensor/xarray.hpp>
#include <xtensor/xfixed.hpp>
#include <xtensor/xtensor.hpp>
Expand Down Expand Up @@ -46,17 +42,10 @@ using NDArray = xt::xtensor<T, N, xt::layout_type::row_major>;
template <typename T>
using DynamicNDArray = xt::xarray<T, xt::layout_type::row_major>;

#if __cplusplus < 202002L
/**
* @brief Represents a date as a number of days since the epoch.
*/
using Date = date::local_days;
#else
/**
* @brief Represents a date as a number of days since the epoch.
*/
using Date = std::chrono::local_days;
#endif

/**
* @brief Represents a time of day as the number of nanoseconds since midnight.
Expand All @@ -66,8 +55,7 @@ using Time = std::chrono::duration<int64_t, std::nano>;
/**
* @brief Represents a datetime as the number of nanoseconds since the epoch.
*/
using DateTime = std::chrono::time_point<std::chrono::system_clock,
std::chrono::duration<int64_t, std::nano>>;
using DateTime = std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>;

/**
* @brief The same as size_t when it is 64 bits, otherwise uint64_t.
Expand Down
12 changes: 0 additions & 12 deletions cpp/evolution/v2/generated/yardl/detail/binary/serializers.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
#include "../../yardl.h"
#include "coded_stream.h"

#if __cplusplus >= 202002L
#include <bit>
#endif

namespace yardl::binary {

/**
Expand Down Expand Up @@ -177,11 +173,7 @@ inline void WriteDate(CodedOutputStream& stream, yardl::Date const& value) {
inline void ReadDate(CodedInputStream& stream, yardl::Date& value) {
int64_t days;
ReadInteger(stream, days);
#if __cplusplus < 202002L
value = yardl::Date(date::days(days));
#else
value = yardl::Date{std::chrono::days{days}};
#endif
}

inline void WriteTime(CodedOutputStream& stream, yardl::Time const& value) {
Expand All @@ -202,13 +194,9 @@ inline void WriteDateTime(CodedOutputStream& stream, yardl::DateTime const& valu
inline void ReadDateTime(CodedInputStream& stream, yardl::DateTime& value) {
int64_t ns;
ReadInteger(stream, ns);
#if __cplusplus < 202002L
value = yardl::DateTime(date::sys_time<std::chrono::nanoseconds>(std::chrono::nanoseconds(ns)));
#else
value = yardl::DateTime{
std::chrono::time_point<std::chrono::system_clock,
std::chrono::nanoseconds>(std::chrono::nanoseconds(ns))};
#endif
}

template <typename T, Writer<T> WriteElement>
Expand Down
6 changes: 0 additions & 6 deletions cpp/evolution/v2/generated/yardl/detail/hdf5/ddl.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,9 @@ static inline H5::PredType const& SizeTypeDdl() {
* of days since the epoch.
*/
static inline H5::DataType DateTypeDdl() {
#if __cplusplus < 202002L
static_assert(sizeof(yardl::Date) == sizeof(int32_t));
static_assert(std::is_same_v<yardl::Date::rep, int32_t>);
return H5::PredType::NATIVE_INT32;
#else
static_assert(sizeof(yardl::Date) == sizeof(int64_t));
static_assert(std::is_same_v<yardl::Date::rep, int64_t>);
return H5::PredType::NATIVE_INT64;
#endif
}

/**
Expand Down
13 changes: 0 additions & 13 deletions cpp/evolution/v2/generated/yardl/detail/ndjson/serializers.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,25 +125,12 @@ struct adl_serializer<std::monostate> {
template <>
struct adl_serializer<yardl::Date> {
static void to_json(ordered_json& j, yardl::Date const& value) {
#if __cplusplus < 202002L
j = date::format("%F", value);
#else
date::local_days ld(value.time_since_epoch());
j = date::format("%F", ld);
#endif
}

static void from_json(ordered_json const& j, yardl::Date& value) {
std::stringstream ss{j.get<std::string>()};

#if __cplusplus < 202002L
ss >> date::parse("%F", value);
#else
date::local_days ld;
ss >> date::parse("%F", ld);
value = yardl::Date(ld.time_since_epoch());
#endif

if (ss.fail()) {
throw std::runtime_error("invalid date format");
}
Expand Down
14 changes: 1 addition & 13 deletions cpp/evolution/v2/generated/yardl/yardl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@

#include <chrono>

#if __cplusplus < 202002L
// This functionality is part of the standard library as of C++20
#include <date/date.h>
#endif

#include <xtensor/xarray.hpp>
#include <xtensor/xfixed.hpp>
#include <xtensor/xtensor.hpp>
Expand Down Expand Up @@ -46,17 +42,10 @@ using NDArray = xt::xtensor<T, N, xt::layout_type::row_major>;
template <typename T>
using DynamicNDArray = xt::xarray<T, xt::layout_type::row_major>;

#if __cplusplus < 202002L
/**
* @brief Represents a date as a number of days since the epoch.
*/
using Date = date::local_days;
#else
/**
* @brief Represents a date as a number of days since the epoch.
*/
using Date = std::chrono::local_days;
#endif

/**
* @brief Represents a time of day as the number of nanoseconds since midnight.
Expand All @@ -66,8 +55,7 @@ using Time = std::chrono::duration<int64_t, std::nano>;
/**
* @brief Represents a datetime as the number of nanoseconds since the epoch.
*/
using DateTime = std::chrono::time_point<std::chrono::system_clock,
std::chrono::duration<int64_t, std::nano>>;
using DateTime = std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>;

/**
* @brief The same as size_t when it is 64 bits, otherwise uint64_t.
Expand Down
8 changes: 8 additions & 0 deletions cpp/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@ add_executable(tests
roundtrip_test.cc
)

# This additional test must be compiled with C++20 to test compatibility with
# the C++17 version of the generated library
add_library(abi_test OBJECT
abi_test.cc
)
set_property(TARGET abi_test PROPERTY CXX_STANDARD 20)

target_link_libraries(
tests
abi_test
fmt
gtest
gtest_main
Expand Down
Loading

0 comments on commit 863223d

Please sign in to comment.