std::wcsftime(3)
β¨ 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 thewchar_tarray 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 formattedtimeβ 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_yearEYβ writes the year in the alternative representation, e.g.εΉ³ζ23εΉ΄(year Heisei 23) instead of2011εΉ΄(year 2011) in theja_JPlocale (C++11) β used field:tm_yearyβ writes the last 2 digits of the year as a decimal number (range[00,99]) β used field:tm_yearOyβ writes the last 2 digits of the year using the alternative numeric system, e.g.εδΈinstead of11in theja_JPlocale (C++11) β used field:tm_yearEyβ writes the year as an offset from the locale's alternative calendar period%EC(locale-dependent) (C++11) β used field:tm_yearCβ writes the first 2 digits of the year as a decimal number (range[00,99]) (C++11) β used field:tm_yearECβ writes the name of the base year (period) in the locale's alternative representation, e.g.εΉ³ζ(Heisei era) inja_JP(C++11) β used field:tm_yearGβ 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
- In ISO 8601, weeks begin with Monday and the first week of the year must satisfy the following requirements:
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
- In ISO 8601, weeks begin with Monday and the first week of the year must satisfy the following requirements:
πΈ Month πΈ
bβ writes the abbreviated month name, e.g.Oct(locale dependent) β used field:tm_monhβ synonym ofb(C++11) β used field:tm_monBβ writes the full month name, e.g.October(locale dependent) β used field:tm_monmβ writes the month as a decimal number (range[01,12]) β used field:tm_monOmβ writes the month using the alternative numeric system, e.g.εδΊinstead of12in theja_JPlocale (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_ydayOUβ writes the week of the year, as by%U, using the alternative numeric system, e.g.δΊεδΊinstead of52in theja_JPlocale (C++11) β used fields:tm_year,tm_wday,tm_ydayWβ 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_ydayOWβ writes the week of the year, as by%W, using the alternative numeric system, e.g.δΊεδΊinstead of52in theja_JPlocale (C++11) β used fields:tm_year,tm_wday,tm_ydayVβ 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
- In ISO 8601, weeks begin with Monday and the first week of the year must satisfy the following requirements:
OVβ writes the week of the year, as by%V, using the alternative numeric system, e.g.δΊεδΊinstead of52in theja_JPlocale (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_ydaydβ writes the day of the month as a decimal number (range[01,31]) β used field:tm_mdayOdβ writes the zero-based day of the month using the alternative numeric system, e.g.δΊεδΈinstead of27in theja_JPlocale (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 of27in theja_JPlocale (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_wdayAβ writes the full weekday name, e.g.Friday(locale dependent) β used field:tm_wdaywβ writes the weekday as a decimal number, where Sunday is0(range[0-6]) β used field:tm_wdayOwβ writes the weekday, where Sunday is0, using the alternative numeric system, e.g.δΊinstead of2in theja_JPlocale (C++11) β used field:tm_wdayuβ writes the weekday as a decimal number, where Monday is1(ISO 8601 format) (range[1-7]) (C++11) β used field:tm_wdayOuβ writes the weekday, where Monday is1, using the alternative numeric system, e.g.δΊinstead of2in theja_JPlocale (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_hourOHβ writes the hour from the 24-hour clock using the alternative numeric system, e.g.εε «instead of18in theja_JPlocale (C++11) β used field:tm_hourIβ writes the hour as a decimal number, 12 hour clock (range[01,12]) β used field:tm_hourOIβ writes the hour from the 12-hour clock using the alternative numeric system, e.g.ε instead of06in theja_JPlocale (C++11) β used field:tm_hourMβ writes the minute as a decimal number (range[00,59]) β used field:tm_minOMβ writes the minute using the alternative numeric system, e.g.δΊεδΊinstead of25in theja_JPlocale (C++11) β used field:tm_minSβ writes the second as a decimal number (range[00,60]) β used field:tm_secOSβ writes the second using the alternative numeric system, e.g.δΊεεinstead of24in theja_JPlocale (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: allEcβ writes the alternative date and time string, e.g. usingεΉ³ζ23εΉ΄(year Heisei 23) instead of2011εΉ΄(year 2011) in theja_JPlocale (C++11) β used fields: allxβ writes the localized date representation (locale dependent) β used fields: allExβ writes the alternative date representation, e.g. usingεΉ³ζ23εΉ΄(year Heisei 23) instead of2011εΉ΄(year 2011) in theja_JPlocale (C++11) β used fields: allXβ writes the localized time representation, e.g.18:40:20or6:40:20 PM(locale dependent) β used fields: allEXβ writes the alternative time representation (locale dependent) (C++11) β used fields: allDβ equivalent to"%m/%d/%y"(C++11) β used fields:tm_mon,tm_mday,tm_yearFβ equivalent to"%Y-%m-%d"(the ISO 8601 date format) (C++11) β used fields:tm_mon,tm_mday,tm_yearrβ writes the localized 12-hour clock time (locale dependent) (C++11) β used fields:tm_hour,tm_min,tm_secRβ equivalent to"%H:%M"(C++11) β used fields:tm_hour,tm_minTβ equivalent to"%H:%M:%S"(the ISO 8601 time format) (C++11) β used fields:tm_hour,tm_min,tm_secpβ writes the localized a.m. or p.m. (locale dependent) β used field:tm_hourzβ 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_isdstZβ 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: ifcountwas reached before the entire string could be stored,0is 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 astd::tmobject 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
- 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