Index: libcxx/include/chrono =================================================================== --- libcxx/include/chrono +++ libcxx/include/chrono @@ -612,13 +612,43 @@ constexpr year_month_weekday_last operator/(const month_weekday_last& mwdl, int y) noexcept; -// 25.9, class template time_of_day // C++20 -template class time_of_day; +// 26.9, class template hh_mm_ss +template +class hh_mm_ss +{ + bool is_neg; // exposition only + chrono::hours h; // exposition only + chrono::minutes m; // exposition only + chrono::seconds s; // exposition only + precision ss; // exposition only + +public: + static unsigned constexpr fractional_width = see below; + using precision = see below; + + constexpr hh_mm_ss() noexcept : hh_mm_ss{Duration::zero()} {} + constexpr explicit hh_mm_ss(Duration d) noexcept; + + constexpr bool is_negative() const noexcept; + constexpr chrono::hours hours() const noexcept; + constexpr chrono::minutes minutes() const noexcept; + constexpr chrono::seconds seconds() const noexcept; + constexpr precision subseconds() const noexcept; + + constexpr explicit operator precision() const noexcept; + constexpr precision to_duration() const noexcept; +}; + +template + basic_ostream& + operator<<(basic_ostream& os, hh_mm_ss const& hms); + +// 26.10, 12/24 hour functions +constexpr bool is_am(hours const& h) noexcept; +constexpr bool is_pm(hours const& h) noexcept; +constexpr hours make12(const hours& h) noexcept; +constexpr hours make24(const hours& h, bool is_pm) noexcept; -template<> class time_of_day; -template<> class time_of_day; -template<> class time_of_day; -template class time_of_day>; // 25.10.2, time zone database // C++20 struct tzdb; @@ -2716,6 +2746,84 @@ inline constexpr year_month_weekday_last& year_month_weekday_last::operator+=(const years& __dy) noexcept { *this = *this + __dy; return *this; } inline constexpr year_month_weekday_last& year_month_weekday_last::operator-=(const years& __dy) noexcept { *this = *this - __dy; return *this; } + +template +class hh_mm_ss +{ +private: + static_assert(__is_duration<_Duration>::value, "template parameter of hh_mm_ss must be a std::chrono::duration"); + using __CommonType = common_type_t<_Duration, chrono::seconds>; + + static constexpr uint64_t __pow10(unsigned __exp) + { + uint64_t __ret = 1; + for (unsigned __i = 0; __i < __exp; ++__i) + __ret *= 10U; + return __ret; + } + + static constexpr unsigned __width(uint64_t __n, uint64_t __d = 10, unsigned __w = 0) + { + if (__n >= 2 && __d != 0 && __w < 19) + return 1 + __width(__n, __d % __n * 10, __w+1); + return 0; + } + +public: + static unsigned constexpr fractional_width = __width(__CommonType::period::den) < 19 ? + __width(__CommonType::period::den) : 6u; + using precision = duration>; + + constexpr hh_mm_ss() noexcept : hh_mm_ss{_Duration::zero()} {} + + constexpr explicit hh_mm_ss(_Duration __d) noexcept : + __is_neg(__d < _Duration(0)), + __h(duration_cast (abs(__d))), + __m(duration_cast(abs(__d) - hours())), + __s(duration_cast(abs(__d) - hours() - minutes())), + __f(duration_cast (abs(__d) - hours() - minutes() - seconds())) + {} + + constexpr bool is_negative() const noexcept { return __is_neg; } + constexpr chrono::hours hours() const noexcept { return __h; } + constexpr chrono::minutes minutes() const noexcept { return __m; } + constexpr chrono::seconds seconds() const noexcept { return __s; } + constexpr precision subseconds() const noexcept { return __f; } + + constexpr precision to_duration() const noexcept + { + auto __dur = __h + __m + __s + __f; + return __is_neg ? -__dur : __dur; + } + + constexpr explicit operator precision() const noexcept { return to_duration(); } + +private: + bool __is_neg; + chrono::hours __h; + chrono::minutes __m; + chrono::seconds __s; + precision __f; +}; + +constexpr bool is_am(const hours& __h) noexcept { return __h >= hours( 0) && __h < hours(12); } +constexpr bool is_pm(const hours& __h) noexcept { return __h >= hours(12) && __h < hours(24); } + +constexpr hours make12(const hours& __h) noexcept +{ + if (__h == hours( 0)) return hours(12); + else if (__h <= hours(12)) return __h; + else return __h - hours(12); +} + +constexpr hours make24(const hours& __h, bool __is_pm) noexcept +{ + if (__is_pm) + return __h == hours(12) ? __h : __h + hours(12); + else + return __h == hours(12) ? hours(0) : __h; +} + #endif // _LIBCPP_STD_VER > 17 } // chrono Index: libcxx/test/std/utilities/time/time.hms/hhmmss.fail.cpp =================================================================== --- /dev/null +++ libcxx/test/std/utilities/time/time.hms/hhmmss.fail.cpp @@ -0,0 +1,29 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 +// + +// template class hh_mm_ss; +// If Duration is not an instance of duration, the program is ill-formed. + +#include +#include +#include +#include "test_macros.h" + +struct A {}; + +int main(int, char**) +{ + std::chrono::hh_mm_ss h0; // expected-error-re@chrono:* {{static_assert failed {{.*}} "template parameter of hh_mm_ss must be a std::chrono::duration"}} + std::chrono::hh_mm_ss h1; // expected-error-re@chrono:* {{static_assert failed {{.*}} "template parameter of hh_mm_ss must be a std::chrono::duration"}} + std::chrono::hh_mm_ss h2; // expected-error-re@chrono:* {{static_assert failed {{.*}} "template parameter of hh_mm_ss must be a std::chrono::duration"}} + std::chrono::hh_mm_ss h3; // expected-error-re@chrono:* {{static_assert failed {{.*}} "template parameter of hh_mm_ss must be a std::chrono::duration"}} + + return 0; +} Index: libcxx/test/std/utilities/time/time.hms/time.hms.members/hours.pass.cpp =================================================================== --- /dev/null +++ libcxx/test/std/utilities/time/time.hms/time.hms.members/hours.pass.cpp @@ -0,0 +1,69 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 +// + +// template +// class hh_mm_ss +// +// constexpr chrono::hours hours() const noexcept; + +// Test values +// duration hours minutes seconds fractional count +// 5000s 1 23 20 0 5000 +// 5000 minutes 83 20 0 0 300000 +// 11h 11 0 0 0 39600 +// 123456789ms 34 17 36 789ms 123456789 +// 123456789us 0 2 3 456789us 123456789 +// 123456789ns 0 0 0 123456789ns 123456789 +// 1000mfn 2 20 9 0.6 (6000/10000) 12096000 +// 10000mfn 3 21 36 0 120960000 + + +#include +#include + +#include "test_macros.h" + +template +constexpr long check_hours(Duration d) +{ + using HMS = std::chrono::hh_mm_ss; + ASSERT_SAME_TYPE(std::chrono::hours, decltype(std::declval().hours())); + ASSERT_NOEXCEPT( std::declval().hours()); + return HMS(d).hours().count(); +} + +int main(int, char**) +{ + using microfortnights = std::chrono::duration>; + + static_assert( check_hours(std::chrono::minutes( 1)) == 0, ""); + static_assert( check_hours(std::chrono::minutes(-1)) == 0, ""); + + assert( check_hours(std::chrono::seconds( 5000)) == 1); + assert( check_hours(std::chrono::seconds(-5000)) == 1); + assert( check_hours(std::chrono::minutes( 5000)) == 83); + assert( check_hours(std::chrono::minutes(-5000)) == 83); + assert( check_hours(std::chrono::hours( 11)) == 11); + assert( check_hours(std::chrono::hours(-11)) == 11); + + assert( check_hours(std::chrono::milliseconds( 123456789LL)) == 34); + assert( check_hours(std::chrono::milliseconds(-123456789LL)) == 34); + assert( check_hours(std::chrono::microseconds( 123456789LL)) == 0); + assert( check_hours(std::chrono::microseconds(-123456789LL)) == 0); + assert( check_hours(std::chrono::nanoseconds( 123456789LL)) == 0); + assert( check_hours(std::chrono::nanoseconds(-123456789LL)) == 0); + + assert( check_hours(microfortnights( 1000)) == 0); + assert( check_hours(microfortnights( -1000)) == 0); + assert( check_hours(microfortnights( 10000)) == 3); + assert( check_hours(microfortnights(-10000)) == 3); + + return 0; +} Index: libcxx/test/std/utilities/time/time.hms/time.hms.members/is_negative.pass.cpp =================================================================== --- /dev/null +++ libcxx/test/std/utilities/time/time.hms/time.hms.members/is_negative.pass.cpp @@ -0,0 +1,54 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 +// + +// template +// class hh_mm_ss +// +// constexpr bool is_negative() const noexcept; + +#include +#include + +#include "test_macros.h" + +template +constexpr bool check_neg(Duration d) +{ + ASSERT_SAME_TYPE(bool, decltype(std::declval>().is_negative())); + ASSERT_NOEXCEPT( std::declval>().is_negative()); + return std::chrono::hh_mm_ss(d).is_negative(); +} + +int main(int, char**) +{ + using microfortnights = std::chrono::duration>; + + static_assert(!check_neg(std::chrono::minutes( 1)), ""); + static_assert( check_neg(std::chrono::minutes(-1)), ""); + + assert(!check_neg(std::chrono::seconds( 5000))); + assert( check_neg(std::chrono::seconds(-5000))); + assert(!check_neg(std::chrono::minutes( 5000))); + assert( check_neg(std::chrono::minutes(-5000))); + assert(!check_neg(std::chrono::hours( 11))); + assert( check_neg(std::chrono::hours(-11))); + + assert(!check_neg(std::chrono::milliseconds( 123456789LL))); + assert( check_neg(std::chrono::milliseconds(-123456789LL))); + assert(!check_neg(std::chrono::microseconds( 123456789LL))); + assert( check_neg(std::chrono::microseconds(-123456789LL))); + assert(!check_neg(std::chrono::nanoseconds( 123456789LL))); + assert( check_neg(std::chrono::nanoseconds(-123456789LL))); + + assert(!check_neg(microfortnights( 10000))); + assert( check_neg(microfortnights(-10000))); + + return 0; +} Index: libcxx/test/std/utilities/time/time.hms/time.hms.members/minutes.pass.cpp =================================================================== --- /dev/null +++ libcxx/test/std/utilities/time/time.hms/time.hms.members/minutes.pass.cpp @@ -0,0 +1,57 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 +// + +// template +// class hh_mm_ss +// +// constexpr chrono::minutes minutes() const noexcept; + +#include +#include + +#include "test_macros.h" + +template +constexpr long check_minutes(Duration d) +{ + using HMS = std::chrono::hh_mm_ss; + ASSERT_SAME_TYPE(std::chrono::minutes, decltype(std::declval().minutes())); + ASSERT_NOEXCEPT( std::declval().minutes()); + return HMS(d).minutes().count(); +} + +int main(int, char**) +{ + using microfortnights = std::chrono::duration>; + + static_assert( check_minutes(std::chrono::minutes( 1)) == 1, ""); + static_assert( check_minutes(std::chrono::minutes(-1)) == 1, ""); + + assert( check_minutes(std::chrono::seconds( 5000)) == 23); + assert( check_minutes(std::chrono::seconds(-5000)) == 23); + assert( check_minutes(std::chrono::minutes( 5000)) == 20); + assert( check_minutes(std::chrono::minutes(-5000)) == 20); + assert( check_minutes(std::chrono::hours( 11)) == 0); + assert( check_minutes(std::chrono::hours(-11)) == 0); + + assert( check_minutes(std::chrono::milliseconds( 123456789LL)) == 17); + assert( check_minutes(std::chrono::milliseconds(-123456789LL)) == 17); + assert( check_minutes(std::chrono::microseconds( 123456789LL)) == 2); + assert( check_minutes(std::chrono::microseconds(-123456789LL)) == 2); + assert( check_minutes(std::chrono::nanoseconds( 123456789LL)) == 0); + assert( check_minutes(std::chrono::nanoseconds(-123456789LL)) == 0); + + assert( check_minutes(microfortnights( 1000)) == 20); + assert( check_minutes(microfortnights( -1000)) == 20); + assert( check_minutes(microfortnights( 10000)) == 21); + assert( check_minutes(microfortnights(-10000)) == 21); + + return 0; +} Index: libcxx/test/std/utilities/time/time.hms/time.hms.members/precision.pass.cpp =================================================================== --- /dev/null +++ libcxx/test/std/utilities/time/time.hms/time.hms.members/precision.pass.cpp @@ -0,0 +1,58 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 +// + +// template +// class hh_mm_ss +// +// constexpr explicit operator precision() const noexcept; + + +#include +#include + +#include "test_macros.h" + +template +constexpr long long check_precision(Duration d) +{ + using HMS = std::chrono::hh_mm_ss; + ASSERT_NOEXCEPT(static_cast(std::declval())); + + return static_cast(HMS(d)).count(); +} + +int main(int, char**) +{ + using microfortnights = std::chrono::duration>; + + static_assert( check_precision(std::chrono::minutes( 1)) == 60, ""); + static_assert( check_precision(std::chrono::minutes(-1)) == -60, ""); + + assert( check_precision(std::chrono::seconds( 5000)) == 5000LL); + assert( check_precision(std::chrono::seconds(-5000)) == -5000LL); + assert( check_precision(std::chrono::minutes( 5000)) == 300000LL); + assert( check_precision(std::chrono::minutes(-5000)) == -300000LL); + assert( check_precision(std::chrono::hours( 11)) == 39600LL); + assert( check_precision(std::chrono::hours(-11)) == -39600LL); + + assert( check_precision(std::chrono::milliseconds( 123456789LL)) == 123456789LL); + assert( check_precision(std::chrono::milliseconds(-123456789LL)) == -123456789LL); + assert( check_precision(std::chrono::microseconds( 123456789LL)) == 123456789LL); + assert( check_precision(std::chrono::microseconds(-123456789LL)) == -123456789LL); + assert( check_precision(std::chrono::nanoseconds( 123456789LL)) == 123456789LL); + assert( check_precision(std::chrono::nanoseconds(-123456789LL)) == -123456789LL); + + assert( check_precision(microfortnights( 1000)) == 12096000); + assert( check_precision(microfortnights( -1000)) == -12096000); + assert( check_precision(microfortnights( 10000)) == 120960000); + assert( check_precision(microfortnights(-10000)) == -120960000); + + return 0; +} Index: libcxx/test/std/utilities/time/time.hms/time.hms.members/precision_type.pass.cpp =================================================================== --- /dev/null +++ libcxx/test/std/utilities/time/time.hms/time.hms.members/precision_type.pass.cpp @@ -0,0 +1,80 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 +// + +// template +// class hh_mm_ss +// { +// public: +// static unsigned constexpr fractional_width = see below; +// using precision = see below; +// +// precision is duration, +// ratio<1, 10^^fractional_width>> + +#include +#include + +#include "test_macros.h" + +constexpr unsigned long long powers[] = { + 1ULL, + 10ULL, + 100ULL, + 1000ULL, + 10000ULL, + 100000ULL, + 1000000ULL, + 10000000ULL, + 100000000ULL, + 1000000000ULL, + 10000000000ULL, + 100000000000ULL, + 1000000000000ULL, + 10000000000000ULL, + 100000000000000ULL, + 1000000000000000ULL, + 10000000000000000ULL, + 100000000000000000ULL, + 1000000000000000000ULL, + 10000000000000000000ULL + }; + +template +constexpr bool check_precision() +{ + using HMS = std::chrono::hh_mm_ss; + using CT = std::common_type_t; + using Pre = std::chrono::duration>; + return std::is_same_v; +} + +int main(int, char**) +{ + using microfortnights = std::chrono::duration>; + + static_assert( check_precision(), ""); + static_assert( check_precision(), ""); + static_assert( check_precision(), ""); + static_assert( check_precision(), ""); + static_assert( check_precision(), ""); + static_assert( check_precision(), ""); + static_assert( check_precision>, 1>(), ""); + static_assert( check_precision>, 6>(), ""); + static_assert( check_precision>, 2>(), ""); + static_assert( check_precision>, 1>(), ""); + static_assert( check_precision>, 6>(), ""); + static_assert( check_precision>, 6>(), ""); + static_assert( check_precision>, 3>(), ""); + static_assert( check_precision>, 6>(), ""); + static_assert( check_precision>, 1>(), ""); + static_assert( check_precision(), ""); + + return 0; +} Index: libcxx/test/std/utilities/time/time.hms/time.hms.members/seconds.pass.cpp =================================================================== --- /dev/null +++ libcxx/test/std/utilities/time/time.hms/time.hms.members/seconds.pass.cpp @@ -0,0 +1,57 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 +// + +// template +// class hh_mm_ss +// +// constexpr chrono::seconds seconds() const noexcept; + +#include +#include + +#include "test_macros.h" + +template +constexpr long check_seconds(Duration d) +{ + using HMS = std::chrono::hh_mm_ss; + ASSERT_SAME_TYPE(std::chrono::seconds, decltype(std::declval().seconds())); + ASSERT_NOEXCEPT( std::declval().seconds()); + return HMS(d).seconds().count(); +} + +int main(int, char**) +{ + using microfortnights = std::chrono::duration>; + + static_assert( check_seconds(std::chrono::seconds( 1)) == 1, ""); + static_assert( check_seconds(std::chrono::seconds(-1)) == 1, ""); + + assert( check_seconds(std::chrono::seconds( 5000)) == 20); + assert( check_seconds(std::chrono::seconds(-5000)) == 20); + assert( check_seconds(std::chrono::minutes( 5000)) == 0); + assert( check_seconds(std::chrono::minutes(-5000)) == 0); + assert( check_seconds(std::chrono::hours( 11)) == 0); + assert( check_seconds(std::chrono::hours(-11)) == 0); + + assert( check_seconds(std::chrono::milliseconds( 123456789LL)) == 36); + assert( check_seconds(std::chrono::milliseconds(-123456789LL)) == 36); + assert( check_seconds(std::chrono::microseconds( 123456789LL)) == 3); + assert( check_seconds(std::chrono::microseconds(-123456789LL)) == 3); + assert( check_seconds(std::chrono::nanoseconds( 123456789LL)) == 0); + assert( check_seconds(std::chrono::nanoseconds(-123456789LL)) == 0); + + assert( check_seconds(microfortnights( 1000)) == 9); + assert( check_seconds(microfortnights( -1000)) == 9); + assert( check_seconds(microfortnights( 10000)) == 36); + assert( check_seconds(microfortnights(-10000)) == 36); + + return 0; +} Index: libcxx/test/std/utilities/time/time.hms/time.hms.members/subseconds.pass.cpp =================================================================== --- /dev/null +++ libcxx/test/std/utilities/time/time.hms/time.hms.members/subseconds.pass.cpp @@ -0,0 +1,57 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 +// + +// template +// class hh_mm_ss +// +// constexpr precision subseconds() const noexcept; + +#include +#include + +#include "test_macros.h" + +template +constexpr long check_subseconds(Duration d) +{ + using HMS = std::chrono::hh_mm_ss; + ASSERT_SAME_TYPE(typename HMS::precision, decltype(std::declval().subseconds())); + ASSERT_NOEXCEPT( std::declval().subseconds()); + return HMS(d).subseconds().count(); +} + +int main(int, char**) +{ + using microfortnights = std::chrono::duration>; + + static_assert( check_subseconds(std::chrono::seconds( 1)) == 0, ""); + static_assert( check_subseconds(std::chrono::seconds(-1)) == 0, ""); + + assert( check_subseconds(std::chrono::seconds( 5000)) == 0); + assert( check_subseconds(std::chrono::seconds(-5000)) == 0); + assert( check_subseconds(std::chrono::minutes( 5000)) == 0); + assert( check_subseconds(std::chrono::minutes(-5000)) == 0); + assert( check_subseconds(std::chrono::hours( 11)) == 0); + assert( check_subseconds(std::chrono::hours(-11)) == 0); + + assert( check_subseconds(std::chrono::milliseconds( 123456789LL)) == 789); + assert( check_subseconds(std::chrono::milliseconds(-123456789LL)) == 789); + assert( check_subseconds(std::chrono::microseconds( 123456789LL)) == 456789LL); + assert( check_subseconds(std::chrono::microseconds(-123456789LL)) == 456789LL); + assert( check_subseconds(std::chrono::nanoseconds( 123456789LL)) == 123456789LL); + assert( check_subseconds(std::chrono::nanoseconds(-123456789LL)) == 123456789LL); + + assert( check_subseconds(microfortnights( 1000)) == 6000); + assert( check_subseconds(microfortnights( -1000)) == 6000); + assert( check_subseconds(microfortnights( 10000)) == 0); + assert( check_subseconds(microfortnights(-10000)) == 0); + + return 0; +} Index: libcxx/test/std/utilities/time/time.hms/time.hms.members/to_duration.pass.cpp =================================================================== --- /dev/null +++ libcxx/test/std/utilities/time/time.hms/time.hms.members/to_duration.pass.cpp @@ -0,0 +1,59 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 +// + +// template +// class hh_mm_ss +// +// constexpr precision to_duration() const noexcept; + + +#include +#include + +#include "test_macros.h" + +template +constexpr long long check_duration(Duration d) +{ + using HMS = std::chrono::hh_mm_ss; + ASSERT_SAME_TYPE(typename HMS::precision, decltype(std::declval().to_duration())); + ASSERT_NOEXCEPT( std::declval().to_duration()); + + return HMS(d).to_duration().count(); +} + +int main(int, char**) +{ + using microfortnights = std::chrono::duration>; + + static_assert( check_duration(std::chrono::minutes( 1)) == 60, ""); + static_assert( check_duration(std::chrono::minutes(-1)) == -60, ""); + + assert( check_duration(std::chrono::seconds( 5000)) == 5000LL); + assert( check_duration(std::chrono::seconds(-5000)) == -5000LL); + assert( check_duration(std::chrono::minutes( 5000)) == 300000LL); + assert( check_duration(std::chrono::minutes(-5000)) == -300000LL); + assert( check_duration(std::chrono::hours( 11)) == 39600LL); + assert( check_duration(std::chrono::hours(-11)) == -39600LL); + + assert( check_duration(std::chrono::milliseconds( 123456789LL)) == 123456789LL); + assert( check_duration(std::chrono::milliseconds(-123456789LL)) == -123456789LL); + assert( check_duration(std::chrono::microseconds( 123456789LL)) == 123456789LL); + assert( check_duration(std::chrono::microseconds(-123456789LL)) == -123456789LL); + assert( check_duration(std::chrono::nanoseconds( 123456789LL)) == 123456789LL); + assert( check_duration(std::chrono::nanoseconds(-123456789LL)) == -123456789LL); + + assert( check_duration(microfortnights( 1000)) == 12096000); + assert( check_duration(microfortnights( -1000)) == -12096000); + assert( check_duration(microfortnights( 10000)) == 120960000); + assert( check_duration(microfortnights(-10000)) == -120960000); + + return 0; +} Index: libcxx/test/std/utilities/time/time.hms/time.hms.members/width.pass.cpp =================================================================== --- /dev/null +++ libcxx/test/std/utilities/time/time.hms/time.hms.members/width.pass.cpp @@ -0,0 +1,58 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 +// + +// template +// class hh_mm_ss +// { +// public: +// static unsigned constexpr fractional_width = see below; +// using precision = see below; +// +// fractional_width is the number of fractional decimal digits represented by precision. +// fractional_width has the value of the smallest possible integer in the range [0, 18] +// such that precision will exactly represent all values of Duration. +// If no such value of fractional_width exists, then fractional_width is 6. + + +#include +#include + +#include "test_macros.h" + +template +constexpr bool check_width() +{ + using HMS = std::chrono::hh_mm_ss; + return HMS::fractional_width == width; +} + +int main(int, char**) +{ + using microfortnights = std::chrono::duration>; + + static_assert( check_width(), ""); + static_assert( check_width(), ""); + static_assert( check_width(), ""); + static_assert( check_width(), ""); + static_assert( check_width(), ""); + static_assert( check_width(), ""); + static_assert( check_width>, 1>(), ""); + static_assert( check_width>, 6>(), ""); + static_assert( check_width>, 2>(), ""); + static_assert( check_width>, 1>(), ""); + static_assert( check_width>, 6>(), ""); + static_assert( check_width>, 6>(), ""); + static_assert( check_width>, 3>(), ""); + static_assert( check_width>, 6>(), ""); + static_assert( check_width>, 1>(), ""); + static_assert( check_width(), ""); + + return 0; +} Index: libcxx/test/std/utilities/time/time.hms/time.hms.nonmembers/nothing.to.do.pass.cpp =================================================================== --- /dev/null +++ libcxx/test/std/utilities/time/time.hms/time.hms.nonmembers/nothing.to.do.pass.cpp @@ -0,0 +1,21 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// + +// template class hh_mm_ss; + +#include +#include + +#include "test_macros.h" + +int main(int, char**) +{ + return 0; +} Index: libcxx/test/std/utilities/time/time.hms/time.12/is_am.pass.cpp =================================================================== --- /dev/null +++ libcxx/test/std/utilities/time/time.hms/time.12/is_am.pass.cpp @@ -0,0 +1,36 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 +// + +// constexpr bool is_am(const hours& h) noexcept; +// Returns: 0h <= h && h <= 11h. + +#include +#include + +#include "test_macros.h" + +int main(int, char**) +{ + using hours = std::chrono::hours; + ASSERT_SAME_TYPE(bool, decltype(std::chrono::is_am(std::declval()))); + ASSERT_NOEXCEPT( std::chrono::is_am(std::declval())); + + static_assert( std::chrono::is_am(hours( 0)), ""); + static_assert( std::chrono::is_am(hours(11)), ""); + static_assert(!std::chrono::is_am(hours(12)), ""); + static_assert(!std::chrono::is_am(hours(23)), ""); + + for (int i = 0; i < 12; ++i) + assert( std::chrono::is_am(hours(i))); + for (int i = 12; i < 24; ++i) + assert(!std::chrono::is_am(hours(i))); + + return 0; +} Index: libcxx/test/std/utilities/time/time.hms/time.12/is_pm.pass.cpp =================================================================== --- /dev/null +++ libcxx/test/std/utilities/time/time.hms/time.12/is_pm.pass.cpp @@ -0,0 +1,36 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 +// + +// constexpr bool is_pm(const hours& h) noexcept; +// Returns: 12h <= h && h <= 23 + +#include +#include + +#include "test_macros.h" + +int main(int, char**) +{ + using hours = std::chrono::hours; + ASSERT_SAME_TYPE(bool, decltype(std::chrono::is_pm(std::declval()))); + ASSERT_NOEXCEPT( std::chrono::is_pm(std::declval())); + + static_assert(!std::chrono::is_pm(hours( 0)), ""); + static_assert(!std::chrono::is_pm(hours(11)), ""); + static_assert( std::chrono::is_pm(hours(12)), ""); + static_assert( std::chrono::is_pm(hours(23)), ""); + + for (int i = 0; i < 12; ++i) + assert(!std::chrono::is_pm(hours(i))); + for (int i = 12; i < 24; ++i) + assert( std::chrono::is_pm(hours(i))); + + return 0; +} Index: libcxx/test/std/utilities/time/time.hms/time.12/make12.pass.cpp =================================================================== --- /dev/null +++ libcxx/test/std/utilities/time/time.hms/time.12/make12.pass.cpp @@ -0,0 +1,38 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 +// + +// constexpr hours make12(const hours& h) noexcept; +// Returns: The 12-hour equivalent of h in the range [1h, 12h]. +// If h is not in the range [0h, 23h], the value returned is unspecified. + +#include +#include + +#include "test_macros.h" + +int main(int, char**) +{ + using hours = std::chrono::hours; + ASSERT_SAME_TYPE(hours, decltype(std::chrono::make12(std::declval()))); + ASSERT_NOEXCEPT( std::chrono::make12(std::declval())); + + static_assert( std::chrono::make12(hours( 0)) == hours(12), ""); + static_assert( std::chrono::make12(hours(11)) == hours(11), ""); + static_assert( std::chrono::make12(hours(12)) == hours(12), ""); + static_assert( std::chrono::make12(hours(23)) == hours(11), ""); + + assert( std::chrono::make12(hours(0)) == hours(12)); + for (int i = 1; i < 13; ++i) + assert( std::chrono::make12(hours(i)) == hours(i)); + for (int i = 13; i < 24; ++i) + assert( std::chrono::make12(hours(i)) == hours(i-12)); + + return 0; +} Index: libcxx/test/std/utilities/time/time.hms/time.12/make24.pass.cpp =================================================================== --- /dev/null +++ libcxx/test/std/utilities/time/time.hms/time.12/make24.pass.cpp @@ -0,0 +1,45 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 +// + +// constexpr hours make24(const hours& h, bool is_pm) noexcept; +// Returns: If is_pm is false, returns the 24-hour equivalent of h in the range [0h, 11h], +// assuming h represents an ante meridiem hour. +// Else returns the 24-hour equivalent of h in the range [12h, 23h], +// assuming h represents a post meridiem hour. +// If h is not in the range [1h, 12h], the value returned is unspecified. + +#include +#include + +#include "test_macros.h" + +int main(int, char**) +{ + using hours = std::chrono::hours; + ASSERT_SAME_TYPE(hours, decltype(std::chrono::make24(std::declval(), false))); + ASSERT_NOEXCEPT( std::chrono::make24(std::declval(), false)); + + static_assert( std::chrono::make24(hours( 1), false) == hours( 1), ""); + static_assert( std::chrono::make24(hours(11), false) == hours(11), ""); + static_assert( std::chrono::make24(hours(12), false) == hours( 0), ""); + static_assert( std::chrono::make24(hours( 1), true) == hours(13), ""); + static_assert( std::chrono::make24(hours(11), true) == hours(23), ""); + static_assert( std::chrono::make24(hours(12), true) == hours(12), ""); + + for (int i = 1; i < 11; ++i) + { + assert((std::chrono::make24(hours(i), false)) == hours(i)); + assert((std::chrono::make24(hours(i), true)) == hours(12+i)); + } + assert((std::chrono::make24(hours(12), false)) == hours( 0)); + assert((std::chrono::make24(hours(12), true)) == hours(12)); + + return 0; +}