| 1 | // { dg-do run { target c++20 } } |
| 2 | // { dg-additional-options "-DSTART_DAY=-50000 -DEND_DAY=50000 -DSTART_YMD=1833y/February/8d" { target simulator } } |
| 3 | |
| 4 | // Copyright (C) 2021-2025 Free Software Foundation, Inc. |
| 5 | // |
| 6 | // This file is part of the GNU ISO C++ Library. This library is free |
| 7 | // software; you can redistribute it and/or modify it under the |
| 8 | // terms of the GNU General Public License as published by the |
| 9 | // Free Software Foundation; either version 3, or (at your option) |
| 10 | // any later version. |
| 11 | |
| 12 | // This library is distributed in the hope that it will be useful, |
| 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | // GNU General Public License for more details. |
| 16 | |
| 17 | // You should have received a copy of the GNU General Public License along |
| 18 | // with this library; see the file COPYING3. If not see |
| 19 | // <http://www.gnu.org/licenses/>. |
| 20 | |
| 21 | // Class year_month_day [time.cal.year_month_day] |
| 22 | |
| 23 | #include <chrono> |
| 24 | #include <testsuite_hooks.h> |
| 25 | |
| 26 | // Slow but very clear way of advancing one day. |
| 27 | constexpr void |
| 28 | advance(std::chrono::year_month_day& ymd) noexcept { |
| 29 | |
| 30 | using namespace std::chrono; |
| 31 | |
| 32 | auto y = ymd.year(); |
| 33 | auto m = ymd.month(); |
| 34 | auto d = ymd.day(); |
| 35 | |
| 36 | if (d != year_month_day_last{year{y}, month_day_last{m}}.day()) |
| 37 | ++d; |
| 38 | else { |
| 39 | d = day{1}; |
| 40 | if (m != December) |
| 41 | ++m; |
| 42 | else { |
| 43 | m = January; |
| 44 | ++y; |
| 45 | } |
| 46 | } |
| 47 | ymd = year_month_day{y, m, d}; |
| 48 | } |
| 49 | |
| 50 | void test01() |
| 51 | { |
| 52 | using namespace std::chrono; |
| 53 | |
| 54 | #ifdef START_DAY |
| 55 | auto n = days{START_DAY}; |
| 56 | auto ymd = START_YMD; |
| 57 | auto end_day = days{END_DAY}; |
| 58 | #else |
| 59 | // [-12687428, 11248737] maps to [-32767y/January/1d, 32767y/December/31d] |
| 60 | |
| 61 | auto n = days{-12687428}; |
| 62 | auto ymd = -32767y/January/1d; |
| 63 | auto end_day = days{11248737}; |
| 64 | #endif |
| 65 | |
| 66 | while (n < end_day) { |
| 67 | VERIFY( year_month_day{sys_days{n}} == ymd ); |
| 68 | ++n; |
| 69 | advance(ymd); |
| 70 | } |
| 71 | // One more for n = 11248737 and ymd = 32767y/December/31d |
| 72 | VERIFY( 32767y/December/31d == year_month_day{sys_days{days{11248737}}} ); |
| 73 | } |
| 74 | |
| 75 | int main() |
| 76 | { |
| 77 | test01(); |
| 78 | return 0; |
| 79 | } |