| 1 |
// { dg-do compile { target c++20 } } |
| 2 |
|
| 3 |
// Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 4 |
// |
| 5 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 6 |
// software; you can redistribute it and/or modify it under the |
| 7 |
// terms of the GNU General Public License as published by the |
| 8 |
// Free Software Foundation; either version 3, or (at your option) |
| 9 |
// any later version. |
| 10 |
|
| 11 |
// This library is distributed in the hope that it will be useful, |
| 12 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
// GNU General Public License for more details. |
| 15 |
|
| 16 |
// You should have received a copy of the GNU General Public License along |
| 17 |
// with this library; see the file COPYING3. If not see |
| 18 |
// <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
// Class template hh_mm_ss [time.hh_mm_ss] |
| 21 |
|
| 22 |
#include <chrono> |
| 23 |
|
| 24 |
constexpr void |
| 25 |
constexpr_hh_mm_ss() |
| 26 |
{ |
| 27 |
using namespace std::chrono; |
| 28 |
using std::ratio; |
| 29 |
|
| 30 |
static_assert(hh_mm_ss<hours>::fractional_width == 0); |
| 31 |
static_assert(hh_mm_ss<minutes>::fractional_width == 0); |
| 32 |
static_assert(hh_mm_ss<seconds>::fractional_width == 0); |
| 33 |
static_assert(hh_mm_ss<milliseconds>::fractional_width == 3); |
| 34 |
static_assert(hh_mm_ss<microseconds>::fractional_width == 6); |
| 35 |
static_assert(hh_mm_ss<nanoseconds>::fractional_width == 9); |
| 36 |
static_assert(hh_mm_ss<duration<int, ratio<1, 2>>>::fractional_width == 1); |
| 37 |
static_assert(hh_mm_ss<duration<int, ratio<1, 3>>>::fractional_width == 6); |
| 38 |
static_assert(hh_mm_ss<duration<int, ratio<1, 4>>>::fractional_width == 2); |
| 39 |
static_assert(hh_mm_ss<duration<int, ratio<2, 4>>>::fractional_width == 1); |
| 40 |
static_assert(hh_mm_ss<duration<int, ratio<1, 5>>>::fractional_width == 1); |
| 41 |
static_assert(hh_mm_ss<duration<int, ratio<1, 6>>>::fractional_width == 6); |
| 42 |
static_assert(hh_mm_ss<duration<int, ratio<1, 7>>>::fractional_width == 6); |
| 43 |
static_assert(hh_mm_ss<duration<int, ratio<1, 8>>>::fractional_width == 3); |
| 44 |
static_assert(hh_mm_ss<duration<int, ratio<1, 9>>>::fractional_width == 6); |
| 45 |
static_assert(hh_mm_ss<duration<int, ratio<1, 10>>>::fractional_width == 1); |
| 46 |
static_assert(hh_mm_ss<duration<int, ratio<756, 625>>>::fractional_width == 4); |
| 47 |
static_assert(hh_mm_ss<duration<int, ratio<1, (1ll << 62)>>>::fractional_width |
| 48 |
== 18); |
| 49 |
|
| 50 |
constexpr auto hms = hh_mm_ss{-(8h + 9min + 10s + 11ms + 12us + 13ns)}; |
| 51 |
static_assert(__is_same(decltype(hms)::precision, nanoseconds)); |
| 52 |
static_assert(hms.hours() == 8h); |
| 53 |
static_assert(hms.minutes() == 9min); |
| 54 |
static_assert(hms.seconds() == 10s); |
| 55 |
static_assert(hms.subseconds() == 11ms + 12us + 13ns); |
| 56 |
static_assert(hms.is_negative()); |
| 57 |
static_assert(hh_mm_ss{hms.to_duration()}.to_duration() == hms.to_duration()); |
| 58 |
|
| 59 |
static_assert(seconds{hh_mm_ss{100min}} == 100min); |
| 60 |
|
| 61 |
// treat_as_floating_point_v |
| 62 |
using fseconds = duration<double, ratio<1>>; |
| 63 |
constexpr hh_mm_ss<fseconds> fsec{0x123.0004p5s}; |
| 64 |
static_assert(std::is_same_v<hh_mm_ss<fseconds>::precision, fseconds>); |
| 65 |
static_assert(fsec.hours() == 2h); |
| 66 |
static_assert(fsec.minutes() == 35min); |
| 67 |
static_assert(fsec.seconds() == 12s); |
| 68 |
static_assert(fsec.subseconds() == 0x.0004p5s); |
| 69 |
static_assert(!fsec.is_negative()); |
| 70 |
static_assert(fsec.to_duration() == 0x123.0004p5s); |
| 71 |
|
| 72 |
using fminutes = duration<double, ratio<60>>; |
| 73 |
constexpr hh_mm_ss<fminutes> fmin{-0x1.23p4min}; |
| 74 |
static_assert(std::is_same_v<hh_mm_ss<fminutes>::precision, fseconds>); |
| 75 |
static_assert(fmin.hours() == 0h); |
| 76 |
static_assert(fmin.minutes() == 18min); |
| 77 |
static_assert(fmin.seconds() == 11s); |
| 78 |
static_assert(fmin.subseconds() == 0.25s); |
| 79 |
static_assert(fmin.is_negative()); |
| 80 |
static_assert(fmin.to_duration() == -0x1.23p4min); |
| 81 |
} |
| 82 |
|
| 83 |
constexpr void |
| 84 |
default_construction() |
| 85 |
{ |
| 86 |
using namespace std::chrono; |
| 87 |
|
| 88 |
constexpr hh_mm_ss<seconds> s1; |
| 89 |
static_assert(s1.to_duration() == s1.to_duration().zero()); |
| 90 |
constexpr hh_mm_ss<duration<char>> s2; |
| 91 |
static_assert(s2.to_duration() == s2.to_duration().zero()); |
| 92 |
constexpr hh_mm_ss<duration<int, std::centi>> s3; |
| 93 |
static_assert(s3.to_duration() == s3.to_duration().zero()); |
| 94 |
constexpr hh_mm_ss<duration<long long, std::femto>> s4; |
| 95 |
static_assert(s4.to_duration() == s4.to_duration().zero()); |
| 96 |
constexpr hh_mm_ss<duration<double>> s5; |
| 97 |
static_assert(s5.to_duration() == s5.to_duration().zero()); |
| 98 |
} |
| 99 |
|
| 100 |
constexpr void |
| 101 |
size() |
| 102 |
{ |
| 103 |
using namespace std::chrono; |
| 104 |
|
| 105 |
struct S0 { long long h; char m; char s; bool neg; struct { } empty; }; |
| 106 |
static_assert(sizeof(hh_mm_ss<seconds>) == sizeof(S0)); |
| 107 |
struct S1 { long long h; char m; char s; bool neg; char ss; }; |
| 108 |
static_assert(sizeof(hh_mm_ss<duration<int, std::centi>>) == sizeof(S1)); |
| 109 |
struct S2 { long long h; char m, s; bool neg; int ss; }; |
| 110 |
static_assert(sizeof(hh_mm_ss<duration<int, std::milli>>) == sizeof(S2)); |
| 111 |
struct S3 { long long h; char m, s; bool neg; long long ss; }; |
| 112 |
static_assert(sizeof(hh_mm_ss<duration<int, std::pico>>) == sizeof(S3)); |
| 113 |
static_assert(sizeof(hh_mm_ss<duration<long long, std::pico>>) == sizeof(S3)); |
| 114 |
struct S4 { long long h; char m, s; bool neg; double ss; }; |
| 115 |
static_assert(sizeof(hh_mm_ss<duration<double, std::micro>>) == sizeof(S4)); |
| 116 |
} |
| 117 |
|
| 118 |
constexpr void |
| 119 |
unsigned_rep() |
| 120 |
{ |
| 121 |
using namespace std::chrono; |
| 122 |
|
| 123 |
constexpr duration<unsigned, std::milli> ms(3690001); |
| 124 |
|
| 125 |
constexpr hh_mm_ss hms(ms); // PR libstdc++/108265 |
| 126 |
static_assert( ! hms.is_negative() ); |
| 127 |
static_assert( hms.to_duration() == milliseconds(ms.count()) ); |
| 128 |
static_assert( hms.hours() == 1h ); |
| 129 |
static_assert( hms.minutes() == 1min ); |
| 130 |
static_assert( hms.seconds() == 30s ); |
| 131 |
static_assert( hms.subseconds() == 1ms ); |
| 132 |
} |