'How to mix formatting in a single call using libfmt

I want to print the time center aligned. But I do not know how to mix arguments with fmt.

std::cout << fmt::format("|{0: ^80}|\n", "");
std::cout << fmt::format("|{0:%c}|\n", std::chrono::system_clock::now());

This prints:

|                                                                                |
|Fri May 13 09:24:05 2022|

I tried the followin but the program crasches:

std::cout << fmt::format("|{0:%c{:^ 80}}|\n", std::chrono::system_clock::now());

How can I print the date string center aligned?

A solution I found is to pass another format:

std::cout << fmt::format("|{0: ^80}|\n",
                 fmt::format("{0:%c}", std::chrono::system_clock::now()));

But this does not look good? I think there should be a way to do it in just one format?



Solution 1:[1]

From https://fmt.dev/latest/syntax.html :

chrono_format_spec ::=  [[fill]align][width]["." precision][chrono_specs]

Just:

fmt::format("|{0: ^80%c}|\n", ....

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1