std::strftime(3)
π 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 thechararray 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 of2011εΉ΄(year 2011) in theja_JPlocale. (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 of11in theja_JPlocale. (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) inja_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 of12in theja_JPlocale. (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 of52in theja_JPlocale. (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 of52in theja_JPlocale. (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 of52in theja_JPlocale. (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 of27in theja_JPlocale. (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 of27in theja_JPlocale. (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 is0(range[0-6]). (field:tm_wday)%Ow(C++11) β writes the weekday, where Sunday is0, using the alternative numeric system, e.g.δΊinstead of2in theja_JPlocale. (field:tm_wday)%u(C++11) β writes the weekday as a decimal number, where Monday is1(ISO 8601 format) (range[1-7]). (field:tm_wday)%Ou(C++11) β writes the weekday, where Monday is1, using the alternative numeric system, e.g.δΊinstead of2in theja_JPlocale. (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 of18in theja_JPlocale. (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 of06in theja_JPlocale. (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 of25in theja_JPlocale. (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 of24in theja_JPlocale. (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 of2011εΉ΄(year 2011) in theja_JPlocale. (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 of2011εΉ΄(year 2011) in theja_JPlocale. (fields: all)%Xβ writes the localized time representation, e.g.18:40:20or6: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 β ifcountwas reached before the entire string could be stored,0is 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 astd::tmobject to a textual representation (function)ctimeβ converts astd::time_tobject to a textual representation (function)wcsftimeβ converts astd::tmobject 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 forhh_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
- 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