std::strftime(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ‘‘ std::strftime(3) β€” C++ Standard Library πŸ‘‘

ok so this is the std::strftime page from the C++ Standard Libary (yes bestie, that's how they spelled it, we're not fixing it πŸ’…) and it's long, so buckle up. Part 1 is right here, part 2 is coming, do not panic.


🌸 NAME 🌸

std::strftime β€” it's literally std::strftime, that's the whole name, no notes πŸŽ€


✨ SYNOPSIS ✨

This little icon is defined in header <ctime>, and here's the signature, character-for-character, don't touch it:

std::size_t strftime( char* str, std::size_t count, const char* format, const std::tm* tp );

So what's the vibe? It takes the date-and-time info from a given calendar time tp and converts it into a null-terminated multibyte character string str, according to the format string format. Up to count bytes get written. It's giving "glow-up your tm struct into pretty text" energy ✨


πŸ’– PARAMETERS πŸ’–

Here's who's who in the group chat:

  • str β€” pointer to the first element of the char array where the output goes.
  • count β€” maximum number of bytes to write.
  • format β€” pointer to a null-terminated multibyte character string that specifies the format of the conversion.
  • tp β€” pointer to the object containing the date and time information to be converted.

🧚 Format string 🧚

ok so this is the main character of the whole page, pay attention bestie πŸ™ˆ

The format string is made up of zero or more conversion specifiers plus ordinary characters (except %). Every ordinary character β€” including the terminating null character β€” gets copied to the output string without modification, no cap.

Each conversion specification starts with a % character, optionally followed by an E or O modifier (ignored if unsupported by the locale β€” important!!), and then followed by the character that determines the behavior of the specifier.

Here are all the format specifiers available, sorted into their little categories ⬇️


🌈 Year

  • % β€” writes a literal %. The full conversion specification must be %%.
  • %n (C++11) β€” writes a newline character.
  • %t (C++11) β€” writes a horizontal tab character.
  • %Y β€” writes the year as a decimal number, e.g. 2017. (field: tm_year)
  • %EY (C++11) β€” writes the year in the alternative representation, e.g. 平成23εΉ΄ (year Heisei 23) instead of 2011εΉ΄ (year 2011) in the ja_JP locale. (field: tm_year)
  • %y β€” writes the last 2 digits of the year as a decimal number (range [00,99]). (field: tm_year)
  • %Oy (C++11) β€” writes the last 2 digits of the year using the alternative numeric system, e.g. 十一 instead of 11 in the ja_JP locale. (field: tm_year)
  • %Ey (C++11) β€” writes the year as an offset from the locale's alternative calendar period %EC (locale-dependent). (field: tm_year)
  • %C (C++11) β€” writes the first 2 digits of the year as a decimal number (range [00,99]). (field: tm_year)
  • %EC (C++11) β€” writes the name of the base year (period) in the locale's alternative representation, e.g. 平成 (Heisei era) in ja_JP. (field: tm_year)
  • %G (C++11) β€” writes the ISO 8601 week-based year, i.e. the year that contains the specified week. (fields: tm_year, tm_wday, tm_yday)

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 (C++11) β€” writes the last 2 digits of the ISO 8601 week-based year, i.e. the year that contains the specified week (range [00,99]). (fields: tm_year, tm_wday, tm_yday)

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

πŸŽ€ Month

  • %b β€” writes the abbreviated month name, e.g. Oct (locale dependent). (field: tm_mon)
  • %h (C++11) β€” synonym of %b. (field: tm_mon)
  • %B β€” writes the full month name, e.g. October (locale dependent). (field: tm_mon)
  • %m β€” writes the month as a decimal number (range [01,12]). (field: tm_mon)
  • %Om (C++11) β€” writes the month using the alternative numeric system, e.g. 十二 instead of 12 in the ja_JP locale. (field: tm_mon)

πŸ¦‹ Week

  • %U β€” writes the week of the year as a decimal number (Sunday is the first day of the week) (range [00,53]). (fields: tm_year, tm_wday, tm_yday)
  • %OU (C++11) β€” writes the week of the year, as by %U, using the alternative numeric system, e.g. δΊ”εδΊŒ instead of 52 in the ja_JP locale. (fields: tm_year, tm_wday, tm_yday)
  • %W β€” writes the week of the year as a decimal number (Monday is the first day of the week) (range [00,53]). (fields: tm_year, tm_wday, tm_yday)
  • %OW (C++11) β€” writes the week of the year, as by %W, using the alternative numeric system, e.g. δΊ”εδΊŒ instead of 52 in the ja_JP locale. (fields: tm_year, tm_wday, tm_yday)
  • %V (C++11) β€” writes the ISO 8601 week of the year (range [01,53]). (fields: tm_year, tm_wday, tm_yday)

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
  • %OV (C++11) β€” writes the week of the year, as by %V, using the alternative numeric system, e.g. δΊ”εδΊŒ instead of 52 in the ja_JP locale. (fields: tm_year, tm_wday, tm_yday)

πŸ“ Day of the year/month

  • %j β€” writes the day of the year as a decimal number (range [001,366]). (field: tm_yday)
  • %d β€” writes the day of the month as a decimal number (range [01,31]). (field: tm_mday)
  • %Od (C++11) β€” writes the zero-based day of the month using the alternative numeric system, e.g. δΊŒεδΈƒ instead of 27 in the ja_JP locale. (field: tm_mday)

A single character is preceded by a space.

  • %e (C++11) β€” writes the day of the month as a decimal number (range [1,31]). (field: tm_mday)

A single digit is preceded by a space.

  • %Oe (C++11) β€” writes the one-based day of the month using the alternative numeric system, e.g. δΊŒεδΈƒ instead of 27 in the ja_JP locale. (field: tm_mday)

A single character is preceded by a space.


πŸ’œ Day of the week

  • %a β€” writes the abbreviated weekday name, e.g. Fri (locale dependent). (field: tm_wday)
  • %A β€” writes the full weekday name, e.g. Friday (locale dependent). (field: tm_wday)
  • %w β€” writes the weekday as a decimal number, where Sunday is 0 (range [0-6]). (field: tm_wday)
  • %Ow (C++11) β€” writes the weekday, where Sunday is 0, using the alternative numeric system, e.g. 二 instead of 2 in the ja_JP locale. (field: tm_wday)
  • %u (C++11) β€” writes the weekday as a decimal number, where Monday is 1 (ISO 8601 format) (range [1-7]). (field: tm_wday)
  • %Ou (C++11) β€” writes the weekday, where Monday is 1, using the alternative numeric system, e.g. 二 instead of 2 in the ja_JP locale. (field: tm_wday)

⭐ Hour, minute, second

  • %H β€” writes the hour as a decimal number, 24 hour clock (range [00-23]). (field: tm_hour)
  • %OH (C++11) β€” writes the hour from the 24-hour clock using the alternative numeric system, e.g. 十八 instead of 18 in the ja_JP locale. (field: tm_hour)
  • %I β€” writes the hour as a decimal number, 12 hour clock (range [01,12]). (field: tm_hour)
  • %OI (C++11) β€” writes the hour from the 12-hour clock using the alternative numeric system, e.g. ε…­ instead of 06 in the ja_JP locale. (field: tm_hour)
  • %M β€” writes the minute as a decimal number (range [00,59]). (field: tm_min)
  • %OM (C++11) β€” writes the minute using the alternative numeric system, e.g. δΊŒεδΊ” instead of 25 in the ja_JP locale. (field: tm_min)
  • %S β€” writes the second as a decimal number (range [00,60]). (field: tm_sec)
  • %OS (C++11) β€” writes the second using the alternative numeric system, e.g. δΊŒεε›› instead of 24 in the ja_JP locale. (field: tm_sec)

🫢 Other

  • %c β€” writes the standard date and time string, e.g. Sun Oct 17 04:41:13 2010 (locale dependent). (fields: all)
  • %Ec (C++11) β€” writes the alternative date and time string, e.g. using 平成23εΉ΄ (year Heisei 23) instead of 2011εΉ΄ (year 2011) in the ja_JP locale. (fields: all)
  • %x β€” writes the localized date representation (locale dependent). (fields: all)
  • %Ex (C++11) β€” writes the alternative date representation, e.g. using 平成23εΉ΄ (year Heisei 23) instead of 2011εΉ΄ (year 2011) in the ja_JP locale. (fields: all)
  • %X β€” writes the localized time representation, e.g. 18:40:20 or 6:40:20 PM (locale dependent). (fields: all)
  • %EX (C++11) β€” writes the alternative time representation (locale dependent). (fields: all)
  • %D (C++11) β€” equivalent to "%m/%d/%y". (fields: tm_mon, tm_mday, tm_year)
  • %F (C++11) β€” equivalent to "%Y-%m-%d" (the ISO 8601 date format). (fields: tm_mon, tm_mday, tm_year)
  • %r (C++11) β€” writes the localized 12-hour clock time (locale dependent). (fields: tm_hour, tm_min, tm_sec)
  • %R (C++11) β€” equivalent to "%H:%M". (fields: tm_hour, tm_min)
  • %T (C++11) β€” equivalent to "%H:%M:%S" (the ISO 8601 time format). (fields: tm_hour, tm_min, tm_sec)
  • %p β€” writes the localized a.m. or p.m. (locale dependent). (field: tm_hour)
  • %z (C++11) β€” writes the offset from UTC in the ISO 8601 format (e.g. -0430), or no characters if the time zone information is not available. (field: tm_isdst)
  • %Z β€” writes the locale-dependent time zone name or abbreviation, or no characters if the time zone information is not available. (field: tm_isdst)

🫢 RETURN VALUE 🫢

On success, you get the number of bytes written into the character array pointed to by str, not including the terminating '\0'.

⚠️ but bestie listen β€” if count was reached before the entire string could be stored, 0 is returned and the contents are indeterminate. So like, don't trust whatever's in that buffer afterward, fr fr 😭

(part 2 incoming, don't go anywhere πŸ’…)

πŸŽ€ Example πŸŽ€

okay bestie, this example is giving main-character energy, and here's the code exactly as the page shows it β€” no cap, no edits πŸ’…

       // Run this code

        #include <ctime>
        #include <iostream>
        #include <iterator>
        #include <locale>

        void utcExample()
        {
            // Example of the very popular RFC 3339 format UTC time
            std::time_t time = std::time({});
            char timeString[std::size("yyyy-mm-ddThh:mm:ssZ")];
            std::strftime(std::data(timeString), std::size(timeString),
                          "%FT%TZ", std::gmtime(&time));
            std::cout << timeString << '\n';
        }

        int main()
        {
            std::time_t t = std::time(nullptr);
            char mbstr[100];

            if (std::strftime(mbstr, sizeof(mbstr), "%A %c",
       std::localtime(&t)))
                std::cout << mbstr << '\n';

            std::locale::global(std::locale("ja_JP.utf8"));
            if (std::strftime(mbstr, sizeof(mbstr), "%A %c",
       std::localtime(&t)))
                std::cout << mbstr << '\n';

            utcExample();
        }

🌸 Possible output 🌸

and the possible output is literally this, spacing and all:

        Tuesday Tue Sep  7 19:40:35 2021
        η«ζ›œζ—₯ 2021εΉ΄09月07ζ—₯ 19ζ™‚40εˆ†35η§’
        2021-09-07T19:40:35Z

πŸ’œ See also πŸ’œ

  • asctime β€” converts a std::tm object to a textual representation (function)
  • ctime β€” converts a std::time_t object to a textual representation (function)
  • wcsftime β€” converts a std::tm object to custom wide string textual representation (function)
  • put_time (C++11) β€” formats and outputs a date/time value according to the specified format (function template)
  • std::formatter<std::chrono::hh_mm_ss> (C++20) β€” formatting support for hh_mm_ss (class template specialization)
  • C documentation for strftime

footer tea, served cute: source is http://cppreference.com, version/date is 2024.06.10, and the page is std::strftime(3) β€” mwah, bestie πŸ’‹βœ¨

πŸ“¦ where this page comes from