1//
2// Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
3// Copyright (c) 2022-2023 Alexander Grund
4//
5// Distributed under the Boost Software License, Version 1.0.
6// https://www.boost.org/LICENSE_1_0.txt
7
8#include <boost/locale/date_time.hpp>
9#include <boost/locale/formatting.hpp>
10#include <boost/locale/generator.hpp>
11#include <boost/locale/localization_backend.hpp>
12#include "boostLocale/test/tools.hpp"
13#include "boostLocale/test/unit_test.hpp"
14#include <cmath>
15#include <ctime>
16#include <iomanip>
17#include <limits>
18#include <sstream>
19
20#ifdef BOOST_LOCALE_WITH_ICU
21# include <unicode/uversion.h>
22# define BOOST_LOCALE_ICU_VERSION (U_ICU_VERSION_MAJOR_NUM * 100 + U_ICU_VERSION_MINOR_NUM)
23#else
24# define BOOST_LOCALE_ICU_VERSION 0
25#endif
26
27#ifdef BOOST_MSVC
28# pragma warning(disable : 4244) // loose data
29#endif
30
31#define TEST_EQ_FMT(t, X) \
32 empty_stream(ss) << (t); \
33 test_eq_impl(ss.str(), X, #t "==" #X, __LINE__)
34
35// Very simple container for a part of the tests. Counts its instances
36struct mock_calendar : public boost::locale::abstract_calendar {
37 using period_mark = boost::locale::period::marks::period_mark;
38
39 mock_calendar() : time(0) { ++num_instances; }
40 mock_calendar(const mock_calendar& other) : time(other.time), tz_(other.tz_), is_dst_(other.is_dst_)
41 {
42 ++num_instances;
43 }
44 ~mock_calendar() { --num_instances; }
45
46 abstract_calendar* clone() const override { return new mock_calendar(*this); }
47 void set_value(period_mark, int) override {} // LCOV_EXCL_LINE
48 void normalize() override {} // LCOV_EXCL_LINE
49 int get_value(period_mark, value_type) const override { return 0; } // LCOV_EXCL_LINE
50 void set_time(const boost::locale::posix_time& t) override { time = t.seconds * 1e3 + t.nanoseconds / 1e6; }
51 boost::locale::posix_time get_time() const override
52 {
53 const auto seconds = static_cast<int64_t>(time / 1e3);
54 return {.seconds: seconds, .nanoseconds: static_cast<uint32_t>((time - seconds * 1e3) * 1e6)};
55 }
56 double get_time_ms() const override { return time; }
57 void set_option(calendar_option_type, int) override {} // LCOV_EXCL_LINE
58 int get_option(calendar_option_type opt) const override { return opt == is_dst ? is_dst_ : false; }
59 void adjust_value(period_mark, update_type, int) override {} // LCOV_EXCL_LINE
60 int difference(const abstract_calendar&, period_mark) const override { return 0; } // LCOV_EXCL_LINE
61 void set_timezone(const std::string& tz) override { tz_ = tz; }
62 std::string get_timezone() const override { return tz_; }
63 bool same(const abstract_calendar* other) const override
64 {
65 return dynamic_cast<const mock_calendar*>(other) != nullptr;
66 }
67
68 static int num_instances;
69 /// Time in ms
70 double time;
71 std::string tz_ = "mock TZ";
72 bool is_dst_ = false;
73};
74int mock_calendar::num_instances = 0;
75struct mock_calendar_facet : boost::locale::calendar_facet {
76 boost::locale::abstract_calendar* create_calendar() const override { return proto_cal.clone(); }
77 mock_calendar proto_cal;
78};
79
80struct scoped_timezone {
81 std::string old_tz_;
82 explicit scoped_timezone(const std::string& tz) : old_tz_(boost::locale::time_zone::global(new_tz: tz)) {}
83 ~scoped_timezone() { boost::locale::time_zone::global(new_tz: old_tz_); }
84};
85
86static bool equal_period(const boost::locale::date_time_period& lhs, const boost::locale::date_time_period& rhs)
87{
88 return lhs.type == rhs.type && lhs.value == rhs.value;
89}
90
91void test_main(int /*argc*/, char** /*argv*/)
92{
93 using namespace boost::locale;
94 using namespace boost::locale::period;
95 {
96 date_time_period_set set;
97 TEST_EQ(set.size(), 0u);
98 TEST_THROWS(set[0], std::out_of_range);
99
100 set = day();
101 TEST_EQ(set.size(), 1u);
102 TEST(equal_period(set[0], day(1)));
103 TEST_THROWS(set[1], std::out_of_range);
104 set = day(v: 1);
105 TEST_EQ(set.size(), 1u);
106 TEST(equal_period(set[0], day(1)));
107 set = day(v: 2);
108 TEST_EQ(set.size(), 1u);
109 TEST(equal_period(set[0], day(2)));
110
111 set = day(v: 7) + month(v: 3);
112 TEST_EQ(set.size(), 2u);
113 TEST(equal_period(set[0], day(7)));
114 TEST(equal_period(set[1], month(3)));
115 TEST_THROWS(set[2], std::out_of_range);
116
117 set = year(v: 3) + month(v: 5) + day(v: 7) + hour(v: 13) + minute(v: 17);
118 TEST_EQ(set.size(), 5u);
119 TEST(equal_period(set[0], year(3)));
120 TEST(equal_period(set[1], month(5)));
121 TEST(equal_period(set[2], day(7)));
122 TEST(equal_period(set[3], hour(13)));
123 TEST(equal_period(set[4], minute(17)));
124 TEST_THROWS(set[5], std::out_of_range);
125 }
126 std::unique_ptr<calendar> mock_cal;
127 {
128 auto* cal_facet = new mock_calendar_facet;
129 std::locale old_loc = std::locale::global(loc: std::locale(std::locale(), cal_facet));
130 mock_calendar::num_instances = 0;
131 {
132 scoped_timezone _("global TZ");
133 cal_facet->proto_cal.time = 42 * 1e3;
134 cal_facet->proto_cal.is_dst_ = false;
135 date_time t1;
136 TEST_EQ(t1.time(), 42);
137 TEST_EQ(t1.timezone(), "global TZ");
138 TEST(!t1.is_in_daylight_saving_time());
139 TEST_EQ(mock_calendar::num_instances, 1);
140 cal_facet->proto_cal.time = 99 * 1e3;
141 cal_facet->proto_cal.is_dst_ = true;
142 boost::locale::time_zone::global(new_tz: "global TZ2");
143 date_time t2;
144 boost::locale::time_zone::global(new_tz: "global TZ3");
145 TEST_EQ(t2.time(), 99);
146 TEST_EQ(t2.timezone(), "global TZ2");
147 TEST(t2.is_in_daylight_saving_time());
148 TEST_EQ(mock_calendar::num_instances, 2);
149 // Copy construct
150 date_time t3 = t1;
151 TEST_EQ(t1.time(), 42);
152 TEST_EQ(t2.time(), 99);
153 TEST_EQ(t3.time(), 42);
154 TEST_EQ(t3.timezone(), "global TZ");
155 TEST_EQ(mock_calendar::num_instances, 3);
156 // Copy assign
157 t3 = t2;
158 TEST_EQ(t3.time(), 99);
159 TEST_EQ(t3.timezone(), "global TZ2");
160 TEST_EQ(mock_calendar::num_instances, 3); // No new
161 {
162 // Move construct
163 date_time t4 = std::move(t1);
164 TEST_EQ(t4.time(), 42);
165 TEST_EQ(t4.timezone(), "global TZ");
166 TEST_EQ(mock_calendar::num_instances, 3); // No new
167 // Move assign
168 t2 = std::move(t4);
169 TEST_EQ(t2.time(), 42);
170 TEST_EQ(t2.timezone(), "global TZ");
171 TEST_LE(mock_calendar::num_instances, 3); // maybe destroy old t2
172 }
173 // Unchanged after t4 (or old t2) is destroyed
174 TEST_EQ(t2.time(), 42);
175 TEST_EQ(t2.timezone(), "global TZ");
176 TEST_EQ(mock_calendar::num_instances, 2);
177 // Self move, via reference to avoid triggering a compiler warning
178 date_time& t2_ref = t2;
179 t2_ref = std::move(t2);
180 TEST_EQ(t2.time(), 42);
181 TEST_EQ(mock_calendar::num_instances, 2);
182
183 // Construct from calendar
184 {
185 const double t = 1337;
186 cal_facet->proto_cal.time = t * 1e3;
187
188 const calendar cal;
189 TEST_EQ(date_time(cal).time(), t);
190 TEST_EQ(date_time(42, cal).time(), 42);
191 }
192 // Constructor from calendar uses calendars TZ
193 {
194 const std::string globalTZ = boost::locale::time_zone::global();
195 TEST_EQ(date_time().timezone(), globalTZ);
196 TEST_EQ(date_time(101).timezone(), globalTZ);
197 TEST_EQ(date_time(year(2001)).timezone(), globalTZ);
198 const std::string calTZ = "calTZ";
199 const calendar cal(calTZ);
200 TEST_EQ(date_time(cal).timezone(), calTZ);
201 TEST_EQ(date_time(101, cal).timezone(), calTZ);
202 TEST_EQ(date_time(year(2001), cal).timezone(), calTZ);
203 }
204
205 // Swap
206 t1 = date_time(99);
207 t2 = date_time(42);
208 TEST_EQ(t1.time(), 99);
209 TEST_EQ(t2.time(), 42);
210 using std::swap;
211 swap(left&: t1, right&: t2);
212 TEST_EQ(t1.time(), 42);
213 TEST_EQ(t2.time(), 99);
214 swap(left&: t1, right&: t1);
215 TEST_EQ(t1.time(), 42);
216 swap(left&: t2, right&: t2);
217 TEST_EQ(t2.time(), 99);
218
219 // Negative times
220 t1 = date_time(-1.25);
221 TEST_EQ(t1.time(), -1.25);
222 t1 = date_time(-0.25);
223 TEST_EQ(t1.time(), -0.25);
224
225 // Comparison in subsecond differences (only if backend supports it)
226 t1.time(v: 42.5);
227 t2.time(v: 42.25);
228 TEST(t1 >= t2);
229 TEST(t1 > t2);
230 t1.time(v: t2.time() - 0.1);
231 TEST(t1 <= t2);
232 TEST(t1 < t2);
233 t1.time(v: t2.time());
234 TEST(t1 <= t2);
235 TEST(t1 >= t2);
236 TEST(t1 == t2);
237 }
238 TEST_EQ(mock_calendar::num_instances, 0); // No leaks
239 mock_cal.reset(p: new calendar());
240 std::locale::global(loc: old_loc);
241 }
242 for(const std::string& backend_name : boost::locale::localization_backend_manager::global().get_all_backends()) {
243 std::cout << "Testing for backend: " << backend_name << std::endl;
244 boost::locale::localization_backend_manager tmp_backend = boost::locale::localization_backend_manager::global();
245 tmp_backend.select(backend_name);
246 boost::locale::localization_backend_manager::global(tmp_backend);
247
248 boost::locale::generator g;
249 std::locale loc = g("en_US.UTF-8");
250 {
251 using boost::locale::abstract_calendar;
252 std::unique_ptr<abstract_calendar> cal(
253 std::use_facet<boost::locale::calendar_facet>(loc: loc).create_calendar());
254 TEST_THROWS(cal->set_option(abstract_calendar::is_gregorian, 0), boost::locale::date_time_error);
255 TEST_THROWS(cal->set_option(abstract_calendar::is_dst, 0), boost::locale::date_time_error);
256 }
257
258 {
259 std::locale::global(loc: loc);
260
261 const std::string tz = "GMT";
262 time_zone::global(new_tz: tz);
263 // A call returns the old tz
264 TEST_EQ(time_zone::global("GMT+01:00"), tz);
265 TEST_EQ(time_zone::global(tz), "GMT+01:00");
266 calendar cal(loc, tz);
267 TEST(cal.get_locale() == loc);
268 TEST_EQ(cal.get_time_zone(), tz);
269
270 TEST(calendar() == cal);
271 TEST(calendar(loc) == cal);
272 TEST(calendar(tz) == cal);
273 {
274 const std::string tz2 = "GMT+01:00";
275 const std::locale loc2 = g("ru_RU.UTF-8");
276 const calendar cal_tz2(loc, "GMT+01:00");
277 const calendar cal_loc2(loc2);
278 TEST(cal_tz2 != cal);
279 TEST(cal_loc2 != cal);
280 calendar cal_tmp(cal);
281 TEST(cal_tmp == cal);
282 TEST(cal_tmp != cal_tz2);
283 cal_tmp = cal_tz2;
284 TEST(cal_tmp == cal_tz2);
285 TEST_EQ(cal_tmp.get_time_zone(), tz2);
286 TEST(cal_tmp.get_locale() == loc);
287 TEST(cal_tmp != cal_loc2);
288 cal_tmp = cal_loc2;
289 TEST(cal_tmp == cal_loc2);
290 TEST_EQ(cal_tmp.get_time_zone(), tz);
291 TEST(cal_tmp.get_locale() == loc2);
292
293 // Stream constructors
294 std::ostringstream ss;
295 calendar cal_s(ss);
296 TEST(cal_s == cal);
297 TEST(cal_s != cal_loc2);
298 TEST(cal_s != cal_tz2);
299 ss.imbue(loc: loc2);
300 cal_s = calendar(ss);
301 TEST(cal_s != cal);
302 TEST(cal_s == cal_loc2);
303 TEST(cal_s != cal_tz2);
304 ss.imbue(loc: loc);
305 ss << boost::locale::as::time_zone(id: "GMT+01:00");
306 cal_s = calendar(ss);
307 TEST(cal_s != cal);
308 TEST(cal_s != cal_loc2);
309 TEST(cal_s == cal_tz2);
310 }
311 {
312 calendar cal2;
313 TEST(cal2 != *mock_cal);
314 cal2 = *mock_cal;
315 TEST(cal2 == *mock_cal);
316 }
317
318 TEST_EQ(cal.minimum(month()), 0);
319 TEST_EQ(cal.maximum(month()), 11);
320 TEST_EQ(cal.minimum(day()), 1);
321 TEST_EQ(cal.greatest_minimum(day()), 1);
322 TEST_EQ(cal.least_maximum(day()), 28);
323 TEST_EQ(cal.maximum(day()), 31);
324 TEST(cal.is_gregorian());
325
326 TEST_EQ(calendar(g("ar_EG.UTF-8")).first_day_of_week(), 7);
327 TEST_EQ(calendar(g("he_IL.UTF-8")).first_day_of_week(), 1);
328 TEST_EQ(calendar(g("ru_RU.UTF-8")).first_day_of_week(), 2);
329
330 std::ostringstream ss;
331 ss.imbue(loc: loc);
332 ss << boost::locale::as::time_zone(id: tz);
333
334 const time_t one_h = 60 * 60;
335 const time_t a_date = 24 * one_h * (31 + 4); // Feb 5th
336 const time_t a_time = 15 * one_h + 60 * 33 + 13; // 15:33:13
337 const time_t a_datetime = a_date + a_time;
338
339 const date_time tp_5_feb_1970_153313 = date_time(a_datetime); // 5th Feb 1970 15:33:13
340 TEST_EQ(tp_5_feb_1970_153313.timezone(), tz);
341
342 // Auto-switch the stream when streaming a date-time
343 {
344 empty_stream(s&: ss) << as::datetime << tp_5_feb_1970_153313;
345 const std::string expected = ss.str();
346 empty_stream(s&: ss) << as::posix;
347 TEST_EQ_FMT(tp_5_feb_1970_153313, expected);
348 // And reset to previous
349 TEST_EQ_FMT(123456789, "123456789");
350 // Same with other preset
351 empty_stream(s&: ss) << as::number << 123456789;
352 const std::string expected2 = ss.str();
353 TEST_EQ_FMT(tp_5_feb_1970_153313, expected);
354 // And reset to previous
355 TEST_EQ_FMT(123456789, expected2);
356 }
357
358 ss << as::ftime(format: "%Y-%m-%d");
359 TEST_EQ_FMT(tp_5_feb_1970_153313, "1970-02-05");
360 ss << as::ftime(format: "%Y-%m-%d %H:%M:%S");
361 TEST_EQ_FMT(tp_5_feb_1970_153313, "1970-02-05 15:33:13");
362
363 // Test set()
364 date_time time_point = tp_5_feb_1970_153313;
365 time_point.set(f: year(), v: 1990);
366 TEST_EQ_FMT(time_point, "1990-02-05 15:33:13");
367 time_point.set(f: month(), v: 5);
368 TEST_EQ_FMT(time_point, "1990-06-05 15:33:13");
369 time_point.set(f: day(), v: 9);
370 TEST_EQ_FMT(time_point, "1990-06-09 15:33:13");
371 time_point.set(f: hour(), v: 11);
372 TEST_EQ_FMT(time_point, "1990-06-09 11:33:13");
373 time_point.set(f: minute(), v: 42);
374 TEST_EQ_FMT(time_point, "1990-06-09 11:42:13");
375 time_point.set(f: second(), v: 24);
376 TEST_EQ_FMT(time_point, "1990-06-09 11:42:24");
377 time_point.set(f: am_pm(), v: 1);
378 TEST_EQ_FMT(time_point, "1990-06-09 23:42:24");
379 // Overflow day of month
380 time_point.set(f: day(), v: time_point.maximum(f: day()) + 1);
381 TEST_EQ_FMT(time_point, "1990-07-01 23:42:24");
382
383 // Same via assignment
384 time_point = tp_5_feb_1970_153313;
385 time_point = year(v: 1990);
386 TEST_EQ_FMT(time_point, "1990-02-05 15:33:13");
387 time_point = month(v: 5);
388 TEST_EQ_FMT(time_point, "1990-06-05 15:33:13");
389 time_point = day(v: 9);
390 TEST_EQ_FMT(time_point, "1990-06-09 15:33:13");
391 time_point = hour(v: 11);
392 TEST_EQ_FMT(time_point, "1990-06-09 11:33:13");
393 time_point = minute(v: 42);
394 TEST_EQ_FMT(time_point, "1990-06-09 11:42:13");
395 time_point = second(v: 24);
396 TEST_EQ_FMT(time_point, "1990-06-09 11:42:24");
397 time_point = am_pm(v: 1);
398 TEST_EQ_FMT(time_point, "1990-06-09 23:42:24");
399 // Overflow day of month
400 time_point = day(v: time_point.maximum(f: day()) + 1);
401 TEST_EQ_FMT(time_point, "1990-07-01 23:42:24");
402 // All at once
403 time_point = year(v: 1989) + month(v: 2) + day(v: 5) + hour(v: 7) + minute(v: 9) + second(v: 11);
404 TEST_EQ_FMT(time_point, "1989-03-05 07:09:11");
405 {
406 // Construction and setting of a timepoint to a fully specified value must be equal
407 // See issue #221
408 date_time explicit_time_point = year(v: 1989) + month(v: 2) + day(v: 5) + hour(v: 7) + minute(v: 9) + second(v: 11);
409 TEST(time_point == explicit_time_point);
410 TEST_EQ(time_point.time(), explicit_time_point.time());
411 }
412 // Partials:
413 time_point = year(v: 1970) + february() + day(v: 5);
414 TEST_EQ_FMT(time_point, "1970-02-05 07:09:11");
415 time_point = 3 * hour_12() + 1 * am_pm() + 33 * minute() + 13 * second();
416 TEST_EQ_FMT(time_point, "1970-02-05 15:33:13");
417
418 time_point = tp_5_feb_1970_153313;
419 time_point += hour();
420 TEST_EQ_FMT(time_point, "1970-02-05 16:33:13");
421
422 TEST_EQ(time_point.minimum(day()), 1);
423 TEST_EQ(time_point.maximum(day()), 28);
424
425 time_point = tp_5_feb_1970_153313;
426 time_point += year() * 2 + 1 * month();
427 TEST_EQ_FMT(time_point, "1972-03-05 15:33:13");
428
429 time_point = tp_5_feb_1970_153313;
430 time_point -= minute();
431 TEST_EQ_FMT(time_point, "1970-02-05 15:32:13");
432
433 time_point = tp_5_feb_1970_153313;
434 time_point <<= minute() * 30;
435 TEST_EQ_FMT(time_point, "1970-02-05 15:03:13");
436 // Same as repeated roll
437 time_point = tp_5_feb_1970_153313;
438 for(int i = 0; i < 30; i++)
439 time_point <<= minute();
440 TEST_EQ_FMT(time_point, "1970-02-05 15:03:13");
441
442 time_point = tp_5_feb_1970_153313;
443 time_point >>= minute(v: 40);
444 TEST_EQ_FMT(time_point, "1970-02-05 15:53:13");
445 // Same as repeated roll
446 time_point = tp_5_feb_1970_153313;
447 for(int i = 0; i < 40; i++)
448 time_point >>= minute();
449 TEST_EQ_FMT(time_point, "1970-02-05 15:53:13");
450
451 time_point = tp_5_feb_1970_153313;
452 TEST_EQ((time_point + month()) / month(), 2);
453 TEST_EQ(month(time_point + month(1)), 2);
454 TEST_EQ(time_point / month(), 1);
455 TEST_EQ((time_point - month()) / month(), 0);
456 TEST_EQ(time_point / month(), 1);
457 TEST_EQ((time_point << month()) / month(), 2);
458 TEST_EQ(time_point / month(), 1);
459 TEST_EQ((time_point >> month()) / month(), 0);
460 TEST_EQ(time_point / month(), 1);
461
462 // To subtract from the year, don't use 1970 which may be the lowest possible year
463 const date_time tp_5_april_1990_153313 = (date_time(tp_5_feb_1970_153313) = (year(v: 1990) + april()));
464 TEST_EQ_FMT(tp_5_april_1990_153313, "1990-04-05 15:33:13");
465 // Test each period
466 TEST_EQ_FMT(tp_5_april_1990_153313 + year(2), "1992-04-05 15:33:13");
467 TEST_EQ_FMT(tp_5_april_1990_153313 << year(2), "1992-04-05 15:33:13");
468 TEST_EQ_FMT(tp_5_april_1990_153313 - year(10), "1980-04-05 15:33:13");
469 TEST_EQ_FMT(tp_5_april_1990_153313 >> year(10), "1980-04-05 15:33:13");
470 TEST_EQ_FMT(tp_5_april_1990_153313 + month(2), "1990-06-05 15:33:13");
471 TEST_EQ_FMT(tp_5_april_1990_153313 << month(2), "1990-06-05 15:33:13");
472 TEST_EQ_FMT(tp_5_april_1990_153313 - month(1), "1990-03-05 15:33:13");
473 TEST_EQ_FMT(tp_5_april_1990_153313 >> month(1), "1990-03-05 15:33:13");
474 TEST_EQ_FMT(tp_5_april_1990_153313 + day(2), "1990-04-07 15:33:13");
475 TEST_EQ_FMT(tp_5_april_1990_153313 << day(2), "1990-04-07 15:33:13");
476 TEST_EQ_FMT(tp_5_april_1990_153313 - day(3), "1990-04-02 15:33:13");
477 TEST_EQ_FMT(tp_5_april_1990_153313 >> day(3), "1990-04-02 15:33:13");
478 TEST_EQ_FMT(tp_5_april_1990_153313 + hour(2), "1990-04-05 17:33:13");
479 TEST_EQ_FMT(tp_5_april_1990_153313 << hour(2), "1990-04-05 17:33:13");
480 TEST_EQ_FMT(tp_5_april_1990_153313 - hour(3), "1990-04-05 12:33:13");
481 TEST_EQ_FMT(tp_5_april_1990_153313 >> hour(3), "1990-04-05 12:33:13");
482 TEST_EQ_FMT(tp_5_april_1990_153313 + minute(2), "1990-04-05 15:35:13");
483 TEST_EQ_FMT(tp_5_april_1990_153313 << minute(2), "1990-04-05 15:35:13");
484 TEST_EQ_FMT(tp_5_april_1990_153313 - minute(3), "1990-04-05 15:30:13");
485 TEST_EQ_FMT(tp_5_april_1990_153313 >> minute(3), "1990-04-05 15:30:13");
486 TEST_EQ_FMT(tp_5_april_1990_153313 + second(2), "1990-04-05 15:33:15");
487 TEST_EQ_FMT(tp_5_april_1990_153313 << second(2), "1990-04-05 15:33:15");
488 TEST_EQ_FMT(tp_5_april_1990_153313 - second(2), "1990-04-05 15:33:11");
489 TEST_EQ_FMT(tp_5_april_1990_153313 >> second(2), "1990-04-05 15:33:11");
490 // Difference between add and roll: The latter only changes the given field
491 // So this tests what happens when going over/under the bound for each field
492 TEST_EQ_FMT(tp_5_april_1990_153313 + month(12 + 2), "1991-06-05 15:33:13");
493 TEST_EQ_FMT(tp_5_april_1990_153313 << month(12 + 2), "1990-06-05 15:33:13");
494 TEST_EQ_FMT(tp_5_april_1990_153313 - month(12 * 3 + 1), "1987-03-05 15:33:13");
495 TEST_EQ_FMT(tp_5_april_1990_153313 >> month(12 * 3 + 1), "1990-03-05 15:33:13");
496 // Check that possible int overflows get handled
497 constexpr int max_full_years_in_months = (std::numeric_limits<int>::max() / 12) * 12;
498 TEST_EQ_FMT(tp_5_april_1990_153313 >> month(max_full_years_in_months), "1990-04-05 15:33:13");
499 TEST_EQ_FMT(tp_5_april_1990_153313 << month(max_full_years_in_months), "1990-04-05 15:33:13");
500 TEST_EQ_FMT(tp_5_april_1990_153313 + day(30 + 2), "1990-05-07 15:33:13");
501 TEST_EQ_FMT(tp_5_april_1990_153313 << day(30 + 2), "1990-04-07 15:33:13");
502 TEST_EQ_FMT(tp_5_april_1990_153313 - day(10), "1990-03-26 15:33:13");
503 TEST_EQ_FMT(tp_5_april_1990_153313 >> day(10), "1990-04-25 15:33:13");
504 TEST_EQ_FMT(tp_5_april_1990_153313 + hour(24 * 3 + 2), "1990-04-08 17:33:13");
505 TEST_EQ_FMT(tp_5_april_1990_153313 << hour(24 * 3 + 2), "1990-04-05 17:33:13");
506 TEST_EQ_FMT(tp_5_april_1990_153313 - hour(24 * 5 + 3), "1990-03-31 12:33:13");
507 TEST_EQ_FMT(tp_5_april_1990_153313 >> hour(24 * 5 + 3), "1990-04-05 12:33:13");
508 TEST_EQ_FMT(tp_5_april_1990_153313 + minute(60 * 5 + 3), "1990-04-05 20:36:13");
509 TEST_EQ_FMT(tp_5_april_1990_153313 << minute(60 * 5 + 3), "1990-04-05 15:36:13");
510 TEST_EQ_FMT(tp_5_april_1990_153313 - minute(60 * 5 + 3), "1990-04-05 10:30:13");
511 TEST_EQ_FMT(tp_5_april_1990_153313 >> minute(60 * 5 + 3), "1990-04-05 15:30:13");
512 TEST_EQ_FMT(tp_5_april_1990_153313 + second(60 * 3 + 2), "1990-04-05 15:36:15");
513 TEST_EQ_FMT(tp_5_april_1990_153313 << second(60 * 3 + 2), "1990-04-05 15:33:15");
514 TEST_EQ_FMT(tp_5_april_1990_153313 - second(60 * 5 + 2), "1990-04-05 15:28:11");
515 TEST_EQ_FMT(tp_5_april_1990_153313 >> second(60 * 5 + 2), "1990-04-05 15:33:11");
516
517 // Add a set of periods
518 TEST_EQ_FMT(tp_5_feb_1970_153313 << (year(2) + month(3) - day(1) + hour(5) + minute(7) + second(9)),
519 "1972-05-04 20:40:22");
520 TEST_EQ_FMT(tp_5_feb_1970_153313 + (year(2) + month(3) - day(1) + hour(5) + minute(7) + second(9)),
521 "1972-05-04 20:40:22");
522 // std calendar can't go below 1970
523 time_point = tp_5_feb_1970_153313;
524 time_point = year(v: 1972) + july();
525 TEST_EQ_FMT(time_point, "1972-07-05 15:33:13");
526 TEST_EQ_FMT(time_point >> (year(2) + month(3) - day(11) + hour(5) + minute(7) + second(9)),
527 "1970-04-16 10:26:04");
528 TEST_EQ_FMT(time_point - (year(2) + month(3) - day(11) + hour(5) + minute(7) + second(9)),
529 "1970-04-16 10:26:04");
530
531 time_point = tp_5_feb_1970_153313;
532 TEST(time_point == tp_5_feb_1970_153313);
533 TEST(!(time_point != tp_5_feb_1970_153313));
534 TEST_EQ(time_point.get(hour()), 15);
535 TEST_EQ(time_point / hour(), 15);
536 TEST(time_point + year() != time_point);
537 TEST(time_point - minute() <= time_point);
538 TEST(time_point <= time_point);
539 TEST(time_point + minute() >= time_point);
540 TEST(time_point >= time_point);
541
542 TEST(time_point < time_point + second());
543 TEST(!(time_point < time_point - second()));
544 TEST(time_point > time_point - second());
545 TEST(!(time_point > time_point + second()));
546 // Difference in ns
547 {
548 const double sec = std::trunc(x: time_point.time()) + 0.5; // Stay inside current second
549 TEST(date_time(sec - 0.25) <= date_time(sec));
550 TEST(date_time(sec + 0.25) >= date_time(sec));
551 // See issue #221: Supporting subseconds is confusing,
552 // especially as it is/was only support by ICU
553 {
554 date_time var0, var1;
555 const auto floorTime = std::floor(x: sec);
556 var0.time(v: floorTime + 0.9);
557 var1.time(v: floorTime + 0.2);
558 TEST_EQ((var0 - var1) / second(), 0);
559 // Adding a seconds should lead to a second difference
560 var1 += second(v: 1);
561 TEST_EQ((var1 - var0) / second(), 1);
562 }
563 }
564
565 TEST_EQ(time_point.get(day()), 5);
566 TEST_EQ(time_point.get(year()), 1970);
567
568 TEST_EQ(time_point.get(era()), 1);
569 TEST_EQ(time_point.get(year()), 1970);
570 TEST_EQ(time_point.get(extended_year()), 1970);
571 if(backend_name == "icu") {
572 time_point = extended_year(v: -3);
573 TEST_EQ(time_point.get(era()), 0);
574 TEST_EQ(time_point.get(year()), 4);
575 }
576
577 time_point = tp_5_feb_1970_153313;
578 TEST_EQ(time_point.get(month()), 1);
579 TEST_EQ(time_point.get(day()), 5);
580 TEST_EQ(time_point.get(day_of_year()), 36);
581 TEST_EQ(time_point.get(day_of_week()), 5);
582 TEST_EQ(time_point.get(day_of_week_in_month()), 1);
583 time_point = date_time(a_datetime, calendar(g("ru_RU.UTF-8")));
584 TEST_EQ(time_point.get(day_of_week_local()), 4);
585 time_point = year(v: 2026) + january() + day(v: 1);
586 TEST_EQ(time_point.get(day_of_week()), 5);
587 TEST_EQ(time_point.get(week_of_year()), 1);
588 TEST_EQ(time_point.get(week_of_month()), 1);
589 time_point = day_of_week() * 1;
590 TEST_EQ(time_point.get(day()), 4);
591 TEST_EQ(time_point.get(week_of_year()), 1);
592 TEST_EQ(time_point.get(week_of_month()), 1);
593 time_point += day() * 1;
594 TEST_EQ(time_point.get(week_of_year()), 2);
595 TEST_EQ(time_point.get(week_of_month()), 2);
596
597 time_point = february() + day() * 2;
598
599 TEST_EQ(time_point.get(week_of_year()), 6);
600
601 // cldr changes
602#if BOOST_LOCALE_ICU_VERSION >= 408 && BOOST_LOCALE_ICU_VERSION <= 6000
603 const bool ICU_cldr_issue = backend_name == "icu";
604#else
605 const bool ICU_cldr_issue = false;
606#endif
607 BOOST_LOCALE_START_CONST_CONDITION
608
609 TEST_EQ(time_point.get(week_of_month()), ICU_cldr_issue ? 2 : 1);
610
611 time_point = year(v: 2010) + january() + day() * 3;
612
613 TEST_EQ(time_point.get(week_of_year()), ICU_cldr_issue ? 1 : 53);
614
615 time_point = year() * 2010 + january() + day() * 4;
616
617 TEST_EQ(time_point.get(week_of_year()), ICU_cldr_issue ? 2 : 1);
618
619 time_point = year() * 2010 + january() + day() * 10;
620
621 TEST_EQ(time_point.get(week_of_year()), ICU_cldr_issue ? 2 : 1);
622
623 time_point = year() * 2010 + january() + day() * 11;
624
625 TEST_EQ(time_point.get(week_of_year()), ICU_cldr_issue ? 3 : 2);
626
627 BOOST_LOCALE_END_CONST_CONDITION
628
629 time_point = date_time(a_datetime);
630 TEST_EQ(time_point.get(hour()), 15);
631 TEST_EQ(date_time(a_datetime, calendar("GMT+01:00")).get(hour()), 16);
632 TEST_EQ(time_point.get(hour_12()), 3);
633 TEST_EQ(time_point.get(am_pm()), 1);
634 TEST_EQ(time_point.get(minute()), 33);
635 TEST_EQ(time_point.get(second()), 13);
636 TEST_EQ(date_time(year() * 1984 + february() + day()).get(week_of_year()), 5);
637 TEST_EQ(time_point.get(week_of_month()), 1);
638
639 time_point.time(v: 24 * 3600. * 2);
640
641 time_point = year() * 2011;
642 time_point = march();
643 time_point = day() * 29;
644
645 TEST_EQ(time_point.get(year()), 2011);
646 TEST_EQ(time_point.get(month()), 2); // march
647 TEST_EQ(time_point.get(day()), 29);
648
649 date_time tp_29_march_2011 = time_point;
650
651 time_point = year() * 2011;
652 time_point = february();
653 time_point = day() * 5;
654
655 TEST_EQ(time_point.get(year()), 2011);
656 TEST_EQ(time_point.get(month()), 2); // march
657 TEST_EQ(time_point.get(day()), 5);
658
659 time_point = tp_29_march_2011;
660
661 time_point = year() * 2011 + february() + day() * 5;
662 TEST_EQ(time_point.get(year()), 2011);
663 TEST_EQ(time_point.get(month()), 1); // february
664 TEST_EQ(time_point.get(day()), 5);
665
666 // Difference
667 TEST_EQ(time_point.difference(time_point + second(3), second()), 3);
668 TEST_EQ(time_point.difference(time_point - minute(5), minute()), -5);
669 TEST_EQ(time_point.difference(time_point - minute(5), second()), -5 * 60);
670 TEST_EQ(time_point.difference(time_point + minute(5) - hour(3), hour()), -2);
671 TEST_EQ(time_point.difference(time_point + day(42) - hour(3), day()), 41);
672 TEST_EQ(time_point.difference(time_point + day(7 * 13) - hour(3), week_of_year()), 12);
673 TEST_EQ(time_point.difference(time_point + day(456), day()), 456);
674 TEST_EQ(time_point.difference(time_point + day(456), year()), 1);
675 // Same for subtracting timepoints, i.e. syntactic sugar for the above
676 TEST_EQ(((time_point + second(3)) - time_point) / second(), 3);
677 TEST_EQ(((time_point - minute(5)) - time_point) / minute(), -5);
678 TEST_EQ(((time_point - minute(5)) - time_point) / second(), -5 * 60);
679 TEST_EQ(((time_point + minute(5) - hour(3)) - time_point) / hour(), -2);
680 TEST_EQ(((time_point + day(42) - hour(3)) - time_point) / day(), 41);
681 TEST_EQ(((time_point + day(7 * 13) - hour(3)) - time_point) / week_of_year(), 12);
682 TEST_EQ(((time_point + day(456)) - time_point) / day(), 456);
683 TEST_EQ(((time_point + day(456)) - time_point) / year(), 1);
684
685 TEST_EQ((time_point + 2 * hour() - time_point) / minute(), 120);
686 TEST_EQ((time_point + month() - time_point) / day(), 28);
687 TEST_EQ((time_point + 2 * month() - (time_point + month())) / day(), 31);
688 TEST_EQ((time_point + month(2) + day(3) - time_point) / month(), 2);
689 TEST_EQ(day(time_point + 2 * month() - (time_point + month())), 31);
690 TEST_EQ((time_point + year() * 1 - hour() * 1 - time_point) / year(), 0);
691 TEST_EQ((time_point + year() * 1 - time_point) / year(), 1);
692 TEST_EQ((time_point + year() * 1 + hour() * 1 - time_point) / year(), 1);
693 TEST_EQ((time_point - year() * 1 + hour() * 1 - time_point) / year(), 0);
694 TEST_EQ((time_point - year() * 1 - time_point) / year(), -1);
695 TEST_EQ((time_point - year() * 1 - hour() * 1 - time_point) / year(), -1);
696 TEST_EQ((time_point - tp_29_march_2011) / era(), 0);
697 const date_time tp_morning = time_point = hour(v: 5) + minute(v: 7) + second(v: 42);
698 TEST_EQ(((tp_morning + am()) - tp_morning) / am_pm(), 0);
699 TEST_EQ(((tp_morning + pm()) - tp_morning) / am_pm(), 1);
700 // Same point
701 TEST_EQ((time_point - time_point) / year(), 0);
702 TEST_EQ((time_point - time_point) / month(), 0);
703 TEST_EQ((time_point - time_point) / day(), 0);
704 TEST_EQ((time_point - time_point) / hour(), 0);
705 TEST_EQ((time_point - time_point) / minute(), 0);
706 TEST_EQ((time_point - time_point) / second(), 0);
707 }
708 // Default constructed time_point
709 {
710 const time_t current_time = std::time(timer: nullptr);
711 date_time time_point_default;
712 // Defaults to current time, i.e. different than a date in 1970
713 date_time time_point_1970 = year(v: 1970) + february() + day(v: 5);
714 TEST(time_point_default != time_point_1970);
715 // We can not check an exact time as we can't know at which exact time the time point was recorded. So
716 // only check that it refers to the same hour
717 const double time_point_time = time_point_default.time();
718 TEST_GE(time_point_time, current_time);
719 constexpr double secsPerHour = 60 * 60;
720 TEST_LE(time_point_time - current_time, secsPerHour);
721 // However at least the date should match
722 const tm current_time_gmt = *gmtime_wrap(time: &current_time);
723 TEST_EQ(time_point_default.get(year()), current_time_gmt.tm_year + 1900);
724 TEST_EQ(time_point_default.get(month()), current_time_gmt.tm_mon);
725 TEST_EQ(time_point_default.get(day()), current_time_gmt.tm_mday);
726
727 // Uses the current global timezone
728 time_zone::global(new_tz: "GMT");
729 date_time tp_gmt;
730 time_zone::global(new_tz: "GMT+01:00");
731 date_time tp_gmt1;
732 // Both refer to the same point in time (i.e. comparison ignores timezones)
733 // Unless the system clock resolution is high enough to detect that the 2 instances
734 // are not created in the exact same second
735 TEST((tp_gmt == tp_gmt1) || (tp_gmt1 - tp_gmt) / second() < 5);
736
737 // But getting the hour shows the difference of 1 hour
738 const int gmt_h = tp_gmt.get(f: hour());
739 // Handle overflow to next day
740 const int expected_gmt1_h = (gmt_h == tp_gmt.maximum(f: hour())) ? tp_gmt.minimum(f: hour()) : gmt_h + 1;
741 TEST_EQ(expected_gmt1_h, tp_gmt1.get(hour()));
742 // Adding the hour automatically handles the overflow, so this works too
743 tp_gmt += hour();
744 TEST_EQ(tp_gmt.get(hour()), tp_gmt1.get(hour()));
745 }
746 // Construction from time-set and calendar/time_point normalizes (e.g. invalid day of month)
747 {
748 const calendar cal;
749 date_time tp1(cal);
750 date_time tp2(year(v: 2001) + march() + day(v: 34), cal);
751 TEST_EQ(tp2 / year(), 2001);
752 TEST_EQ(tp2 / month(), 3);
753 TEST_EQ(tp2 / day(), 3);
754 TEST_EQ(tp2 / hour(), tp1 / hour());
755 TEST_EQ(tp2 / minute(), tp1 / minute());
756 TEST_EQ(tp2 / second(), tp1 / second());
757 date_time tp3(tp2, year(v: 2002) + january() + day(v: 35));
758 TEST_EQ(tp3 / year(), 2002);
759 TEST_EQ(tp3 / month(), 1);
760 TEST_EQ(tp3 / day(), 4);
761 TEST_EQ(tp3 / hour(), tp2 / hour());
762 TEST_EQ(tp3 / minute(), tp2 / minute());
763 TEST_EQ(tp3 / second(), tp2 / second());
764 }
765 } // for loop
766}
767

source code of boost/libs/locale/test/test_date_time.cpp