forked from OSchip/llvm-project
[libc++][chrono] Implements formatter month.
Partially implements: - P1361 Integration of chrono with text formatting Reviewed By: ldionne, #libc Differential Revision: https://reviews.llvm.org/D134138
This commit is contained in:
parent
41b51007e6
commit
1522f190eb
|
@ -9,7 +9,7 @@ Section,Description,Dependencies,Assignee,Status,First released version
|
|||
`[time.syn] <https://wg21.link/time.syn>`_,"Formatter ``chrono::local_time<Duration>``",,Not assigned,,,
|
||||
`[time.syn] <https://wg21.link/time.syn>`_,"Formatter ``chrono::local-time-format-t<Duration>``",A ``<chrono>`` implementation,Not assigned,,,
|
||||
`[time.syn] <https://wg21.link/time.syn>`_,"Formatter ``chrono::day``",,Mark de Wever,|Complete|, Clang 16
|
||||
`[time.syn] <https://wg21.link/time.syn>`_,"Formatter ``chrono::month``",,Mark de Wever,|In Progress|,
|
||||
`[time.syn] <https://wg21.link/time.syn>`_,"Formatter ``chrono::month``",,Mark de Wever,|Complete|, Clang 16
|
||||
`[time.syn] <https://wg21.link/time.syn>`_,"Formatter ``chrono::year``",,Mark de Wever,|Complete|, Clang 16
|
||||
`[time.syn] <https://wg21.link/time.syn>`_,"Formatter ``chrono::weekday``",,Mark de Wever,|In Progress|,
|
||||
`[time.syn] <https://wg21.link/time.syn>`_,"Formatter ``chrono::weekday_indexed``",,Mark de Wever,|In Progress|,
|
||||
|
|
Can't render this file because it has a wrong number of fields in line 2.
|
|
@ -11,6 +11,7 @@
|
|||
#define _LIBCPP___CHRONO_CONVERT_TO_TM_H
|
||||
|
||||
#include <__chrono/day.h>
|
||||
#include <__chrono/month.h>
|
||||
#include <__chrono/year.h>
|
||||
#include <__concepts/same_as.h>
|
||||
#include <__config>
|
||||
|
@ -34,6 +35,8 @@ _LIBCPP_HIDE_FROM_ABI _Tm __convert_to_tm(const _ChronoCalendarTimePoint& __valu
|
|||
|
||||
if constexpr (same_as<_ChronoCalendarTimePoint, chrono::day>)
|
||||
__result.tm_mday = static_cast<unsigned>(__value);
|
||||
else if constexpr (same_as<_ChronoCalendarTimePoint, chrono::month>)
|
||||
__result.tm_mon = static_cast<unsigned>(__value) - 1;
|
||||
else if constexpr (same_as<_ChronoCalendarTimePoint, chrono::year>)
|
||||
__result.tm_year = static_cast<int>(__value) - 1900;
|
||||
else
|
||||
|
|
|
@ -12,11 +12,14 @@
|
|||
|
||||
#include <__chrono/convert_to_tm.h>
|
||||
#include <__chrono/day.h>
|
||||
#include <__chrono/month.h>
|
||||
#include <__chrono/parser_std_format_spec.h>
|
||||
#include <__chrono/statically_widen.h>
|
||||
#include <__chrono/year.h>
|
||||
#include <__concepts/same_as.h>
|
||||
#include <__config>
|
||||
#include <__format/concepts.h>
|
||||
#include <__format/format_error.h>
|
||||
#include <__format/format_functions.h>
|
||||
#include <__format/format_parse_context.h>
|
||||
#include <__format/formatter.h>
|
||||
|
@ -172,6 +175,18 @@ _LIBCPP_HIDE_FROM_ABI void __format_chrono_using_chrono_specs(
|
|||
}
|
||||
}
|
||||
|
||||
template <class _Tp>
|
||||
_LIBCPP_HIDE_FROM_ABI constexpr bool __month_name_ok(const _Tp& __value) {
|
||||
if constexpr (same_as<_Tp, chrono::day>)
|
||||
return true;
|
||||
else if constexpr (same_as<_Tp, chrono::month>)
|
||||
return __value.ok();
|
||||
else if constexpr (same_as<_Tp, chrono::year>)
|
||||
return true;
|
||||
else
|
||||
static_assert(sizeof(_Tp) == 0, "Add the missing type specialization");
|
||||
}
|
||||
|
||||
template <class _CharT, class _Tp>
|
||||
_LIBCPP_HIDE_FROM_ABI auto
|
||||
__format_chrono(const _Tp& __value,
|
||||
|
@ -191,8 +206,12 @@ __format_chrono(const _Tp& __value,
|
|||
|
||||
if (__chrono_specs.empty())
|
||||
__sstr << __value;
|
||||
else
|
||||
else {
|
||||
if (__specs.__chrono_.__month_name_ && !__formatter::__month_name_ok(__value))
|
||||
std::__throw_format_error("formatting a month name from an invalid month number");
|
||||
|
||||
__formatter::__format_chrono_using_chrono_specs(__value, __sstr, __chrono_specs);
|
||||
}
|
||||
|
||||
// TODO FMT Use the stringstream's view after P0408R7 has been implemented.
|
||||
basic_string<_CharT> __str = __sstr.str();
|
||||
|
@ -231,6 +250,18 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
template <__fmt_char_type _CharT>
|
||||
struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<chrono::month, _CharT>
|
||||
: public __formatter_chrono<_CharT> {
|
||||
public:
|
||||
using _Base = __formatter_chrono<_CharT>;
|
||||
|
||||
_LIBCPP_HIDE_FROM_ABI constexpr auto parse(basic_format_parse_context<_CharT>& __parse_ctx)
|
||||
-> decltype(__parse_ctx.begin()) {
|
||||
return _Base::__parse(__parse_ctx, __format_spec::__fields_chrono, __format_spec::__flags::__month);
|
||||
}
|
||||
};
|
||||
|
||||
template <__fmt_char_type _CharT>
|
||||
struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<chrono::year, _CharT>
|
||||
: public __formatter_chrono<_CharT> {
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
#define _LIBCPP___CHRONO_OSTREAM_H
|
||||
|
||||
#include <__chrono/day.h>
|
||||
#include <__chrono/month.h>
|
||||
#include <__chrono/statically_widen.h>
|
||||
#include <__chrono/year.h>
|
||||
#include <__config>
|
||||
|
@ -41,6 +42,15 @@ operator<<(basic_ostream<_CharT, _Traits>& __os, const day& __d) {
|
|||
: std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:02} is not a valid day"), static_cast<unsigned>(__d)));
|
||||
}
|
||||
|
||||
template <class _CharT, class _Traits>
|
||||
_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT basic_ostream<_CharT, _Traits>&
|
||||
operator<<(basic_ostream<_CharT, _Traits>& __os, const month& __m) {
|
||||
return __os << (__m.ok() ? std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%b}"), __m)
|
||||
: std::format(__os.getloc(),
|
||||
_LIBCPP_STATICALLY_WIDEN(_CharT, "{} is not a valid month"),
|
||||
static_cast<unsigned>(__m))); // TODO FMT Standard mandated locale isn't used.
|
||||
}
|
||||
|
||||
template <class _CharT, class _Traits>
|
||||
_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_FORMAT basic_ostream<_CharT, _Traits>&
|
||||
operator<<(basic_ostream<_CharT, _Traits>& __os, const year& __y) {
|
||||
|
|
|
@ -345,6 +345,9 @@ constexpr month operator+(const month& x, const months& y) noexcept;
|
|||
constexpr month operator+(const months& x, const month& y) noexcept;
|
||||
constexpr month operator-(const month& x, const months& y) noexcept;
|
||||
constexpr months operator-(const month& x, const month& y) noexcept;
|
||||
template<class charT, class traits>
|
||||
basic_ostream<charT, traits>&
|
||||
operator<<(basic_ostream<charT, traits>& os, const month& m);
|
||||
|
||||
// 25.8.5, class year // C++20
|
||||
class year;
|
||||
|
@ -623,6 +626,7 @@ bool operator>=(const time_zone& x, const time_zone& y) noexcept;
|
|||
|
||||
namespace std {
|
||||
template<class charT> struct formatter<chrono::day, charT>; // C++20
|
||||
template<class charT> struct formatter<chrono::month, charT>; // C++20
|
||||
template<class charT> struct formatter<chrono::year, charT>; // C++20
|
||||
} // namespace std
|
||||
|
||||
|
|
|
@ -142,7 +142,7 @@ void test_P1361() {
|
|||
assert_is_not_formattable<std::chrono::local_time<std::chrono::microseconds>, CharT>();
|
||||
|
||||
assert_is_formattable<std::chrono::day, CharT>();
|
||||
assert_is_not_formattable<std::chrono::month, CharT>();
|
||||
assert_is_formattable<std::chrono::month, CharT>();
|
||||
assert_is_formattable<std::chrono::year, CharT>();
|
||||
|
||||
assert_is_not_formattable<std::chrono::weekday, CharT>();
|
||||
|
|
|
@ -0,0 +1,167 @@
|
|||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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++03, c++11, c++14, c++17
|
||||
// UNSUPPORTED: no-localization
|
||||
// UNSUPPORTED: libcpp-has-no-incomplete-format
|
||||
|
||||
// TODO FMT Investigate Windows issues.
|
||||
// UNSUPPORTED: msvc, target={{.+}}-windows-gnu
|
||||
|
||||
// REQUIRES: locale.fr_FR.UTF-8
|
||||
// REQUIRES: locale.ja_JP.UTF-8
|
||||
|
||||
// <chrono>
|
||||
|
||||
// class month;
|
||||
|
||||
// template<class charT, class traits>
|
||||
// basic_ostream<charT, traits>&
|
||||
// operator<<(basic_ostream<charT, traits>& os, const month& month);
|
||||
|
||||
#include <chrono>
|
||||
#include <cassert>
|
||||
#include <sstream>
|
||||
|
||||
#include "make_string.h"
|
||||
#include "platform_support.h" // locale name macros
|
||||
#include "test_macros.h"
|
||||
|
||||
#define SV(S) MAKE_STRING_VIEW(CharT, S)
|
||||
|
||||
template <class CharT>
|
||||
static std::basic_string<CharT> stream_c_locale(std::chrono::month month) {
|
||||
std::basic_stringstream<CharT> sstr;
|
||||
sstr << month;
|
||||
return sstr.str();
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
static std::basic_string<CharT> stream_fr_FR_locale(std::chrono::month month) {
|
||||
std::basic_stringstream<CharT> sstr;
|
||||
const std::locale locale(LOCALE_fr_FR_UTF_8);
|
||||
sstr.imbue(locale);
|
||||
sstr << month;
|
||||
return sstr.str();
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
static std::basic_string<CharT> stream_ja_JP_locale(std::chrono::month month) {
|
||||
std::basic_stringstream<CharT> sstr;
|
||||
const std::locale locale(LOCALE_ja_JP_UTF_8);
|
||||
sstr.imbue(locale);
|
||||
sstr << month;
|
||||
return sstr.str();
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
static void test() {
|
||||
assert(stream_c_locale<CharT>(std::chrono::month{0}) == SV("0 is not a valid month"));
|
||||
assert(stream_c_locale<CharT>(std::chrono::month{1}) == SV("Jan"));
|
||||
assert(stream_c_locale<CharT>(std::chrono::month{2}) == SV("Feb"));
|
||||
assert(stream_c_locale<CharT>(std::chrono::month{3}) == SV("Mar"));
|
||||
assert(stream_c_locale<CharT>(std::chrono::month{4}) == SV("Apr"));
|
||||
assert(stream_c_locale<CharT>(std::chrono::month{5}) == SV("May"));
|
||||
assert(stream_c_locale<CharT>(std::chrono::month{6}) == SV("Jun"));
|
||||
assert(stream_c_locale<CharT>(std::chrono::month{7}) == SV("Jul"));
|
||||
assert(stream_c_locale<CharT>(std::chrono::month{8}) == SV("Aug"));
|
||||
assert(stream_c_locale<CharT>(std::chrono::month{9}) == SV("Sep"));
|
||||
assert(stream_c_locale<CharT>(std::chrono::month{10}) == SV("Oct"));
|
||||
assert(stream_c_locale<CharT>(std::chrono::month{11}) == SV("Nov"));
|
||||
assert(stream_c_locale<CharT>(std::chrono::month{12}) == SV("Dec"));
|
||||
assert(stream_c_locale<CharT>(std::chrono::month{13}) == SV("13 is not a valid month"));
|
||||
assert(stream_c_locale<CharT>(std::chrono::month{255}) == SV("255 is not a valid month"));
|
||||
|
||||
assert(stream_fr_FR_locale<CharT>(std::chrono::month{0}) == SV("0 is not a valid month"));
|
||||
#if defined(__APPLE__)
|
||||
assert(stream_fr_FR_locale<CharT>(std::chrono::month{1}) == SV("jan"));
|
||||
assert(stream_fr_FR_locale<CharT>(std::chrono::month{2}) == SV("fév"));
|
||||
assert(stream_fr_FR_locale<CharT>(std::chrono::month{3}) == SV("mar"));
|
||||
assert(stream_fr_FR_locale<CharT>(std::chrono::month{4}) == SV("avr"));
|
||||
assert(stream_fr_FR_locale<CharT>(std::chrono::month{5}) == SV("mai"));
|
||||
assert(stream_fr_FR_locale<CharT>(std::chrono::month{6}) == SV("jui"));
|
||||
assert(stream_fr_FR_locale<CharT>(std::chrono::month{7}) == SV("jul"));
|
||||
assert(stream_fr_FR_locale<CharT>(std::chrono::month{8}) == SV("aoû"));
|
||||
assert(stream_fr_FR_locale<CharT>(std::chrono::month{9}) == SV("sep"));
|
||||
assert(stream_fr_FR_locale<CharT>(std::chrono::month{10}) == SV("oct"));
|
||||
assert(stream_fr_FR_locale<CharT>(std::chrono::month{11}) == SV("nov"));
|
||||
assert(stream_fr_FR_locale<CharT>(std::chrono::month{12}) == SV("déc"));
|
||||
#else // defined(__APPLE__)
|
||||
assert(stream_fr_FR_locale<CharT>(std::chrono::month{1}) == SV("janv."));
|
||||
assert(stream_fr_FR_locale<CharT>(std::chrono::month{2}) == SV("févr."));
|
||||
assert(stream_fr_FR_locale<CharT>(std::chrono::month{3}) == SV("mars"));
|
||||
# if defined(_WIN32) || defined(_AIX)
|
||||
assert(stream_fr_FR_locale<CharT>(std::chrono::month{4}) == SV("avr."));
|
||||
# else
|
||||
assert(stream_fr_FR_locale<CharT>(std::chrono::month{4}) == SV("avril"));
|
||||
# endif
|
||||
assert(stream_fr_FR_locale<CharT>(std::chrono::month{5}) == SV("mai"));
|
||||
assert(stream_fr_FR_locale<CharT>(std::chrono::month{6}) == SV("juin"));
|
||||
assert(stream_fr_FR_locale<CharT>(std::chrono::month{7}) == SV("juil."));
|
||||
assert(stream_fr_FR_locale<CharT>(std::chrono::month{8}) == SV("août"));
|
||||
assert(stream_fr_FR_locale<CharT>(std::chrono::month{9}) == SV("sept."));
|
||||
assert(stream_fr_FR_locale<CharT>(std::chrono::month{10}) == SV("oct."));
|
||||
assert(stream_fr_FR_locale<CharT>(std::chrono::month{11}) == SV("nov."));
|
||||
assert(stream_fr_FR_locale<CharT>(std::chrono::month{12}) == SV("déc."));
|
||||
#endif // defined(__APPLE__)
|
||||
assert(stream_fr_FR_locale<CharT>(std::chrono::month{13}) == SV("13 is not a valid month"));
|
||||
assert(stream_fr_FR_locale<CharT>(std::chrono::month{255}) == SV("255 is not a valid month"));
|
||||
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{0}) == SV("0 is not a valid month"));
|
||||
#if defined(__APPLE__)
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{1}) == SV(" 1"));
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{2}) == SV(" 2"));
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{3}) == SV(" 3"));
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{4}) == SV(" 4"));
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{5}) == SV(" 5"));
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{6}) == SV(" 6"));
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{7}) == SV(" 7"));
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{8}) == SV(" 8"));
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{9}) == SV(" 9"));
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{10}) == SV("10"));
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{11}) == SV("11"));
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{12}) == SV("12"));
|
||||
#else // defined(__APPLE__)
|
||||
# if defined(_WIN32) || defined(_AIX)
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{1}) == SV("1月"));
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{2}) == SV("2月"));
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{3}) == SV("3月"));
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{4}) == SV("4月"));
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{5}) == SV("5月"));
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{6}) == SV("6月"));
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{7}) == SV("7月"));
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{8}) == SV("8月"));
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{9}) == SV("9月"));
|
||||
# else // defined(_WIN32) || defined(_AIX)
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{1}) == SV(" 1月"));
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{2}) == SV(" 2月"));
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{3}) == SV(" 3月"));
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{4}) == SV(" 4月"));
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{5}) == SV(" 5月"));
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{6}) == SV(" 6月"));
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{7}) == SV(" 7月"));
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{8}) == SV(" 8月"));
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{9}) == SV(" 9月"));
|
||||
# endif // defined(_WIN32) || defined(_AIX)
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{10}) == SV("10月"));
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{11}) == SV("11月"));
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{12}) == SV("12月"));
|
||||
#endif // defined(__APPLE__)
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{13}) == SV("13 is not a valid month"));
|
||||
assert(stream_ja_JP_locale<CharT>(std::chrono::month{255}) == SV("255 is not a valid month"));
|
||||
}
|
||||
|
||||
int main(int, char**) {
|
||||
test<char>();
|
||||
|
||||
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
|
||||
test<wchar_t>();
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,196 @@
|
|||
//===----------------------------------------------------------------------===//
|
||||
// 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++03, c++11, c++14, c++17
|
||||
// UNSUPPORTED: no-localization
|
||||
// UNSUPPORTED: libcpp-has-no-incomplete-format
|
||||
|
||||
// REQUIRES: locale.fr_FR.UTF-8
|
||||
// REQUIRES: locale.ja_JP.UTF-8
|
||||
|
||||
// <chrono>
|
||||
|
||||
// template<class charT> struct formatter<chrono::month, charT>;
|
||||
|
||||
#include <chrono>
|
||||
#include <format>
|
||||
|
||||
#include <cassert>
|
||||
#include <concepts>
|
||||
#include <locale>
|
||||
#include <iostream>
|
||||
#include <type_traits>
|
||||
|
||||
#include "formatter_tests.h"
|
||||
#include "make_string.h"
|
||||
#include "platform_support.h" // locale name macros
|
||||
#include "string_literal.h"
|
||||
#include "test_macros.h"
|
||||
|
||||
template <class CharT>
|
||||
static void test_no_chrono_specs() {
|
||||
// Valid month
|
||||
check(SV("Jan"), SV("{}"), std::chrono::month{1});
|
||||
check(SV("*Jan*"), SV("{:*^5}"), std::chrono::month{1});
|
||||
check(SV("*Jan"), SV("{:*>4}"), std::chrono::month{1});
|
||||
|
||||
// Invalid month
|
||||
check(SV("0 is not a valid month"), SV("{}"), std::chrono::month{0});
|
||||
check(SV("*0 is not a valid month*"), SV("{:*^24}"), std::chrono::month{0});
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
static void test_valid_values() {
|
||||
// Test that %b, %h, and %B throw an exception.
|
||||
check_exception("formatting a month name from an invalid month number", SV("{:%b}"), std::chrono::month{200});
|
||||
check_exception("formatting a month name from an invalid month number", SV("{:%b}"), std::chrono::month{13});
|
||||
check_exception("formatting a month name from an invalid month number", SV("{:%b}"), std::chrono::month{255});
|
||||
|
||||
check_exception("formatting a month name from an invalid month number", SV("{:%h}"), std::chrono::month{0});
|
||||
check_exception("formatting a month name from an invalid month number", SV("{:%h}"), std::chrono::month{13});
|
||||
check_exception("formatting a month name from an invalid month number", SV("{:%h}"), std::chrono::month{255});
|
||||
|
||||
check_exception("formatting a month name from an invalid month number", SV("{:%B}"), std::chrono::month{0});
|
||||
check_exception("formatting a month name from an invalid month number", SV("{:%B}"), std::chrono::month{13});
|
||||
check_exception("formatting a month name from an invalid month number", SV("{:%B}"), std::chrono::month{255});
|
||||
|
||||
constexpr std::basic_string_view<CharT> fmt = SV("{:%%b='%b'%t%%B='%B'%t%%h='%h'%t%%m='%m'%t%%Om='%Om'%n}");
|
||||
constexpr std::basic_string_view<CharT> lfmt = SV("{:L%%b='%b'%t%%B='%B'%t%%h='%h'%t%%m='%m'%t%%Om='%Om'%n}");
|
||||
|
||||
const std::locale loc(LOCALE_ja_JP_UTF_8);
|
||||
std::locale::global(std::locale(LOCALE_fr_FR_UTF_8));
|
||||
|
||||
// Non localized output using C-locale
|
||||
check(SV("%b='Jan'\t%B='January'\t%h='Jan'\t%m='01'\t%Om='01'\n"), fmt, std::chrono::January);
|
||||
check(SV("%b='Feb'\t%B='February'\t%h='Feb'\t%m='02'\t%Om='02'\n"), fmt, std::chrono::February);
|
||||
check(SV("%b='Mar'\t%B='March'\t%h='Mar'\t%m='03'\t%Om='03'\n"), fmt, std::chrono::March);
|
||||
check(SV("%b='Apr'\t%B='April'\t%h='Apr'\t%m='04'\t%Om='04'\n"), fmt, std::chrono::April);
|
||||
check(SV("%b='May'\t%B='May'\t%h='May'\t%m='05'\t%Om='05'\n"), fmt, std::chrono::May);
|
||||
check(SV("%b='Jun'\t%B='June'\t%h='Jun'\t%m='06'\t%Om='06'\n"), fmt, std::chrono::June);
|
||||
check(SV("%b='Jul'\t%B='July'\t%h='Jul'\t%m='07'\t%Om='07'\n"), fmt, std::chrono::July);
|
||||
check(SV("%b='Aug'\t%B='August'\t%h='Aug'\t%m='08'\t%Om='08'\n"), fmt, std::chrono::August);
|
||||
check(SV("%b='Sep'\t%B='September'\t%h='Sep'\t%m='09'\t%Om='09'\n"), fmt, std::chrono::September);
|
||||
check(SV("%b='Oct'\t%B='October'\t%h='Oct'\t%m='10'\t%Om='10'\n"), fmt, std::chrono::October);
|
||||
check(SV("%b='Nov'\t%B='November'\t%h='Nov'\t%m='11'\t%Om='11'\n"), fmt, std::chrono::November);
|
||||
check(SV("%b='Dec'\t%B='December'\t%h='Dec'\t%m='12'\t%Om='12'\n"), fmt, std::chrono::December);
|
||||
|
||||
// Use the global locale (fr_FR)
|
||||
#if defined(__APPLE__)
|
||||
check(SV("%b='jan'\t%B='janvier'\t%h='jan'\t%m='01'\t%Om='01'\n"), lfmt, std::chrono::January);
|
||||
check(SV("%b='fév'\t%B='février'\t%h='fév'\t%m='02'\t%Om='02'\n"), lfmt, std::chrono::February);
|
||||
check(SV("%b='mar'\t%B='mars'\t%h='mar'\t%m='03'\t%Om='03'\n"), lfmt, std::chrono::March);
|
||||
check(SV("%b='avr'\t%B='avril'\t%h='avr'\t%m='04'\t%Om='04'\n"), lfmt, std::chrono::April);
|
||||
check(SV("%b='mai'\t%B='mai'\t%h='mai'\t%m='05'\t%Om='05'\n"), lfmt, std::chrono::May);
|
||||
check(SV("%b='jui'\t%B='juin'\t%h='jui'\t%m='06'\t%Om='06'\n"), lfmt, std::chrono::June);
|
||||
check(SV("%b='jul'\t%B='juillet'\t%h='jul'\t%m='07'\t%Om='07'\n"), lfmt, std::chrono::July);
|
||||
check(SV("%b='aoû'\t%B='août'\t%h='aoû'\t%m='08'\t%Om='08'\n"), lfmt, std::chrono::August);
|
||||
check(SV("%b='sep'\t%B='septembre'\t%h='sep'\t%m='09'\t%Om='09'\n"), lfmt, std::chrono::September);
|
||||
check(SV("%b='oct'\t%B='octobre'\t%h='oct'\t%m='10'\t%Om='10'\n"), lfmt, std::chrono::October);
|
||||
check(SV("%b='nov'\t%B='novembre'\t%h='nov'\t%m='11'\t%Om='11'\n"), lfmt, std::chrono::November);
|
||||
check(SV("%b='déc'\t%B='décembre'\t%h='déc'\t%m='12'\t%Om='12'\n"), lfmt, std::chrono::December);
|
||||
#else // defined(__APPLE__)
|
||||
check(SV("%b='janv.'\t%B='janvier'\t%h='janv.'\t%m='01'\t%Om='01'\n"), lfmt, std::chrono::January);
|
||||
check(SV("%b='févr.'\t%B='février'\t%h='févr.'\t%m='02'\t%Om='02'\n"), lfmt, std::chrono::February);
|
||||
check(SV("%b='mars'\t%B='mars'\t%h='mars'\t%m='03'\t%Om='03'\n"), lfmt, std::chrono::March);
|
||||
# if defined(_WIN32) || defined(_AIX)
|
||||
check(SV("%b='avr.'\t%B='avril'\t%h='avr.'\t%m='04'\t%Om='04'\n"), lfmt, std::chrono::April);
|
||||
# else
|
||||
check(SV("%b='avril'\t%B='avril'\t%h='avril'\t%m='04'\t%Om='04'\n"), lfmt, std::chrono::April);
|
||||
# endif
|
||||
check(SV("%b='mai'\t%B='mai'\t%h='mai'\t%m='05'\t%Om='05'\n"), lfmt, std::chrono::May);
|
||||
check(SV("%b='juin'\t%B='juin'\t%h='juin'\t%m='06'\t%Om='06'\n"), lfmt, std::chrono::June);
|
||||
check(SV("%b='juil.'\t%B='juillet'\t%h='juil.'\t%m='07'\t%Om='07'\n"), lfmt, std::chrono::July);
|
||||
check(SV("%b='août'\t%B='août'\t%h='août'\t%m='08'\t%Om='08'\n"), lfmt, std::chrono::August);
|
||||
check(SV("%b='sept.'\t%B='septembre'\t%h='sept.'\t%m='09'\t%Om='09'\n"), lfmt, std::chrono::September);
|
||||
check(SV("%b='oct.'\t%B='octobre'\t%h='oct.'\t%m='10'\t%Om='10'\n"), lfmt, std::chrono::October);
|
||||
check(SV("%b='nov.'\t%B='novembre'\t%h='nov.'\t%m='11'\t%Om='11'\n"), lfmt, std::chrono::November);
|
||||
check(SV("%b='déc.'\t%B='décembre'\t%h='déc.'\t%m='12'\t%Om='12'\n"), lfmt, std::chrono::December);
|
||||
#endif // defined(__APPLE__)
|
||||
|
||||
// Use supplied locale (ja_JP)
|
||||
#if defined(_WIN32)
|
||||
check(loc, SV("%b='1'\t%B='1月'\t%h='1'\t%m='01'\t%Om='01'\n"), lfmt, std::chrono::January);
|
||||
check(loc, SV("%b='2'\t%B='2月'\t%h='2'\t%m='02'\t%Om='02'\n"), lfmt, std::chrono::February);
|
||||
check(loc, SV("%b='3'\t%B='3月'\t%h='3'\t%m='03'\t%Om='03'\n"), lfmt, std::chrono::March);
|
||||
check(loc, SV("%b='4'\t%B='4月'\t%h='4'\t%m='04'\t%Om='04'\n"), lfmt, std::chrono::April);
|
||||
check(loc, SV("%b='5'\t%B='5月'\t%h='5'\t%m='05'\t%Om='05'\n"), lfmt, std::chrono::May);
|
||||
check(loc, SV("%b='6'\t%B='6月'\t%h='6'\t%m='06'\t%Om='06'\n"), lfmt, std::chrono::June);
|
||||
check(loc, SV("%b='7'\t%B='7月'\t%h='7'\t%m='07'\t%Om='07'\n"), lfmt, std::chrono::July);
|
||||
check(loc, SV("%b='8'\t%B='8月'\t%h='8'\t%m='08'\t%Om='08'\n"), lfmt, std::chrono::August);
|
||||
check(loc, SV("%b='9'\t%B='9月'\t%h='9'\t%m='09'\t%Om='09'\n"), lfmt, std::chrono::September);
|
||||
check(loc, SV("%b='10'\t%B='10月'\t%h='10'\t%m='10'\t%Om='10'\n"), lfmt, std::chrono::October);
|
||||
check(loc, SV("%b='11'\t%B='11月'\t%h='11'\t%m='11'\t%Om='11'\n"), lfmt, std::chrono::November);
|
||||
check(loc, SV("%b='12'\t%B='12月'\t%h='12'\t%m='12'\t%Om='12'\n"), lfmt, std::chrono::December);
|
||||
#elif defined(_AIX) // defined(_WIN32)
|
||||
check(loc, SV("%b='1月'\t%B='1月'\t%h='1月'\t%m='01'\t%Om='01'\n"), lfmt, std::chrono::January);
|
||||
check(loc, SV("%b='2月'\t%B='2月'\t%h='2月'\t%m='02'\t%Om='02'\n"), lfmt, std::chrono::February);
|
||||
check(loc, SV("%b='3月'\t%B='3月'\t%h='3月'\t%m='03'\t%Om='03'\n"), lfmt, std::chrono::March);
|
||||
check(loc, SV("%b='4月'\t%B='4月'\t%h='4月'\t%m='04'\t%Om='04'\n"), lfmt, std::chrono::April);
|
||||
check(loc, SV("%b='5月'\t%B='5月'\t%h='5月'\t%m='05'\t%Om='05'\n"), lfmt, std::chrono::May);
|
||||
check(loc, SV("%b='6月'\t%B='6月'\t%h='6月'\t%m='06'\t%Om='06'\n"), lfmt, std::chrono::June);
|
||||
check(loc, SV("%b='7月'\t%B='7月'\t%h='7月'\t%m='07'\t%Om='07'\n"), lfmt, std::chrono::July);
|
||||
check(loc, SV("%b='8月'\t%B='8月'\t%h='8月'\t%m='08'\t%Om='08'\n"), lfmt, std::chrono::August);
|
||||
check(loc, SV("%b='9月'\t%B='9月'\t%h='9月'\t%m='09'\t%Om='09'\n"), lfmt, std::chrono::September);
|
||||
check(loc, SV("%b='10月'\t%B='10月'\t%h='10月'\t%m='10'\t%Om='10'\n"), lfmt, std::chrono::October);
|
||||
check(loc, SV("%b='11月'\t%B='11月'\t%h='11月'\t%m='11'\t%Om='11'\n"), lfmt, std::chrono::November);
|
||||
check(loc, SV("%b='12月'\t%B='12月'\t%h='12月'\t%m='12'\t%Om='12'\n"), lfmt, std::chrono::December);
|
||||
#elif defined(__APPLE__) // defined(_WIN32)
|
||||
check(loc, SV("%b=' 1'\t%B='1月'\t%h=' 1'\t%m='01'\t%Om='01'\n"), lfmt, std::chrono::January);
|
||||
check(loc, SV("%b=' 2'\t%B='2月'\t%h=' 2'\t%m='02'\t%Om='02'\n"), lfmt, std::chrono::February);
|
||||
check(loc, SV("%b=' 3'\t%B='3月'\t%h=' 3'\t%m='03'\t%Om='03'\n"), lfmt, std::chrono::March);
|
||||
check(loc, SV("%b=' 4'\t%B='4月'\t%h=' 4'\t%m='04'\t%Om='04'\n"), lfmt, std::chrono::April);
|
||||
check(loc, SV("%b=' 5'\t%B='5月'\t%h=' 5'\t%m='05'\t%Om='05'\n"), lfmt, std::chrono::May);
|
||||
check(loc, SV("%b=' 6'\t%B='6月'\t%h=' 6'\t%m='06'\t%Om='06'\n"), lfmt, std::chrono::June);
|
||||
check(loc, SV("%b=' 7'\t%B='7月'\t%h=' 7'\t%m='07'\t%Om='07'\n"), lfmt, std::chrono::July);
|
||||
check(loc, SV("%b=' 8'\t%B='8月'\t%h=' 8'\t%m='08'\t%Om='08'\n"), lfmt, std::chrono::August);
|
||||
check(loc, SV("%b=' 9'\t%B='9月'\t%h=' 9'\t%m='09'\t%Om='09'\n"), lfmt, std::chrono::September);
|
||||
check(loc, SV("%b='10'\t%B='10月'\t%h='10'\t%m='10'\t%Om='10'\n"), lfmt, std::chrono::October);
|
||||
check(loc, SV("%b='11'\t%B='11月'\t%h='11'\t%m='11'\t%Om='11'\n"), lfmt, std::chrono::November);
|
||||
check(loc, SV("%b='12'\t%B='12月'\t%h='12'\t%m='12'\t%Om='12'\n"), lfmt, std::chrono::December);
|
||||
#else // defined(_WIN32)
|
||||
check(loc, SV("%b=' 1月'\t%B='1月'\t%h=' 1月'\t%m='01'\t%Om='一'\n"), lfmt, std::chrono::January);
|
||||
check(loc, SV("%b=' 2月'\t%B='2月'\t%h=' 2月'\t%m='02'\t%Om='二'\n"), lfmt, std::chrono::February);
|
||||
check(loc, SV("%b=' 3月'\t%B='3月'\t%h=' 3月'\t%m='03'\t%Om='三'\n"), lfmt, std::chrono::March);
|
||||
check(loc, SV("%b=' 4月'\t%B='4月'\t%h=' 4月'\t%m='04'\t%Om='四'\n"), lfmt, std::chrono::April);
|
||||
check(loc, SV("%b=' 5月'\t%B='5月'\t%h=' 5月'\t%m='05'\t%Om='五'\n"), lfmt, std::chrono::May);
|
||||
check(loc, SV("%b=' 6月'\t%B='6月'\t%h=' 6月'\t%m='06'\t%Om='六'\n"), lfmt, std::chrono::June);
|
||||
check(loc, SV("%b=' 7月'\t%B='7月'\t%h=' 7月'\t%m='07'\t%Om='七'\n"), lfmt, std::chrono::July);
|
||||
check(loc, SV("%b=' 8月'\t%B='8月'\t%h=' 8月'\t%m='08'\t%Om='八'\n"), lfmt, std::chrono::August);
|
||||
check(loc, SV("%b=' 9月'\t%B='9月'\t%h=' 9月'\t%m='09'\t%Om='九'\n"), lfmt, std::chrono::September);
|
||||
check(loc, SV("%b='10月'\t%B='10月'\t%h='10月'\t%m='10'\t%Om='十'\n"), lfmt, std::chrono::October);
|
||||
check(loc, SV("%b='11月'\t%B='11月'\t%h='11月'\t%m='11'\t%Om='十一'\n"), lfmt, std::chrono::November);
|
||||
check(loc, SV("%b='12月'\t%B='12月'\t%h='12月'\t%m='12'\t%Om='十二'\n"), lfmt, std::chrono::December);
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
std::locale::global(std::locale::classic());
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
static void test() {
|
||||
test_no_chrono_specs<CharT>();
|
||||
test_valid_values<CharT>();
|
||||
check_invalid_types<CharT>({SV("b"), SV("B"), SV("h"), SV("m"), SV("Om")}, std::chrono::January);
|
||||
|
||||
check_exception("Expected '%' or '}' in the chrono format-string", SV("{:A"), std::chrono::January);
|
||||
check_exception("The chrono-specs contains a '{'", SV("{:%%{"), std::chrono::January);
|
||||
check_exception("End of input while parsing the modifier chrono conversion-spec", SV("{:%"), std::chrono::January);
|
||||
check_exception("End of input while parsing the modifier E", SV("{:%E"), std::chrono::January);
|
||||
check_exception("End of input while parsing the modifier O", SV("{:%O"), std::chrono::January);
|
||||
|
||||
// Precision not allowed
|
||||
check_exception("Expected '%' or '}' in the chrono format-string", SV("{:.3}"), std::chrono::January);
|
||||
}
|
||||
|
||||
int main(int, char**) {
|
||||
test<char>();
|
||||
|
||||
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
|
||||
test<wchar_t>();
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -184,7 +184,7 @@ void test_P1361() {
|
|||
assert_formatter_is_disabled<std::chrono::local_time<std::chrono::microseconds>, CharT>();
|
||||
|
||||
assert_formatter_is_enabled<std::chrono::day, CharT>();
|
||||
assert_formatter_is_disabled<std::chrono::month, CharT>();
|
||||
assert_formatter_is_enabled<std::chrono::month, CharT>();
|
||||
assert_formatter_is_enabled<std::chrono::year, CharT>();
|
||||
|
||||
assert_formatter_is_disabled<std::chrono::weekday, CharT>();
|
||||
|
|
|
@ -193,6 +193,7 @@ check-generated-output)
|
|||
--exclude 'formatter.*.pass.cpp' \
|
||||
--exclude 'grep.pass.cpp' \
|
||||
--exclude 'locale-specific_form.pass.cpp' \
|
||||
--exclude 'ostream.pass.cpp' \
|
||||
--exclude 'std_format_spec_string_unicode.bench.cpp' \
|
||||
|| false
|
||||
|
||||
|
|
Loading…
Reference in New Issue