Skip to content

Commit f4fc89b

Browse files
committed
Specify duration type for std::chrono::time_point
To make sure that all of the platforms use the same representation, tests modified accordingly Signed-off-by: Martynas Gurskas <[email protected]>
1 parent 496ddf2 commit f4fc89b

File tree

5 files changed

+55
-11
lines changed

5 files changed

+55
-11
lines changed

bindgen/src/bindings/cpp/gen_cpp/miscellany.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ macro_rules! impl_code_type_for_miscellany {
2626

2727
impl_code_type_for_miscellany!(
2828
TimestampCodeType,
29-
"std::chrono::time_point<std::chrono::system_clock>",
29+
"std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>",
3030
"Timestamp"
3131
);
3232
impl_code_type_for_miscellany!(

cpp-tests/scaffolding_tests/chronological/lib_chronological.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ chronological::duration chronological::return_duration(chronological::duration a
1313
}
1414

1515
std::string chronological::to_string_timestamp(chronological::timestamp a) {
16-
std::time_t time = std::chrono::system_clock::to_time_t(a);
16+
using time_point = std::chrono::system_clock::time_point;
17+
std::time_t time = std::chrono::system_clock::to_time_t(time_point {std::chrono::duration_cast<time_point::duration>(a.time_since_epoch())});
1718
auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(a.time_since_epoch()).count() % 1000000000;
1819
if (ns < 0) {
1920
ns += 1000000000;

cpp-tests/scaffolding_tests/chronological/lib_chronological.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace {
77
namespace chronological {
8-
typedef std::chrono::time_point<std::chrono::system_clock> timestamp;
8+
typedef std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> timestamp;
99
typedef std::chrono::duration<int64_t, std::nano> duration;
1010

1111
namespace chronological_error {

cpp-tests/scaffolding_tests/coverall/lib_coverall.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace {
1414
namespace coverall {
15-
typedef std::chrono::time_point<std::chrono::system_clock> timestamp;
15+
typedef std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> timestamp;
1616

1717
class IFirst {
1818
public:

cpp-tests/tests/chronological/main.cpp

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,55 @@ std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> epo
1414

1515

1616
std::chrono::duration<int64_t, std::nano> time_span_seconds(int seconds, int nanoseconds) {
17-
return std::chrono::nanoseconds(std::chrono::seconds(seconds) + std::chrono::nanoseconds(nanoseconds));
17+
return std::chrono::seconds(seconds) + std::chrono::nanoseconds(nanoseconds);
1818
}
1919

2020
std::chrono::time_point<std::chrono::system_clock> time_from_string(const std::string& time) {
2121
std::tm tm = {};
2222
auto ss = std::stringstream(time);
2323
ss >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%S");
2424

25+
#ifdef _WIN32
26+
return std::chrono::system_clock::from_time_t(_mkgmtime(&tm));
27+
#else
2528
return std::chrono::system_clock::from_time_t(timegm(&tm));
29+
#endif
2630
}
2731

2832
void test_string_timestamps() {
33+
// Test post-epoch timestamps
34+
{
35+
auto time_str = "1970-12-12T00:00:00.000000000Z";
36+
auto time = time_from_string(time_str);
37+
38+
ASSERT_EQ(time_str, chronological::to_string_timestamp(time));
39+
}
40+
41+
{
42+
// get_time doesn't support nanoseconds, so we have to add them manually
43+
auto time_str = "1970-12-31T23:59:58.999999900Z";
44+
auto time = time_from_string(time_str) + 999999900ns;
45+
46+
ASSERT_EQ(time_str, chronological::to_string_timestamp(time));
47+
}
48+
49+
{
50+
// get_time doesn't support nanoseconds, so we have to add them manually
51+
auto time = time_from_string("2024-11-05T00:06:01.283000200Z") + 283000200ns;
52+
auto time2 = time_from_string("2024-11-05T00:06:00.283000100Z") + 283000100ns;
53+
54+
ASSERT_EQ(
55+
time,
56+
chronological::add(time2, time_span_seconds(1, 100))
57+
);
58+
}
59+
60+
#ifdef _WIN32
61+
// _mkgmtime doesn't support dates before 1970, so we skip these tests
62+
return;
63+
#endif
64+
65+
// Test pre-epoch timestamps
2966
{
3067
auto time_str = "1969-12-12T00:00:00.000000000Z";
3168
auto time = time_from_string(time_str);
@@ -55,8 +92,8 @@ void test_string_timestamps() {
5592

5693
int main() {
5794
ASSERT_EQ(
58-
epoch_second(101, 110),
59-
chronological::add(epoch_second(100, 100), time_span_seconds(1, 10))
95+
epoch_second(101, 200),
96+
chronological::add(epoch_second(100, 100), time_span_seconds(1, 100))
6097
);
6198

6299
ASSERT_EQ(
@@ -69,14 +106,20 @@ int main() {
69106
chronological::ChronologicalError
70107
);
71108

109+
110+
// When testing the min and max values, we need to round them to the nearest 100 ns, as on Windows one tick is 100 ns
111+
auto min = std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>::min().time_since_epoch().count() / 100 * 100;
112+
auto min_timepoint = std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>(std::chrono::nanoseconds(min));
72113
ASSERT_EQ(
73-
std::chrono::time_point<std::chrono::system_clock>::min(),
74-
chronological::return_timestamp(std::chrono::time_point<std::chrono::system_clock>::min())
114+
min_timepoint,
115+
chronological::return_timestamp(min_timepoint)
75116
);
76117

118+
auto max = std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>::max().time_since_epoch().count() / 100 * 100;
119+
auto max_timepoint = std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>(std::chrono::nanoseconds(max));
77120
ASSERT_EQ(
78-
std::chrono::time_point<std::chrono::system_clock>::max(),
79-
chronological::return_timestamp(std::chrono::time_point<std::chrono::system_clock>::max())
121+
max_timepoint,
122+
chronological::return_timestamp(max_timepoint)
80123
);
81124

82125
ASSERT_EQ(

0 commit comments

Comments
 (0)