@@ -2164,6 +2164,8 @@ namespace __detail
year_month_day _M_ymd{};
weekday _M_wd{};
__format::_ChronoParts _M_need;
+ unsigned _M_is_leap_second : 1 {};
+ unsigned _M_reserved : 15 {};
template<typename _CharT, typename _Traits, typename _Alloc>
basic_istream<_CharT, _Traits>&
@@ -2742,8 +2744,13 @@ namespace __detail
__detail::_Parser_t<_Duration> __p(__need);
if (__p(__is, __fmt, __abbrev, __offset))
{
- auto __st = __p._M_sys_days + __p._M_time - *__offset;
- __tp = chrono::time_point_cast<_Duration>(__st);
+ if (__p._M_is_leap_second)
+ __is.setstate(ios_base::failbit);
+ else
+ {
+ auto __st = __p._M_sys_days + __p._M_time - *__offset;
+ __tp = chrono::time_point_cast<_Duration>(__st);
+ }
}
return __is;
}
@@ -2765,9 +2772,21 @@ namespace __detail
basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
minutes* __offset = nullptr)
{
- sys_time<_Duration> __st;
- if (chrono::from_stream(__is, __fmt, __st, __abbrev, __offset))
- __tp = utc_clock::from_sys(__st);
+ minutes __off{};
+ if (!__offset)
+ __offset = &__off;
+ using __format::_ChronoParts;
+ auto __need = _ChronoParts::_Year | _ChronoParts::_Month
+ | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
+ __detail::_Parser_t<_Duration> __p(__need);
+ if (__p(__is, __fmt, __abbrev, __offset))
+ {
+ // Converting to utc_time before adding _M_time is necessary for
+ // "23:59:60" to correctly produce a time within a leap second.
+ auto __ut = utc_clock::from_sys(__p._M_sys_days) + __p._M_time
+ - *__offset;
+ __tp = chrono::time_point_cast<_Duration>(__ut);
+ }
return __is;
}
@@ -2788,9 +2807,24 @@ namespace __detail
basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
minutes* __offset = nullptr)
{
- utc_time<_Duration> __ut;
- if (chrono::from_stream(__is, __fmt, __ut, __abbrev, __offset))
- __tp = tai_clock::from_utc(__ut);
+ minutes __off{};
+ if (!__offset)
+ __offset = &__off;
+ using __format::_ChronoParts;
+ auto __need = _ChronoParts::_Year | _ChronoParts::_Month
+ | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
+ __detail::_Parser_t<_Duration> __p(__need);
+ if (__p(__is, __fmt, __abbrev, __offset))
+ {
+ if (__p._M_is_leap_second)
+ __is.setstate(ios_base::failbit);
+ else
+ {
+ auto __st = __p._M_sys_days + __p._M_time - *__offset;
+ auto __tt = tai_clock::from_utc(utc_clock::from_sys(__st));
+ __tp = chrono::time_point_cast<_Duration>(__tt);
+ }
+ }
return __is;
}
@@ -2811,9 +2845,24 @@ namespace __detail
basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
minutes* __offset = nullptr)
{
- utc_time<_Duration> __ut;
- if (chrono::from_stream(__is, __fmt, __ut, __abbrev, __offset))
- __tp = gps_clock::from_utc(__ut);
+ minutes __off{};
+ if (!__offset)
+ __offset = &__off;
+ using __format::_ChronoParts;
+ auto __need = _ChronoParts::_Year | _ChronoParts::_Month
+ | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
+ __detail::_Parser_t<_Duration> __p(__need);
+ if (__p(__is, __fmt, __abbrev, __offset))
+ {
+ if (__p._M_is_leap_second)
+ __is.setstate(ios_base::failbit);
+ else
+ {
+ auto __st = __p._M_sys_days + __p._M_time - *__offset;
+ auto __tt = gps_clock::from_utc(utc_clock::from_sys(__st));
+ __tp = chrono::time_point_cast<_Duration>(__tt);
+ }
+ }
return __is;
}
@@ -2836,7 +2885,7 @@ namespace __detail
{
sys_time<_Duration> __st;
if (chrono::from_stream(__is, __fmt, __st, __abbrev, __offset))
- __tp = file_clock::from_sys(__st);
+ __tp = chrono::time_point_cast<_Duration>(file_clock::from_sys(__st));
return __is;
}
@@ -4209,6 +4258,7 @@ namespace __detail
{
__ok = true;
__t += __s;
+ _M_is_leap_second = __s >= seconds(60);
}
if (__ok)
new file mode 100644
@@ -0,0 +1,53 @@
+// { dg-do run { target c++20 } }
+
+#include <chrono>
+#include <sstream>
+#include <testsuite_hooks.h>
+
+template<class Clock>
+void
+test_leap_second_parsing()
+{
+ std::chrono::time_point<Clock, std::chrono::milliseconds> tp, tp2;
+
+ std::istringstream ss("20161231-23:59:60.05");
+ ss >> std::chrono::parse("%Y%m%d-%T", tp);
+
+ if constexpr (std::is_same_v<Clock, std::chrono::local_t>)
+ VERIFY( ss ); // We allow parsing "23:59:60" as local_time.
+ else
+ {
+ if constexpr (std::is_same_v<Clock, std::chrono::utc_clock>)
+ {
+ // Entire input was consumed.
+ VERIFY( ss );
+ VERIFY( ss.eof() );
+ // The parsed value is the leap second inserted on Jan 1 2017.
+ VERIFY( std::chrono::get_leap_second_info(tp).is_leap_second );
+ }
+ else
+ VERIFY( !ss ); // Other clocks do not allow "HH:MM:60"
+
+ ss.clear();
+ ss.str("20161231-22:59:60.05 -0100"); // Same time at -1h offset.
+ ss >> std::chrono::parse("%Y%m%d-%T %z", tp2);
+
+ if constexpr (std::is_same_v<Clock, std::chrono::utc_clock>)
+ {
+ VERIFY( ss );
+ VERIFY( tp2 == tp );
+ }
+ else
+ VERIFY( !ss );
+ }
+}
+
+int main()
+{
+ test_leap_second_parsing<std::chrono::system_clock>();
+ test_leap_second_parsing<std::chrono::utc_clock>();
+ test_leap_second_parsing<std::chrono::tai_clock>();
+ test_leap_second_parsing<std::chrono::gps_clock>();
+ test_leap_second_parsing<std::chrono::file_clock>();
+ test_leap_second_parsing<std::chrono::local_t>();
+}
similarity index 100%
rename from libstdc++-v3/testsuite/std/time/parse.cc
rename to libstdc++-v3/testsuite/std/time/parse/parse.cc