std::put_time(3)
π NAME π
Okay bestie, the manual name is std::put_time, and the NAME line is literally std::put_time - std::put_time β no notes, just vibes. This is the C++ Standard Libary page (yes, the header spells it "Libary", and I'm not fixing it π).
This is part 1, so buckle up.
β¨ Synopsis β¨
Defined in header <iomanip>, and it's a template on CharT. Since C++11, babes.
template< class CharT >
/*unspecified*/ put_time( const std::tm* tmb, const CharT* fmt );
When used in an expression out << put_time(tmb, fmt), it converts the date and time information from a given calendar time tmb to a character string according to format string fmt, as if by calling std::strftime, std::wcsftime, or analog (depending on CharT), according to the std::time_put facet of the locale currently imbued in the output stream out.
π Parameters π
tmbβ pointer to the calendar time structure as obtained fromstd::localtimeorstd::gmtimefmtβ pointer to a null-terminatedCharTstring specifying the format of conversion
π¦ Format string π¦
The format string consists of zero or more conversion specifiers and ordinary characters (except %). All ordinary characters, including the terminating null character, are copied to the output string without modification. Each conversion specification begins with % character, optionally followed by E or O modifier (ignored if unsupported by the locale), followed by the character that determines the behavior of the specifier. The following format specifiers are available:
πΈ Year πΈ
Yβ writes year as a decimal number, e.g.2017. Used fields:tm_year.EY(C++11) β writes year in the alternative representation, e.g.εΉ³ζ23εΉ΄(year Heisei 23) instead of2011εΉ΄(year 2011) inja_JPlocale. Used fields:tm_year.yβ writes last 2 digits of year as a decimal number (range[00,99]). Used fields:tm_year.Oy(C++11) β writes last 2 digits of year using the alternative numeric system, e.g.εδΈinstead of11inja_JPlocale. Used fields:tm_year.Ey(C++11) β writes year as offset from locale's alternative calendar period%EC(locale-dependent). Used fields:tm_year.C(C++11) β writes first 2 digits of year as a decimal number (range[00,99]). Used fields:tm_year.EC(C++11) β writes name of the base year (period) in the locale's alternative representation, e.g.εΉ³ζ(Heisei era) inja_JP. Used fields:tm_year.G(C++11) β writes ISO 8601 week-based year, i.e. the year that contains the specified week.
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
Used fields: tm_year, tm_wday, tm_yday.
g(C++11) β writes last 2 digits of ISO 8601 week-based year, i.e. the year that contains the specified week (range[00,99]).
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
Used fields: tm_year, tm_wday, tm_yday.
π Month π
bβ writes abbreviated month name, e.g.Oct(locale dependent). Used fields:tm_mon.h(C++11) β synonym ofb. Used fields:tm_mon.Bβ writes full month name, e.g.October(locale dependent). Used fields:tm_mon.mβ writes month as a decimal number (range[01,12]). Used fields:tm_mon.Om(C++11) β writes month using the alternative numeric system, e.g.εδΊinstead of12inja_JPlocale. Used fields:tm_mon.
π§ Week π§
Uβ writes 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(C++11) β writes week of the year, as by%U, using the alternative numeric system, e.g.δΊεδΊinstead of52inja_JPlocale. Used fields:tm_year,tm_wday,tm_yday.Wβ writes 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(C++11) β writes week of the year, as by%W, using the alternative numeric system, e.g.δΊεδΊinstead of52inja_JPlocale. Used fields:tm_year,tm_wday,tm_yday.V(C++11) β writes ISO 8601 week of the year (range[01,53]).
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
Used fields: tm_year, tm_wday, tm_yday.
OV(C++11) β writes week of the year, as by%V, using the alternative numeric system, e.g.δΊεδΊinstead of52inja_JPlocale. Used fields:tm_year,tm_wday,tm_yday.
π Day of the year/month π
jβ writes day of the year as a decimal number (range[001,366]). Used fields:tm_yday.dβ writes day of the month as a decimal number (range[01,31]). Used fields:tm_mday.Od(C++11) β writes zero-based day of the month using the alternative numeric system, e.g.δΊεδΈinstead of27inja_JPlocale. Single character is preceded by a space. Used fields:tm_mday.e(C++11) β writes day of the month as a decimal number (range[1,31]). Single digit is preceded by a space. Used fields:tm_mday.Oe(C++11) β writes one-based day of the month using the alternative numeric system, e.g.δΊεδΈinstead of27inja_JPlocale. Single character is preceded by a space. Used fields:tm_mday.
π Day of the week π
aβ writes abbreviated weekday name, e.g.Fri(locale dependent). Used fields:tm_wday.Aβ writes full weekday name, e.g.Friday(locale dependent). Used fields:tm_wday.wβ writes weekday as a decimal number, where Sunday is 0 (range[0-6]). Used fields:tm_wday.Ow(C++11) β writes weekday, where Sunday is 0, using the alternative numeric system, e.g.δΊinstead of2inja_JPlocale. Used fields:tm_wday.u(C++11) β writes weekday as a decimal number, where Monday is 1 (ISO 8601 format) (range[1-7]). Used fields:tm_wday.Ou(C++11) β writes weekday, where Monday is 1, using the alternative numeric system, e.g.δΊinstead of2inja_JPlocale. Used fields:tm_wday.
β Hour, minute, second β
Hβ writes hour as a decimal number, 24 hour clock (range[00-23]). Used fields:tm_hour.OH(C++11) β writes hour from 24-hour clock using the alternative numeric system, e.g.εε «instead of18inja_JPlocale. Used fields:tm_hour.Iβ writes hour as a decimal number, 12 hour clock (range[01,12]). Used fields:tm_hour.OI(C++11) β writes hour from 12-hour clock using the alternative numeric system, e.g.ε instead of06inja_JPlocale. Used fields:tm_hour.Mβ writes minute as a decimal number (range[00,59]). Used fields:tm_min.OM(C++11) β writes minute using the alternative numeric system, e.g.δΊεδΊinstead of25inja_JPlocale. Used fields:tm_min.Sβ writes second as a decimal number (range[00,60]). Used fields:tm_sec.OS(C++11) β writes second using the alternative numeric system, e.g.δΊεεinstead of24inja_JPlocale. Used fields:tm_sec.
π Other π
cβ writes standard date and time string, e.g.Sun Oct 17 04:41:13 2010(locale dependent). Used fields: all.Ec(C++11) β writes alternative date and time string, e.g. usingεΉ³ζ23εΉ΄(year Heisei 23) instead of2011εΉ΄(year 2011) inja_JPlocale. Used fields: all.xβ writes localized date representation (locale dependent). Used fields: all.Ex(C++11) β writes alternative date representation, e.g. usingεΉ³ζ23εΉ΄(year Heisei 23) instead of2011εΉ΄(year 2011) inja_JPlocale. Used fields: all.Xβ writes localized time representation, e.g.18:40:20or6:40:20 PM(locale dependent). Used fields: all.EX(C++11) β writes alternative time representation (locale dependent). Used fields: all.D(C++11) β equivalent to"%m/%d/%y". Used fields:tm_mon,tm_mday,tm_year.F(C++11) β equivalent to"%Y-%m-%d"(the ISO 8601 date format). Used fields:tm_mon,tm_mday,tm_year.r(C++11) β writes localized 12-hour clock time (locale dependent). Used fields:tm_hour,tm_min,tm_sec.R(C++11) β equivalent to"%H:%M". Used fields:tm_hour,tm_min.T(C++11) β equivalent to"%H:%M:%S"(the ISO 8601 time format). Used fields:tm_hour,tm_min,tm_sec.pβ writes localizeda.m.orp.m.(locale dependent). Used fields:tm_hour.z(C++11) β writes offset from UTC in the ISO 8601 format (e.g.-0430), or no characters if the time zone information is not available. Used fields:tm_isdst.Zβ writes locale-dependent time zone name or abbreviation, or no characters if the time zone information is not available. Used fields:tm_isdst.
π Return value π
An object of unspecified type such that:
- if
outis an object of typestd::basic_ostream<CharT, Traits>, the expressionout << put_time(tmb, fmt)- has type
std::basic_ostream<CharT, Traits>& - has value
out - behaves as if it called
f(out, tmb, fmt)
- has type
where the function f is defined as:
template<class CharT, class Traits>
void f(std::basic_ios<CharT, Traits>& str, const std::tm* tmb, const CharT* fmt)
{
using Iter = std::ostreambuf_iterator<CharT, Traits>;
using TimePut = std::time_put<CharT, Iter>;
const TimePut& tp = std::use_facet<TimePut>(str.getloc());
const Iter end = tp.put(Iter(str.rdbuf()), str, str.fill(), tmb,
fmt, fmt + Traits::length(fmt));
if (end.failed())
str.setstate(std::ios_base::badbit);
}
πΈ EXAMPLE πΈ
ok bestie this is the moment, the main character demo, the whole reason you're here π literally just run this code:
// Run this code
#include <ctime>
#include <iomanip>
#include <iostream>
int main()
{
std::time_t t = std::time(nullptr);
std::tm tm = *std::localtime(&t);
std::cout.imbue(std::locale("ru_RU.utf8"));
std::cout << "ru_RU: " << std::put_time(&tm, "%c %Z") << '\n';
std::cout.imbue(std::locale("ja_JP.utf8"));
std::cout << "ja_JP: " << std::put_time(&tm, "%c %Z") << '\n';
}
so what's happening here π you grab the current time with std::time(nullptr), turn it into a std::tm via *std::localtime(&t), then you hit std::cout.imbue(...) to swap the locale β first ru_RU.utf8, then ja_JP.utf8 β and print the same std::put_time(&tm, "%c %Z") both times. same std::tm, same format string, two different locales, two different vibes. that's the whole flex of this function tbh β¨
β¨ Possible output β¨
and it may spit out something like this π (note: possible, not guaranteed, locales be like that):
ru_RU: Π‘Ρ. 28 Π΄Π΅ΠΊ. 2011 10:21:16 EST
ja_JP: 2011εΉ΄12ζ28ζ₯ 10ζ21ε16η§ EST
look at that π
same instant in time, and %c gives you the full locale-flavoured date-time while %Z tacks on the time zone name. iconic behaviour, no cap π«Ά
π SEE ALSO π
your little friends in the neighbourhood, go say hi:
time_putβ formats contents ofstd::tmfor output as character sequence (class template) πget_timeβ parses a date/time value of specified format (C++11) (function template) πstrftimeβ converts astd::tmobject to custom textual representation (function) πwcsftimeβ converts astd::tmobject to custom wide string textual representation (function) π
ok that's the whole page, you made it to the end, I'm so proud of you fr fr ππ this lil beauty comes to you courtesy of cppreference.com, version dated 2024.06.10 β go drink some water and then go format some timestamps, you absolute icon πβ¨π
π¦ 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