summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/std/time/year_month_day/1.cc
blob: 24546c36fd57b968673f56f6774f0c428d1424ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// { dg-options "-std=gnu++2a" }
// { dg-do compile { target c++2a } }

// Copyright (C) 2020 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.

// Class template day [time.cal.year_month_day]

#include <chrono>

constexpr void
constexpr_year_month_day()
{
  using namespace std::chrono;
  using ymd = year_month_day;

  constexpr ymd ymd1{year{1984}, August, 3d};
  static_assert(ymd1.ok());
  static_assert(ymd1.year() == year{1984});
  static_assert(ymd1.month() == August);
  static_assert(ymd1.day() == 3d);
  //static_assert(sys_days(ymd1) == time_point_cast<days>(days{5356}));
  //static_assert(local_days(ymd1) == time_point_cast<days>(days{5356}));

  static_assert(2015y/August/14d == ymd{year{2015}, month{8}, day{14}});
  static_assert(2015y/August/14 == ymd{year{2015}, month{8}, day{14}});
  static_assert(2015y/(August/14d) == ymd{year{2015}, month{8}, day{14}});
  static_assert(2015/(August/14d) == ymd{year{2015}, month{8}, day{14}});
  static_assert(August/14d/2015y == ymd{year{2015}, month{8}, day{14}});
  static_assert(August/14d/2015 == ymd{year{2015}, month{8}, day{14}});

  static_assert(((ymd{1000y, January, 1d} += months{1}) += years{1})
		== February/1d/1001y);
  static_assert(((ymd{1000y, January, 1d} -= years{1}) -= months{1})
		== December/1d/998y);

  static_assert(!ymd{1000y, February, 30d}.ok());

  static_assert(June/1d/1977y == June/1d/1977y);
  static_assert(June/1d/1977y != June/1d/1987y);
  static_assert(May/15d/1950y <=> May/15d/1950y == std::strong_ordering::equal);
  static_assert(May/15d/1950y <=> May/14d/1950y == std::strong_ordering::greater);
  static_assert(April/15d/1950y <=> May/14d/1950y == std::strong_ordering::less);

  static_assert(January/1d/1900y + months{13} == February/1d/1901y);
  static_assert(months{13} + January/1d/1900y == February/1d/1901y);
  static_assert(January/1d/1900y + years{1} == January/1d/1901y);
  static_assert(years{1} + January/1d/1900y == January/1d/1901y);
  static_assert(January/1d/1900y - months{13} == December/1d/1898y);
  static_assert(January/1d/1900y - years{1} == January/1d/1899y);

  // N.B. unix seems to be a macro somewhere!
  constexpr ymd myunix = 1970y/1/1;
  static_assert(myunix.ok());
  static_assert(myunix.year() == year{1970});
  static_assert(myunix.month() == January);
  static_assert(myunix.day() == day{1});
  static_assert(sys_days(myunix).time_since_epoch() == days{0});
  //static_assert(local_days(myunix) == time_point_cast<days>(days{0}));

  static_assert(sys_days{August/20d/2020y}.time_since_epoch() == days{18494});

  static_assert(ymd{sys_days{2017y/January/0}}  == 2016y/December/31);
  static_assert(ymd{sys_days{2017y/January/31}} == 2017y/January/31);
  static_assert(ymd{sys_days{2017y/January/32}} == 2017y/February/1);
  static_assert(ymd{sys_days{2017y/33/59 + months{0}}} == 2019y/10/29);

  static_assert(ymd{local_days{2017y/January/0}}  == 2016y/December/31);
  static_assert(ymd{local_days{2017y/January/31}} == 2017y/January/31);
  static_assert(ymd{local_days{2017y/January/32}} == 2017y/February/1);
  static_assert(ymd{local_days{2017y/33/59 + months{0}}} == 2019y/10/29);

  static_assert((2000y/February/29d).ok());
  static_assert(!(2001y/February/29d).ok());
  static_assert(!(2100y/February/29d).ok());
  static_assert(!(1999y/February/29d).ok());
}