1 // { dg-options "-std=gnu++2a" }
2 // { dg-do compile { target c++2a } }
3 
4 // Copyright (C) 2020-2021 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 template year_month [time.cal.year_month]
22 
23 #include <chrono>
24 
25 constexpr void
constexpr_year_month()26 constexpr_year_month()
27 {
28   using namespace std::chrono;
29   using ym = year_month;
30 
31   ym ym0 = 2015y/April;
32   ym0 += years{100};
33   ym0 -= years{100};
34   ym0 += months{50};
35   ym0 -= months{50};
36 
37   constexpr ym ym1 = {2015y, June};
38   static_assert(ym1.year() == year{2015});
39   static_assert(ym1.month() == June);
40   static_assert(ym1.ok());
41 
42   constexpr ym ym2 = {2016y, May};
43   static_assert(ym2.year() == year{2016});
44   static_assert(ym2.month() == May);
45   static_assert(ym2.ok());
46 
47   static_assert(ym1 == ym1);
48   static_assert(ym1 != ym2);
49   static_assert(ym1 < ym2);
50   static_assert(ym1 <= ym2);
51   static_assert(ym2 > ym1);
52   static_assert(ym2 >= ym2);
53 
54   static_assert(ym1 <=> ym1 == std::strong_ordering::equal);
55   static_assert(ym1 <=> ym2 == std::strong_ordering::less);
56   static_assert(ym2 <=> ym1 == std::strong_ordering::greater);
57 
58   static_assert(2015y/August == ym{year{2015}, August});
59   static_assert(2015y/8 == ym{year{2015}, August});
60 
61   static_assert(ym1 + months{6} == 2015y/December);
62   static_assert(ym1 + months{7} == 2016y/January);
63   static_assert(months{24} + ym1 == 2017y/June);
64   static_assert(months{25} + ym1 == 2017y/July);
65 
66   static_assert(ym1 + months{-5} == 2015y/January);
67   static_assert(ym1 + months{-6} == 2014y/December);
68   static_assert(ym1 + months{-24} == 2013y/June);
69   static_assert(ym1 + months{-25} == 2013y/May);
70 
71   static_assert(ym1 - months{5} == 2015y/January);
72   static_assert(ym1 - months{6} == 2014y/December);
73   static_assert(ym1 - months{24} == 2013y/June);
74   static_assert(ym1 - months{25} == 2013y/May);
75 
76   static_assert(ym2 - ym1 == months{11});
77   static_assert(ym1 - ym2 == -months{11});
78 
79   static_assert(ym2 + years{1} == 2017y/May);
80   static_assert(ym2 + years{-1} == 2015y/May);
81   static_assert(ym2 - years{1} == 2015y/May);
82 
83   static_assert(2017y/33 + months{0} == 2019y/9);
84 
85   static_assert(2010y/January + months{-12} == 2009y/January);
86 
87   static_assert(2010y/month{0} + months{-1} == 2009y/November);
88   static_assert(2010y/month{0} + months{0} == 2009y/December);
89   static_assert(2010y/month{0} + months{1} == 2010y/January);
90   static_assert(2010y/month{0} + months{2} == 2010y/February);
91   static_assert(2010y/month{0} + months{11} == 2010y/November);
92   static_assert(2010y/month{0} + months{12} == 2010y/December);
93   static_assert(2010y/month{0} + months{13} == 2011y/January);
94 
95   static_assert(months{-1} + 2010y/month{37} == 2012y/December);
96   static_assert(months{0} + 2010y/month{37} == 2013y/January);
97   static_assert(months{1} + 2010y/month{37} == 2013y/February);
98 }
99