std::format..._time_format(3)
โจ std::format..._time_format(3) โจ
okay so bestie, this is the C++ Standard Libary manual, section 3, and itโs part 1 of 2 โ yes, the manual really spells it โLibary,โ and we are not fixing the lore ๐
. Todayโs main characters are std::formatter and std::chrono::local_time_format, and theyโre sharing a page, which is lowkey iconic because formatting a std::chrono::local_time should not be a whole villain arc.
๐ NAME ๐
std::formatter,std::chrono::local_time_format โ std::formatter,std::chrono::local_time_format
๐ฆ Synopsis ๐ฆ
okay, deep breath, hereโs the whole declaration vibe:
Defined in header <chrono>
template< class Duration, class CharT > (1)
struct formatter<std::chrono::local_time<Duration>, CharT>;
template< class Duration >
struct /*local-time-format-t*/ {
std::chrono::local_time<Duration> time; // exposition
only
const std::string* abbrev; // exposition (2)
(exposition only*)
only
const std::chrono::seconds* offset; // exposition
only
};
template< class Duration >
/*local-time-format-t*/<Duration>
local_time_format( const
std::chrono::local_time<Duration>& tp, (3)
const std::string* abbrev = nullptr,
const std::chrono::seconds* offset_sec
= nullptr );
template< class Duration, class CharT > (4)
struct formatter</*local-time-format-t*/<Duration>, CharT>;
so what is this? Itโs the specialization of std::formatter that defines the formatting rules for a std::chrono::local_time. periodt.
- 1) โ if
%Z,%zor a modified variant of%zis used, astd::format_erroris thrown. like, instant drama ๐ญ - 2-4) โ the
local_time_formatfunction returns a value of unspecified type that holds the local time, offset, and time zone abbreviation. When an objectfof this unspecified type is formatted, if%Zis used, it is replaced with*f.abbrevwidened toCharTiff.abbrevis not null; otherwise astd::format_erroris thrown. If%zor a modified variant thereof is used, an offset of*f.offset_secwill be formatted iff.offset_secis not null; otherwise astd::format_erroris thrown.
and std::formatter is usually not directly accessed, but is used through formatting functions โ behind-the-scenes main character energy, fr fr.
๐ Format specification ๐
The format specification has the form
fill-and-align (optional) width (optional) precision (optional) L(optional) chrono-spec (optional)
fill-and-align, width, and precision have the same meaning as in standard format specification. precision is valid only for std::chrono::duration types where the representation type Rep is a floating-point type, otherwise std::format_error is thrown.
The locale used for formatting is determined as follows:
- the default
"C"locale ifLis not present in the format specification, - otherwise, the locale denoted by the
std::localepassed to the formatting function, if any, - otherwise (
Lis present but nostd::localeis passed to the formatting function), the global locale.
The chrono-spec consists of one or more conversion specifiers and ordinary characters (other than {, }, and %). A chrono-spec must start with a conversion specifier. All ordinary characters are written to the output without modification. Each unmodified conversion specifier begins with a % character followed by a character that determines the behavior of the specifier. Some conversion specifiers have a modified form in which an E or O modifier character is inserted after the % character. Each conversion specifier is replaced by appropriate characters in the output as described below.
If the chrono-spec is empty, the chrono object is formatted as if by streaming it to an object os of type std::basic_ostringstream<CharT> with the formatting locale (one of std::locale::classic(), the passed std::locale object, and std::locale::global()) imbued and copying os.str() to the output buffer with additional padding and adjustments as per format specifiers.
๐ธ The following format specifiers are available ๐ธ
โจ General โจ
%%โ writes a literal%character.%nโ writes a newline character.%tโ writes a horizontal tab character.
๐ธ Year ๐ธ
%Cโ writes the year divided by100using floored division. If the result is a single decimal digit, it is prefixed with0.%ECโ the modified command%ECwrites the localeโs alternative representation of the century.%yโ writes the last two decimal digits of the year. If the result is a single digit it is prefixed by0.%Oyโ the modified command%Oywrites the localeโs alternative representation.%Eyโ the modified command%Eywrites the localeโs alternative representation of offset from%EC(year only).%Yโ writes the year as a decimal number. If the result is less than four digits it is left-padded with0to four digits.%EYโ the modified command%EYwrites the localeโs alternative full year representation.
๐ธ Month ๐ธ
%b,%hโ writes the localeโs abbreviated month name.%Bโ writes the localeโs full month name.%mโ writes the month as a decimal number (January is01). If the result is a single digit, it is prefixed with0.%Omโ the modified command%Omwrites the localeโs alternative representation.
๐ธ Day ๐ธ
%dโ writes the day of month as a decimal number. If the result is a single decimal digit, it is prefixed with0.%Odโ the modified command%Odwrites the localeโs alternative representation.%eโ writes the day of month as a decimal number. If the result is a single decimal digit, it is prefixed with a space.%Oeโ the modified command%Oewrites the localeโs alternative representation.
๐ธ Day of the week ๐ธ
%aโ writes the localeโs abbreviated weekday name.%Aโ writes the localeโs full weekday name.%uโ writes the ISO weekday as a decimal number (1-7), where Monday is1.%Ouโ the modified command%Ouwrites the localeโs alternative representation.%wโ writes the weekday as a decimal number (0-6), where Sunday is0.%Owโ the modified command%Owwrites the localeโs alternative representation.
๐ธ ISO 8601 week-based year ๐ธ
In ISO 8601 weeks begin with Monday and the first week of the year must satisfy the following requirements:
- Includes January 4
- Includes first Thursday of the year
%gโ writes the last two decimal digits of the ISO 8601 week-based year. If the result is a single digit it is prefixed by0.%Gโ writes the ISO 8601 week-based year as a decimal number. If the result is less than four digits it is left-padded with0to four digits.%Vโ writes the ISO 8601 week of the year as a decimal number. If the result is a single digit, it is prefixed with0.%OVโ the modified command%OVwrites the localeโs alternative representation.
๐ธ Week/day of the year ๐ธ
%jโ writes the day of the year as a decimal number (January 1 is001). If the result is less than three digits, it is left-padded with0to three digits.%Uโ writes the week number of the year as a decimal number. The first Sunday of the year is the first day of week01. Days of the same year prior to that are in week00. If the result is a single digit, it is prefixed with0.%OUโ the modified command%OUwrites the localeโs alternative representation.%Wโ writes the week number of the year as a decimal number. The first Monday of the year is the first day of week01. Days of the same year prior to that are in week00. If the result is a single digit, it is prefixed with0.%OWโ the modified command%OWwrites the localeโs alternative representation.
๐ธ Date ๐ธ
%Dโ equivalent to"%m/%d/%y".%Fโ equivalent to"%Y-%m-%d".%xโ writes the localeโs date representation.%Exโ the modified command%Exproduces the localeโs alternate date representation.
๐ธ Time of day ๐ธ
%Hโ writes the hour (24-hour clock) as a decimal number. If the result is a single digit, it is prefixed with0.%OHโ the modified command%OHwrites the localeโs alternative representation.%Iโ writes the hour (12-hour clock) as a decimal number. If the result is a single digit, it is prefixed with0.%OIโ the modified command%OIwrites the localeโs alternative representation.%Mโ writes the minute as a decimal number. If the result is a single digit, it is prefixed with0.%OMโ the modified command%OMwrites the localeโs alternative representation.%S,%OSโ writes the second as a decimal number. If the number of seconds is less than10, the result is prefixed with0. If the precision of the input cannot be exactly represented with seconds, then the format is a decimal floating-point number with a fixed format and a precision matching that of the precision of the input (or to a microseconds precision if the conversion to floating-point decimal seconds cannot be made within18fractional digits). The character for the decimal point is localized according to the locale.%OSโ the modified command%OSwrites out the locale's alternative representation. slay ๐%pโ drops the locale's equivalent of the AM/PM designations that go with a 12-hour clock. iconic.%Rโ literally the same energy as"%H:%M".%Tโ and this one's just"%H:%M:%S", no cap.%rโ writes the locale's 12-hour clock time. cute.%Xโ writes the locale's time representation.%EXโ the modified command%EXwrites the locale's alternate time representation. fancy โจ
๐ Time zone ๐
%zโ writes the offset from UTC in the ISO 8601 format. for example,-0430refers to 4 hours 30 minutes behind UTC. and if the offset is zero,+0000is what gets used. periodt.%Ez,%Ozโ the modified commands%Ezand%Ozslip a:in between the hours and minutes (e.g.,-04:30). the way that little colon ate ๐ซถ%Zโ writes the time zone abbreviation.
๐ Miscellaneous ๐
%cโ writes the locale's date and time representation.%Ecโ the modified command%Ecwrites the locale's alternative date and time representation. bestie behaviour honestly.
heads up: the following specifiers are recognized, but they will cause std::format_error to be thrown. not a drill ๐
Conversion specifier โ Duration count
%Qโ writes the count of ticks of the duration, i.e. the value obtained viacount().%qโ writes the unit suffix of the duration, as specified inoperator<<().
๐ Example ๐
This section is incomplete
Reason: no example
ngl this section is giving "todo: me" ๐ญ no example yet, we move.
๐ See also ๐
formatโ stores formatted representation of the arguments in a new string โ (C++20) (function template)
Category:
- Todo no example
okay that's the whole vibe check, you're officially a time-formatting main character now ๐ this page came straight from cppreference.com, dated 2024.06.10, and it's the manual page for std::format..._time_format(3) โ go forth and format, bestie ๐ธ๐ฆ
๐ฆ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 ยท MIT
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms