// in the format-spec, e.g. "{:L%a}" is localized and locale-specific,
// but "{:L}" is only localized and "{:%a}" is only locale-specific.
unsigned _M_locale_specific : 1;
- // Indicates that we are handling duration.
- unsigned _M_time_only : 1;
+ // Indicates that we are handling time_point.
+ unsigned _M_time_point : 1;
// Indicates that duration should be treated as floating point.
unsigned _M_floating_point_rep : 1;
// Indicate that duration uses user-defined representation.
__allowed_mods = _Mod_O;
break;
case 'j':
- __needed = __spec._M_time_only ? _HoursMinutesSeconds
- : _DayOfYear;
+ __needed = __parts & _DayOfYear;
+ // If we do not know day-of-year then we must have a duration,
+ // which is to be formatted as decimal number of days.
+ if (__needed == _None)
+ __needed = _HoursMinutesSeconds;
break;
case 'm':
__needed = _Month;
{
switch (__conv)
{
+ case 'a':
+ case 'A':
+ case 'b':
+ case 'B':
case 'c':
+ case 'h':
+ case 'p':
case 'r':
case 'x':
case 'X':
return __out;
}
+ void
+ _M_check_ok(const _ChronoData<_CharT>& __t, _CharT __conv) const
+ {
+ // n.b. for time point all date parts are computed, so
+ // they are always ok.
+ if (_M_spec._M_time_point || _M_spec._M_debug)
+ return;
+
+ switch (__conv)
+ {
+ case 'a':
+ case 'A':
+ if (!__t._M_weekday.ok()) [[unlikely]]
+ __throw_format_error("format error: invalid weekday");
+ return;
+ case 'b':
+ case 'h':
+ case 'B':
+ if (!__t._M_month.ok()) [[unlikely]]
+ __throw_format_error("format error: invalid month");
+ return;
+ default:
+ return;
+ }
+ }
+
template<typename _OutIter, typename _FormatContext>
_OutIter
_M_format_to(const _ChronoData<_CharT>& __t, _OutIter __out,
do
{
_CharT __c = *__first++;
+ _M_check_ok(__t, __c);
+
if (__use_locale_fmt && _S_localized_spec(__c, __mod)) [[unlikely]]
__out = _M_locale_fmt(std::move(__out), __fc.locale(),
__tm, __c, __mod);
{
// %a Locale's abbreviated weekday name.
// %A Locale's full weekday name.
- if (!__wd.ok())
+ if (_M_spec._M_debug && !__wd.ok())
{
- if (!_M_spec._M_debug)
- __throw_format_error("format error: invalid weekday");
-
_CharT __buf[3];
__out = __format::__write(std::move(__out),
_S_str_d1(__buf, __wd.c_encoding()));
{
// %b Locale's abbreviated month name.
// %B Locale's full month name.
- if (!__m.ok())
+ if (_M_spec._M_debug && !__m.ok())
{
- if (!_M_spec._M_debug)
- __throw_format_error("format error: invalid month");
-
_CharT __buf[3];
__out = __format::__write(std::move(__out),
_S_str_d1(__buf, (unsigned)__m));
_OutIter
_M_j(const _ChronoData<_CharT>& __t, _OutIter __out) const
{
- if (_M_spec._M_time_only)
+ if (!_M_spec._M_needs(_ChronoParts::_DayOfYear))
{
// Decimal number of days, without padding.
auto __d = chrono::floor<chrono::days>(__t._M_hours).count();
{
if (__n < 100) [[likely]]
return _S_two_digits(__n);
- return _S_str_d3(__buf, __n);
+ return _S_str_d3(__buf, __n);
}
// Returns decimal representation of __n, padded to 3 digits.
using enum _ChronoParts;
_ChronoSpec<_CharT> __res{};
- __res._M_time_only = (__parts & _Date) == 0;
+ __res._M_time_point = (__parts & _DateTime) == _DateTime;
__res._M_floating_point_rep = chrono::treat_as_floating_point_v<_Rep>;
__res._M_custom_rep = !is_arithmetic_v<_Rep>;
__res._M_prec = chrono::hh_mm_ss<_Duration>::fractional_width;
VERIFY( not is_format_string_for("{:%OOy}", t) );
VERIFY( not is_format_string_for("{:%OEy}", t) );
VERIFY( not is_format_string_for("{:%EOy}", t) );
+
+ // weekday and month values for which ok() is false
+ VERIFY( not is_format_string_for("{:%a}", std::chrono::weekday(8)) );
+ VERIFY( not is_format_string_for("{:%A}", std::chrono::weekday(8)) );
+ VERIFY( not is_format_string_for("{:%b}", std::chrono::month(13)) );
+ VERIFY( not is_format_string_for("{:%h}", std::chrono::month(13)) );
+ VERIFY( not is_format_string_for("{:%B}", std::chrono::month(13)) );
}
template<typename I>
--- /dev/null
+// { dg-do run { target c++20 } }
+
+#include <chrono>
+#include <format>
+#include <locale>
+#include <testsuite_hooks.h>
+
+struct custom_time_put : std::time_put<char>
+{
+ iter_type
+ do_put(iter_type out, std::ios_base& io, char_type fill, const tm* t,
+ char format, char modifier) const override
+ {
+ using Base = std::time_put<char>;
+
+ switch (format) {
+ case 'a': case 'A': case 'b': case 'h': case 'B': case 'p':
+ *out++ = '[';
+ *out++ = format;
+ *out++ = ']';
+ }
+ return Base::do_put(out, io, fill, t, format, modifier);
+ }
+};
+
+int main()
+{
+ using namespace std::chrono;
+ std::locale loc(std::locale::classic(), new custom_time_put);
+#define test(t, fmt, exp) VERIFY( std::format(loc, fmt, t) == exp )
+ test(Monday, "{:L%a}", "[a]Mon");
+ test(Monday, "{:L%A}", "[A]Monday");
+ test(January, "{:L%b}", "[b]Jan");
+ test(January, "{:L%h}", "[h]Jan");
+ test(January, "{:L%B}", "[B]January");
+ test(1h, "{:L%p}", "[p]AM");
+}