xtd 0.2.0
Loading...
Searching...
No Matches
format_system_clock.cpp

Show how to use format xtd::format class with std::chrono::system_clock class.

#include <xtd/xtd>
enum class cap {title, middle, end};
auto make_time(uint32 year, uint32 month, uint32 day, uint32 hour, uint32 minute, uint32 second) {
auto date = std::tm {};
date.tm_year = year - 1900;
date.tm_mon = month - 1;
date.tm_mday = day;
date.tm_hour = hour;
date.tm_min = minute;
date.tm_sec = second;
return std::mktime(&date);
}
auto make_time(uint32 year, uint32 month, uint32 day) {
return make_time(year, month, day, 0, 0, 0);
}
auto make_time() {
return std::tm {};
}
template<typename clock_t>
auto print_system_clock(const string& text, const std::chrono::time_point<clock_t>& value, cap c) {
if (c == cap::title)
console::out
<< "┌───────────────────────────────────────┬─────────────────────┬──────────────────────────────────────────┐" << environment::new_line
<< "│ system_clock │ format │ representation │" << environment::new_line
<< "├───────────────────────────────────────┼─────────────────────┼──────────────────────────────────────────┤" << environment::new_line;
console::out << "│ " << text.pad_right(37) << " │ {} │ " << format("{}", value).pad_right(40) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(37) << " │ {:d} │ " << format("{:d}", value).pad_right(40) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(37) << " │ {:D} │ " << format("{:D}", value).pad_right(40) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(37) << " │ {:f} │ " << format("{:f}", value).pad_right(40) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(37) << " │ {:F} │ " << format("{:F}", value).pad_right(40) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(37) << " │ {:g} │ " << format("{:g}", value).pad_right(40) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(37) << " │ {:G} │ " << format("{:G}", value).pad_right(40) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(37) << " │ {:m} │ " << format("{:m}", value).pad_right(40) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(37) << " │ {:M} │ " << format("{:M}", value).pad_right(40) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(37) << " │ {:o} │ " << format("{:o}", value).pad_right(40) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(37) << " │ {:O} │ " << format("{:O}", value).pad_right(40) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(37) << " │ {:r} │ " << format("{:r}", value).pad_right(40) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(37) << " │ {:R} │ " << format("{:R}", value).pad_right(40) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(37) << " │ {:s} │ " << format("{:s}", value).pad_right(40) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(37) << " │ {:t} │ " << format("{:t}", value).pad_right(40) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(37) << " │ {:T} │ " << format("{:T}", value).pad_right(40) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(37) << " │ {:u} │ " << format("{:u}", value).pad_right(40) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(37) << " │ {:U} │ " << format("{:U}", value).pad_right(40) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(37) << " │ {:y} │ " << format("{:y}", value).pad_right(40) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(37) << " │ {:Y} │ " << format("{:Y}", value).pad_right(40) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(37) << " │ {:h:mm:ss.ff t} │ " << format("{:h:mm:ss.ff t}", value).pad_right(40) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(37) << " │ {:d MMM yyyy} │ " << format("{:d MMM yyyy}", value).pad_right(40) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(37) << " │ {:HH:mm:ss.f} │ " << format("{:HH:mm:ss.f}", value).pad_right(40) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(37) << " │ {:dd MMM HH:mm:ss} │ " << format("{:dd MMM HH:mm:ss}", value).pad_right(40) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(37) << " │ {:\\Mon\\t\\h\\: M} │ " << format("{:\\Mon\\t\\h\\: M}", value).pad_right(40) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(37) << " │ {:HH:mm:ss.ffffzzz} │ " << format("{:HH:mm:ss.ffffzzz}", value).pad_right(40) << " │" << environment::new_line;
if (c != cap::end)
console::out << "├───────────────────────────────────────┼─────────────────────┼──────────────────────────────────────────┤" << environment::new_line;
else
console::out << "└───────────────────────────────────────┴─────────────────────┴──────────────────────────────────────────┘" << environment::new_line;
}
auto main() -> int {
print_system_clock("0", std::chrono::system_clock::from_time_t(0), cap::title);
print_system_clock("12345678912", std::chrono::system_clock::from_time_t(12345678912), cap::middle);
print_system_clock("make_time(1971, 1, 5)", std::chrono::system_clock::from_time_t(make_time(1971, 1, 5)), cap::middle);
print_system_clock("make_time(1971, 1, 5, 21, 30, 3)", std::chrono::system_clock::from_time_t(make_time(1971, 1, 5, 21, 30, 3)), cap::middle);
print_system_clock("now", std::chrono::system_clock::now(), cap::end);
}
// This code can be produce the following output :
//
// ┌───────────────────────────────────────┬─────────────────────┬──────────────────────────────────────────┐
// │ system_clock │ format │ representation │
// ├───────────────────────────────────────┼─────────────────────┼──────────────────────────────────────────┤
// │ 0 │ {} │ 1/1/1970 01:00:00 │
// │ 0 │ {:d} │ 1/1/1970 │
// │ 0 │ {:D} │ Thursday, January 1, 1970 │
// │ 0 │ {:f} │ Thursday, January 1, 1970 01:00 │
// │ 0 │ {:F} │ Thursday, January 1, 1970 01:00:00 │
// │ 0 │ {:g} │ 1/1/1970 01:00 │
// │ 0 │ {:G} │ 1/1/1970 01:00:00 │
// │ 0 │ {:m} │ January 1 │
// │ 0 │ {:M} │ January 1 │
// │ 0 │ {:o} │ 1970-01-01T01:00:00.0000000Z │
// │ 0 │ {:O} │ 1970-01-01T01:00:00.0000000Z │
// │ 0 │ {:r} │ Thu, 01 Jan 1970 01:00:00 GMT │
// │ 0 │ {:R} │ Thu, 01 Jan 1970 01:00:00 GMT │
// │ 0 │ {:s} │ 1970-01-01T01:00:00 │
// │ 0 │ {:t} │ 01:00 │
// │ 0 │ {:T} │ 01:00:00 │
// │ 0 │ {:u} │ 1970-01-01 01:00:00Z │
// │ 0 │ {:U} │ Thursday, January 1, 1970 01:00:00 │
// │ 0 │ {:y} │ January 1970 │
// │ 0 │ {:Y} │ January 1970 │
// │ 0 │ {:h:mm:ss.ff t} │ 1:00:00.00 A │
// │ 0 │ {:d MMM yyyy} │ 1 Jan 1970 │
// │ 0 │ {:HH:mm:ss.f} │ 01:00:00.0 │
// │ 0 │ {:dd MMM HH:mm:ss} │ 01 Jan 01:00:00 │
// │ 0 │ {:\Mon\t\h\: M} │ Month: 1 │
// │ 0 │ {:HH:mm:ss.ffffzzz} │ 01:00:00.0000+01:00 │
// ├───────────────────────────────────────┼─────────────────────┼──────────────────────────────────────────┤
// │ 12345678912 │ {} │ 3/21/2361 20:15:12 │
// │ 12345678912 │ {:d} │ 3/21/2361 │
// │ 12345678912 │ {:D} │ Tuesday, March 21, 2361 │
// │ 12345678912 │ {:f} │ Tuesday, March 21, 2361 20:15 │
// │ 12345678912 │ {:F} │ Tuesday, March 21, 2361 20:15:12 │
// │ 12345678912 │ {:g} │ 3/21/2361 20:15 │
// │ 12345678912 │ {:G} │ 3/21/2361 20:15:12 │
// │ 12345678912 │ {:m} │ March 21 │
// │ 12345678912 │ {:M} │ March 21 │
// │ 12345678912 │ {:o} │ 2361-03-21T20:15:12.0000000Z │
// │ 12345678912 │ {:O} │ 2361-03-21T20:15:12.0000000Z │
// │ 12345678912 │ {:r} │ Tue, 21 Mar 2361 20:15:12 GMT │
// │ 12345678912 │ {:R} │ Tue, 21 Mar 2361 20:15:12 GMT │
// │ 12345678912 │ {:s} │ 2361-03-21T20:15:12 │
// │ 12345678912 │ {:t} │ 20:15 │
// │ 12345678912 │ {:T} │ 20:15:12 │
// │ 12345678912 │ {:u} │ 2361-03-21 20:15:12Z │
// │ 12345678912 │ {:U} │ Tuesday, March 21, 2361 20:15:12 │
// │ 12345678912 │ {:y} │ March 2361 │
// │ 12345678912 │ {:Y} │ March 2361 │
// │ 12345678912 │ {:h:mm:ss.ff t} │ 8:15:12.00 P │
// │ 12345678912 │ {:d MMM yyyy} │ 21 Mar 2361 │
// │ 12345678912 │ {:HH:mm:ss.f} │ 20:15:12.0 │
// │ 12345678912 │ {:dd MMM HH:mm:ss} │ 21 Mar 20:15:12 │
// │ 12345678912 │ {:\Mon\t\h\: M} │ Month: 3 │
// │ 12345678912 │ {:HH:mm:ss.ffffzzz} │ 20:15:12.0000+01:00 │
// ├───────────────────────────────────────┼─────────────────────┼──────────────────────────────────────────┤
// │ make_time(1971, 1, 5) │ {} │ 1/5/1971 00:00:00 │
// │ make_time(1971, 1, 5) │ {:d} │ 1/5/1971 │
// │ make_time(1971, 1, 5) │ {:D} │ Tuesday, January 5, 1971 │
// │ make_time(1971, 1, 5) │ {:f} │ Tuesday, January 5, 1971 00:00 │
// │ make_time(1971, 1, 5) │ {:F} │ Tuesday, January 5, 1971 00:00:00 │
// │ make_time(1971, 1, 5) │ {:g} │ 1/5/1971 00:00 │
// │ make_time(1971, 1, 5) │ {:G} │ 1/5/1971 00:00:00 │
// │ make_time(1971, 1, 5) │ {:m} │ January 5 │
// │ make_time(1971, 1, 5) │ {:M} │ January 5 │
// │ make_time(1971, 1, 5) │ {:o} │ 1971-01-05T00:00:00.0000000Z │
// │ make_time(1971, 1, 5) │ {:O} │ 1971-01-05T00:00:00.0000000Z │
// │ make_time(1971, 1, 5) │ {:r} │ Tue, 05 Jan 1971 00:00:00 GMT │
// │ make_time(1971, 1, 5) │ {:R} │ Tue, 05 Jan 1971 00:00:00 GMT │
// │ make_time(1971, 1, 5) │ {:s} │ 1971-01-05T00:00:00 │
// │ make_time(1971, 1, 5) │ {:t} │ 00:00 │
// │ make_time(1971, 1, 5) │ {:T} │ 00:00:00 │
// │ make_time(1971, 1, 5) │ {:u} │ 1971-01-05 00:00:00Z │
// │ make_time(1971, 1, 5) │ {:U} │ Tuesday, January 5, 1971 00:00:00 │
// │ make_time(1971, 1, 5) │ {:y} │ January 1971 │
// │ make_time(1971, 1, 5) │ {:Y} │ January 1971 │
// │ make_time(1971, 1, 5) │ {:h:mm:ss.ff t} │ 0:00:00.00 A │
// │ make_time(1971, 1, 5) │ {:d MMM yyyy} │ 5 Jan 1971 │
// │ make_time(1971, 1, 5) │ {:HH:mm:ss.f} │ 00:00:00.0 │
// │ make_time(1971, 1, 5) │ {:dd MMM HH:mm:ss} │ 05 Jan 00:00:00 │
// │ make_time(1971, 1, 5) │ {:\Mon\t\h\: M} │ Month: 1 │
// │ make_time(1971, 1, 5) │ {:HH:mm:ss.ffffzzz} │ 00:00:00.0000+01:00 │
// ├───────────────────────────────────────┼─────────────────────┼──────────────────────────────────────────┤
// │ make_time(1971, 1, 5, 21, 30, 3) │ {} │ 1/5/1971 21:30:03 │
// │ make_time(1971, 1, 5, 21, 30, 3) │ {:d} │ 1/5/1971 │
// │ make_time(1971, 1, 5, 21, 30, 3) │ {:D} │ Tuesday, January 5, 1971 │
// │ make_time(1971, 1, 5, 21, 30, 3) │ {:f} │ Tuesday, January 5, 1971 21:30 │
// │ make_time(1971, 1, 5, 21, 30, 3) │ {:F} │ Tuesday, January 5, 1971 21:30:03 │
// │ make_time(1971, 1, 5, 21, 30, 3) │ {:g} │ 1/5/1971 21:30 │
// │ make_time(1971, 1, 5, 21, 30, 3) │ {:G} │ 1/5/1971 21:30:03 │
// │ make_time(1971, 1, 5, 21, 30, 3) │ {:m} │ January 5 │
// │ make_time(1971, 1, 5, 21, 30, 3) │ {:M} │ January 5 │
// │ make_time(1971, 1, 5, 21, 30, 3) │ {:o} │ 1971-01-05T21:30:03.0000000Z │
// │ make_time(1971, 1, 5, 21, 30, 3) │ {:O} │ 1971-01-05T21:30:03.0000000Z │
// │ make_time(1971, 1, 5, 21, 30, 3) │ {:r} │ Tue, 05 Jan 1971 21:30:03 GMT │
// │ make_time(1971, 1, 5, 21, 30, 3) │ {:R} │ Tue, 05 Jan 1971 21:30:03 GMT │
// │ make_time(1971, 1, 5, 21, 30, 3) │ {:s} │ 1971-01-05T21:30:03 │
// │ make_time(1971, 1, 5, 21, 30, 3) │ {:t} │ 21:30 │
// │ make_time(1971, 1, 5, 21, 30, 3) │ {:T} │ 21:30:03 │
// │ make_time(1971, 1, 5, 21, 30, 3) │ {:u} │ 1971-01-05 21:30:03Z │
// │ make_time(1971, 1, 5, 21, 30, 3) │ {:U} │ Tuesday, January 5, 1971 21:30:03 │
// │ make_time(1971, 1, 5, 21, 30, 3) │ {:y} │ January 1971 │
// │ make_time(1971, 1, 5, 21, 30, 3) │ {:Y} │ January 1971 │
// │ make_time(1971, 1, 5, 21, 30, 3) │ {:h:mm:ss.ff t} │ 9:30:03.00 P │
// │ make_time(1971, 1, 5, 21, 30, 3) │ {:d MMM yyyy} │ 5 Jan 1971 │
// │ make_time(1971, 1, 5, 21, 30, 3) │ {:HH:mm:ss.f} │ 21:30:03.0 │
// │ make_time(1971, 1, 5, 21, 30, 3) │ {:dd MMM HH:mm:ss} │ 05 Jan 21:30:03 │
// │ make_time(1971, 1, 5, 21, 30, 3) │ {:\Mon\t\h\: M} │ Month: 1 │
// │ make_time(1971, 1, 5, 21, 30, 3) │ {:HH:mm:ss.ffffzzz} │ 21:30:03.0000+01:00 │
// ├───────────────────────────────────────┼─────────────────────┼──────────────────────────────────────────┤
// │ now │ {} │ 10/5/2025 00:18:49 │
// │ now │ {:d} │ 10/5/2025 │
// │ now │ {:D} │ Sunday, October 5, 2025 │
// │ now │ {:f} │ Sunday, October 5, 2025 00:18 │
// │ now │ {:F} │ Sunday, October 5, 2025 00:18:49 │
// │ now │ {:g} │ 10/5/2025 00:18 │
// │ now │ {:G} │ 10/5/2025 00:18:49 │
// │ now │ {:m} │ October 5 │
// │ now │ {:M} │ October 5 │
// │ now │ {:o} │ 2025-10-05T00:18:49.0000000Z │
// │ now │ {:O} │ 2025-10-05T00:18:49.0000000Z │
// │ now │ {:r} │ Sun, 05 Oct 2025 00:18:49 GMT │
// │ now │ {:R} │ Sun, 05 Oct 2025 00:18:49 GMT │
// │ now │ {:s} │ 2025-10-05T00:18:49 │
// │ now │ {:t} │ 00:18 │
// │ now │ {:T} │ 00:18:49 │
// │ now │ {:u} │ 2025-10-05 00:18:49Z │
// │ now │ {:U} │ Sunday, October 5, 2025 00:18:49 │
// │ now │ {:y} │ October 2025 │
// │ now │ {:Y} │ October 2025 │
// │ now │ {:h:mm:ss.ff t} │ 0:18:49.00 A │
// │ now │ {:d MMM yyyy} │ 5 Oct 2025 │
// │ now │ {:HH:mm:ss.f} │ 00:18:49.0 │
// │ now │ {:dd MMM HH:mm:ss} │ 05 Oct 00:18:49 │
// │ now │ {:\Mon\t\h\: M} │ Month: 10 │
// │ now │ {:HH:mm:ss.ffffzzz} │ 00:18:49.0000+02:00 │
// └───────────────────────────────────────┴─────────────────────┴──────────────────────────────────────────┘
basic_string pad_right(xtd::size total_width) const noexcept
Left-aligns the characters in this basic_string, padding with spaces on the right for a specified tot...
Definition basic_string.hpp:1037
xtd::string format(const xtd::string &fmt, args_t &&... args)
Writes the text representation of the specified arguments list, to string using the specified format ...
Definition format.hpp:21
day
Specifies the day of the week.
Definition day.hpp:21
@ text
The xtd::forms::status_bar_panel displays text in the standard font.
Definition status_bar_panel_style.hpp:25