std::wcsftime(3)

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

✨ std::wcsftime ✨

ok so this is std::wcsftime, and it lives in the C++ Standard Library β€” we love a legend with a home fr fr πŸ’–. Come on bestie, let's format some wide-character dates together πŸ’…


πŸŽ€ NAME πŸŽ€

std::wcsftime β€” that's it, that's the name, no notes, periodt πŸ‘‘


🌸 Synopsis 🌸

Defined in header <cwchar>

std::size_t wcsftime( wchar_t* str, std::size_t count, const wchar_t* format,
                      const std::tm* time );

So what does it do? It takes the date and time info from a given calendar time time and converts it into a null-terminated wide character string str, following the format string format. Up to count wide characters get written. That's the whole vibe πŸ¦‹


πŸ’œ Parameters πŸ’œ

  • str β€” pointer to the first element of the wchar_t array where the output goes. The stage, the venue, the main character ✨
  • count β€” the maximum number of wide characters to write. It's a limit, not a suggestion, bestie πŸ™ˆ
  • format β€” pointer to a null-terminated wide character string that says how the conversion should be formatted
  • time β€” pointer to the date and time information that's getting converted

🌈 Format string 🌈

ok so the format string is made of zero or more conversion specifiers and ordinary characters (ordinary characters being everything except %). All the ordinary characters, including the terminating null character, get copied to the output string without modification β€” no edits, no glow-up, just straight through πŸ’―

Every conversion specification starts with a % character, then optionally an E or O modifier (ignored if the locale doesn't support it, no hard feelings), then the character that decides what the specifier actually does.

Here's the full roster of format specifiers, bestie β€” every single one πŸ‘‡

🧚 The little ones 🧚

  • % β€” writes a literal %. Careful though: the full conversion specification must be %% πŸ’€
  • n β€” writes the newline character (C++11)
  • t β€” writes the horizontal tab character (C++11)

πŸ‘‘ Year πŸ‘‘

  • Y β€” writes the year as a decimal number, e.g. 2017 β€” used field: tm_year
  • EY β€” writes the year in the alternative representation, e.g. 平成23εΉ΄ (year Heisei 23) instead of 2011εΉ΄ (year 2011) in the ja_JP locale (C++11) β€” used field: tm_year
  • y β€” writes the last 2 digits of the year as a decimal number (range [00,99]) β€” used field: tm_year
  • Oy β€” writes the last 2 digits of the year using the alternative numeric system, e.g. 十一 instead of 11 in the ja_JP locale (C++11) β€” used field: tm_year
  • Ey β€” writes the year as an offset from the locale's alternative calendar period %EC (locale-dependent) (C++11) β€” used field: tm_year
  • C β€” writes the first 2 digits of the year as a decimal number (range [00,99]) (C++11) β€” used field: tm_year
  • EC β€” writes the name of the base year (period) in the locale's alternative representation, e.g. 平成 (Heisei era) in ja_JP (C++11) β€” used field: tm_year
  • G β€” writes the ISO 8601 week-based year, i.e. the year that contains the specified week (C++11) β€” used 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 β€” writes the last 2 digits of the ISO 8601 week-based year, i.e. the year that contains the specified week (range [00,99]) (C++11) β€” used 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) β€” used field: tm_mon
  • h β€” synonym of b (C++11) β€” used field: tm_mon
  • B β€” writes the full month name, e.g. October (locale dependent) β€” used field: tm_mon
  • m β€” writes the month as a decimal number (range [01,12]) β€” used field: tm_mon
  • Om β€” writes the month using the alternative numeric system, e.g. 十二 instead of 12 in the ja_JP locale (C++11) β€” used 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]) β€” used fields: tm_year, tm_wday, tm_yday
  • OU β€” writes the week of the year, as by %U, using the alternative numeric system, e.g. δΊ”εδΊŒ instead of 52 in the ja_JP locale (C++11) β€” used 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]) β€” used fields: tm_year, tm_wday, tm_yday
  • OW β€” writes the week of the year, as by %W, using the alternative numeric system, e.g. δΊ”εδΊŒ instead of 52 in the ja_JP locale (C++11) β€” used fields: tm_year, tm_wday, tm_yday
  • V β€” writes the ISO 8601 week of the year (range [01,53]) (C++11) β€” used 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 β€” writes the week of the year, as by %V, using the alternative numeric system, e.g. δΊ”εδΊŒ instead of 52 in the ja_JP locale (C++11) β€” used 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]) β€” used field: tm_yday
  • d β€” writes the day of the month as a decimal number (range [01,31]) β€” used field: tm_mday
  • Od β€” writes the zero-based day of the month using the alternative numeric system, e.g. δΊŒεδΈƒ instead of 27 in the ja_JP locale (C++11) β€” used field: tm_mday
    • Single character is preceded by a space.
  • e β€” writes the day of the month as a decimal number (range [1,31]) (C++11) β€” used field: tm_mday
    • Single digit is preceded by a space.
  • Oe β€” writes the one-based day of the month using the alternative numeric system, e.g. δΊŒεδΈƒ instead of 27 in the ja_JP locale (C++11) β€” used field: tm_mday
    • Single character is preceded by a space.

πŸ’… Day of the week πŸ’…

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

⭐ Hour, minute, second ⭐

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

πŸ”₯ Other πŸ”₯

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

🫢 Return value 🫢

On success you get the number of wide characters written into the wide character array pointed to by str, not counting the terminating L'\0' β€” that one's on the house, bestie πŸ’–

⚠️ But listen: if count was reached before the entire string could be stored, 0 is returned and the contents are undefined. Not "partially there", not "close enough" β€” literally undefined. So size your buffer like you mean it 😭

πŸ’€ Example πŸ’€

// Run this code

#include <ctime>
#include <cwchar>
#include <iostream>
#include <locale>

int main()
{
    std::locale::global(std::locale("ja_JP.utf8"));
    std::time_t t = std::time(nullptr);
    wchar_t wstr[100];
    if (std::wcsftime(wstr, 100, L"%A %c", std::localtime(&t)))
        std::wcout << wstr << '\n';
}

Possible output:

η«ζ›œζ—₯ 2011εΉ΄12月27ζ—₯ 17ζ™‚43εˆ†13η§’

no cap that output is genuinely iconic πŸŽ€

πŸ‘€ See also πŸ‘€

  • strftime β€” bestie, this is the O.G. version: it converts a std::tm object into a custom textual representation. (It's a function, no template nonsense.) ✨
  • put_time β€” formats and outputs a date/time value according to the specified format. It's giving "the fancy C++11 way," and yes it's a function template. πŸŽ€ (C++11)
  • C documentation for wcsftime β€” like, the whole original C vibe if you wanna go full retro. 🌈

okay so that's literally the whole list πŸ’… go forth and format your little hearts out, bestie 🫢

And that's a wrap, fr fr! This iconic little page came from http://cppreference.com, dated 2024.06.10, and it's the std::wcsftime(3) page β€” she ate and left no crumbs πŸ‘‘πŸ’–

mwah β€” catch you in the next manpage, bestie! πŸŒΈπŸ¦‹β­

πŸ“¦ where this page comes from