[committed] libstdc++: Fix std::chrono::hh_mm_ss with unsigned rep [PR108265]
Checks
Commit Message
Tested x86_64-linux. Pushed to trunk, backports to gcc-11 and gcc-12
will follow.
-- >8 --
libstdc++-v3/ChangeLog:
PR libstdc++/108265
* include/std/chrono (hh_mm_ss): Do not use chrono::abs if
duration rep is unsigned.
* testsuite/std/time/hh_mm_ss/1.cc: Check unsigned rep.
---
libstdc++-v3/include/std/chrono | 15 ++++++++++++---
libstdc++-v3/testsuite/std/time/hh_mm_ss/1.cc | 16 ++++++++++++++++
2 files changed, 28 insertions(+), 3 deletions(-)
@@ -2294,7 +2294,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
constexpr
- hh_mm_ss(_Duration __d, bool __is_neg) noexcept
+ hh_mm_ss(_Duration __d, bool __is_neg)
: _M_h (duration_cast<chrono::hours>(__d)),
_M_m (duration_cast<chrono::minutes>(__d - hours())),
_M_s (duration_cast<chrono::seconds>(__d - hours() - minutes())),
@@ -2307,6 +2307,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
_M_ss._M_r = duration_cast<precision>(__ss).count();
}
+ static constexpr _Duration
+ _S_abs(_Duration __d)
+ {
+ if constexpr (numeric_limits<typename _Duration::rep>::is_signed)
+ return chrono::abs(__d);
+ else
+ return __d;
+ }
+
public:
static constexpr unsigned fractional_width = {_S_fractional_width()};
@@ -2318,8 +2327,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
constexpr hh_mm_ss() noexcept = default;
constexpr explicit
- hh_mm_ss(_Duration __d) noexcept
- : hh_mm_ss(chrono::abs(__d), __d < _Duration::zero())
+ hh_mm_ss(_Duration __d)
+ : hh_mm_ss(_S_abs(__d), __d < _Duration::zero())
{ }
constexpr bool
@@ -115,3 +115,19 @@ size()
struct S4 { long long h; char m, s; bool neg; double ss; };
static_assert(sizeof(hh_mm_ss<duration<double, std::micro>>) == sizeof(S4));
}
+
+constexpr void
+unsigned_rep()
+{
+ using namespace std::chrono;
+
+ constexpr duration<unsigned, std::milli> ms(3690001);
+
+ constexpr hh_mm_ss hms(ms); // PR libstdc++/108265
+ static_assert( ! hms.is_negative() );
+ static_assert( hms.to_duration() == milliseconds(ms.count()) );
+ static_assert( hms.hours() == 1h );
+ static_assert( hms.minutes() == 1min );
+ static_assert( hms.seconds() == 30s );
+ static_assert( hms.subseconds() == 1ms );
+}