]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Fix std::chrono::file_clock conversions for low-precision times
authorJonathan Wakely <jwakely@redhat.com>
Sun, 21 Jan 2024 18:11:16 +0000 (18:11 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Tue, 30 Jan 2024 14:54:37 +0000 (14:54 +0000)
The std::chrono::file_clock conversions were not using common_type and
so failed to compile when converting anything that should have increased
precision after arithmetic with a std::chrono::seconds value.

libstdc++-v3/ChangeLog:

* include/bits/chrono.h (__file_clock::from_sys)
(__file_clock::to_sys, __file_clock::_S_from_sys)
(__file_clock::_S_to_sys): Use common_type for return type.
* testsuite/std/time/clock/file/members.cc: Check round trip
conversion for time with lower precision that seconds.

(cherry picked from commit fba15da8fa518bfb8d3e061795bc3ca2dea01d27)

libstdc++-v3/include/bits/chrono.h
libstdc++-v3/testsuite/std/time/clock/file/members.cc

index 0aeea0ae57eb95a37706c08171335dae56051b2a..04078ff8f2dfdaaba75d234b5e934ae009a96ed9 100644 (file)
@@ -1455,14 +1455,14 @@ _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2)
 #if __cplusplus > 201703L
       template<typename _Dur>
        static
-       chrono::file_time<_Dur>
+       chrono::file_time<common_type_t<_Dur, chrono::seconds>>
        from_sys(const chrono::sys_time<_Dur>& __t) noexcept
        { return _S_from_sys(__t); }
 
       // For internal use only
       template<typename _Dur>
        static
-       chrono::sys_time<_Dur>
+       chrono::sys_time<common_type_t<_Dur, chrono::seconds>>
        to_sys(const chrono::file_time<_Dur>& __t) noexcept
        { return _S_to_sys(__t); }
 #endif // C++20
@@ -1479,20 +1479,22 @@ _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2)
       // For internal use only
       template<typename _Dur>
        static
-       chrono::time_point<__file_clock, _Dur>
+       chrono::time_point<__file_clock, common_type_t<_Dur, chrono::seconds>>
        _S_from_sys(const chrono::time_point<__sys_clock, _Dur>& __t) noexcept
        {
-         using __file_time = chrono::time_point<__file_clock, _Dur>;
+         using _CDur = common_type_t<_Dur, chrono::seconds>;
+         using __file_time = chrono::time_point<__file_clock, _CDur>;
          return __file_time{__t.time_since_epoch()} - _S_epoch_diff;
        }
 
       // For internal use only
       template<typename _Dur>
        static
-       chrono::time_point<__sys_clock, _Dur>
+       chrono::time_point<__sys_clock, common_type_t<_Dur, chrono::seconds>>
        _S_to_sys(const chrono::time_point<__file_clock, _Dur>& __t) noexcept
        {
-         using __sys_time = chrono::time_point<__sys_clock, _Dur>;
+         using _CDur = common_type_t<_Dur, chrono::seconds>;
+         using __sys_time = chrono::time_point<__sys_clock, _CDur>;
          return __sys_time{__t.time_since_epoch()} + _S_epoch_diff;
        }
     };
index b34df49c6500d754535d190ca6017e97585c8cd9..2047b029e9bd89bb1d9de065b2466a165fb66301 100644 (file)
@@ -42,9 +42,18 @@ test02()
   VERIFY( t - s < 1s );
 }
 
+void
+test03()
+{
+  using namespace std::chrono;
+  auto st = sys_days(2024y/January/21);
+  VERIFY( file_clock::to_sys(file_clock::from_sys(st)) == st );
+}
+
 int
 main()
 {
   test01();
   test02();
+  test03();
 }