fmt icon indicating copy to clipboard operation
fmt copied to clipboard

Green Day Support

Open vitaut opened this issue 2 years ago • 7 comments

{fmt} doesn't support printing green day at the moment:

#include <fmt/chrono.h>
#include <fmt/color.h>

int main() {
  fmt::print(fg(fmt::color::green), "{}", std::chrono::day());
}

Error:

include/fmt/core.h:1594:63: error: implicit instantiation of undefined template 'fmt::detail::type_is_unformattable_for<const std::chrono::day, char>'
    type_is_unformattable_for<T, typename Context::char_type> _;
                                                              ^
...
include/fmt/core.h:1597:3: error: static assertion failed due to requirement 'formattable': Cannot format an argument. To make type T formattable provide a formatter<T> specialization: https://fmt.dev/latest/api.html#udt
  static_assert(
  ^
2 errors generated.

vitaut avatar Dec 17 '23 19:12 vitaut

Looking at this, not only day(), but also month() and year(). It seems they do not have a formatter.

Adding a custom formatter does fix it, but should we support it? If we want to, where is the best place to define them?

zivshek avatar Dec 20 '23 04:12 zivshek

The C++ standard mentions these formatters (https://eel.is/c++draft/time):

  template<class charT> struct formatter<chrono::day, charT>;
  template<class charT> struct formatter<chrono::month, charT>;
  template<class charT> struct formatter<chrono::year, charT>;

vitaut avatar Dec 20 '23 05:12 vitaut

in chrono.h, it defined the formatters for weekday, duration, time_point, etc., putting the defines for day, month, and year there works. I'll do some clean ups.

zivshek avatar Dec 20 '23 05:12 zivshek

The C++ standard mentions these formatters (https://eel.is/c++draft/time):

  template<class charT> struct formatter<chrono::day, charT>;
  template<class charT> struct formatter<chrono::month, charT>;
  template<class charT> struct formatter<chrono::year, charT>;

you're right, std should have those formatters, ~~but __cpp_lib_format is false, did some research, is it std::format not supported yet in MSVC?~~

It's interesting, as C++ 20 supports formatting all the chrono types, why you had to define the formatters for duration, weekday etc. in fmt in the first place? I think the issue is to make fmt make use the std::formatter's.

zivshek avatar Dec 20 '23 05:12 zivshek

why you had to define the formatters for duration, weekday etc. in fmt in the first place? I think the issue is to make fmt make use the std::formatter's.

Good question. std::chrono is usually wider available than std::format and considering that day/month/year formatters should be easy to implement I think we should just do it ourselves.

vitaut avatar Dec 20 '23 21:12 vitaut

Also year_month_day mentioned in #3772.

vitaut avatar Dec 21 '23 16:12 vitaut

There are a couple more of interest.

grafik

razaqq avatar Dec 21 '23 17:12 razaqq