DEKKER
DEKKER

Reputation: 911

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?

Upvotes: 2

Views: 364

Answers (1)

KamilCuk
KamilCuk

Reputation: 141190

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

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

Just:

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

Upvotes: 1

Related Questions