@@ -14,18 +14,55 @@ std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> epo
14
14
15
15
16
16
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);
18
18
}
19
19
20
20
std::chrono::time_point<std::chrono::system_clock> time_from_string (const std::string& time) {
21
21
std::tm tm = {};
22
22
auto ss = std::stringstream (time);
23
23
ss >> std::get_time (&tm, " %Y-%m-%dT%H:%M:%S" );
24
24
25
+ #ifdef _WIN32
26
+ return std::chrono::system_clock::from_time_t (_mkgmtime (&tm));
27
+ #else
25
28
return std::chrono::system_clock::from_time_t (timegm (&tm));
29
+ #endif
26
30
}
27
31
28
32
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
29
66
{
30
67
auto time_str = " 1969-12-12T00:00:00.000000000Z" ;
31
68
auto time = time_from_string (time_str);
@@ -55,8 +92,8 @@ void test_string_timestamps() {
55
92
56
93
int main () {
57
94
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 ))
60
97
);
61
98
62
99
ASSERT_EQ (
@@ -69,14 +106,20 @@ int main() {
69
106
chronological::ChronologicalError
70
107
);
71
108
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));
72
113
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 )
75
116
);
76
117
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));
77
120
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 )
80
123
);
81
124
82
125
ASSERT_EQ (
0 commit comments