std::format..._time_format(3)

๐Ÿ“ฆ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

โœจ 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, %z or a modified variant of %z is used, a std::format_error is thrown. like, instant drama ๐Ÿ˜ญ
  • 2-4) โ€” the local_time_format function returns a value of unspecified type that holds the local time, offset, and time zone abbreviation. When an object f of this unspecified type is formatted, if %Z is used, it is replaced with *f.abbrev widened to CharT if f.abbrev is not null; otherwise a std::format_error is thrown. If %z or a modified variant thereof is used, an offset of *f.offset_sec will be formatted if f.offset_sec is not null; otherwise a std::format_error is 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 if L is not present in the format specification,
  • otherwise, the locale denoted by the std::locale passed to the formatting function, if any,
  • otherwise (L is present but no std::locale is 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 by 100 using floored division. If the result is a single decimal digit, it is prefixed with 0.
  • %EC โ€” the modified command %EC writes 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 by 0.
  • %Oy โ€” the modified command %Oy writes the localeโ€™s alternative representation.
  • %Ey โ€” the modified command %Ey writes 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 with 0 to four digits.
  • %EY โ€” the modified command %EY writes 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 is 01). If the result is a single digit, it is prefixed with 0.
  • %Om โ€” the modified command %Om writes 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 with 0.
  • %Od โ€” the modified command %Od writes 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 %Oe writes 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 is 1.
  • %Ou โ€” the modified command %Ou writes the localeโ€™s alternative representation.
  • %w โ€” writes the weekday as a decimal number (0-6), where Sunday is 0.
  • %Ow โ€” the modified command %Ow writes 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 by 0.
  • %G โ€” writes the ISO 8601 week-based year as a decimal number. If the result is less than four digits it is left-padded with 0 to 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 with 0.
  • %OV โ€” the modified command %OV writes the localeโ€™s alternative representation.

๐ŸŒธ Week/day of the year ๐ŸŒธ

  • %j โ€” writes the day of the year as a decimal number (January 1 is 001). If the result is less than three digits, it is left-padded with 0 to 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 week 01. Days of the same year prior to that are in week 00. If the result is a single digit, it is prefixed with 0.
  • %OU โ€” the modified command %OU writes 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 week 01. Days of the same year prior to that are in week 00. If the result is a single digit, it is prefixed with 0.
  • %OW โ€” the modified command %OW writes 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 %Ex produces 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 with 0.
  • %OH โ€” the modified command %OH writes 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 with 0.
  • %OI โ€” the modified command %OI writes the localeโ€™s alternative representation.
  • %M โ€” writes the minute as a decimal number. If the result is a single digit, it is prefixed with 0.
  • %OM โ€” the modified command %OM writes the localeโ€™s alternative representation.
  • %S, %OS โ€” writes the second as a decimal number. If the number of seconds is less than 10, the result is prefixed with 0. 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 within 18 fractional digits). The character for the decimal point is localized according to the locale.
  • %OS โ€” the modified command %OS writes 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 %EX writes the locale's alternate time representation. fancy โœจ

๐ŸŒ Time zone ๐ŸŒ

  • %z โ€” writes the offset from UTC in the ISO 8601 format. for example, -0430 refers to 4 hours 30 minutes behind UTC. and if the offset is zero, +0000 is what gets used. periodt.
  • %Ez, %Oz โ€” the modified commands %Ez and %Oz slip 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 %Ec writes 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 via count().
  • %q โ€” writes the unit suffix of the duration, as specified in operator<<().

๐ŸŒˆ 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