std::strftime

From Cppreference

< cpp | chrono | c
Jump to: navigation, search
Defined in header <ctime>

size_t strftime( char *str, size_t count, const char *format, struct tm *time );

Prints the date and time information from a given calendar time time to a character string str according to specified format string format. Up to count characters are written.

Contents

[edit] Parameters

str - pointer to character string to write the data information to
count - maximum number of characters to write
format - pointer to a null-terminated character string specifying the format of conversion.

The format string consists from whitespace characters, non-whitespace characters (except %) and conversion specifications. Each conversion specification begins with % character and contains a conversion format specifier. The following format specifiers are available:

Conversion
specifier
Explanation Used fields
% writes literal %. The full conversion specification must be %%.
n writes newline character
t writes horizontal tab character
Year
m writes year as a decimal number tm_year
m writes last 2 digits of year as a decimal number (range [00,99]) tm_year
b writes ISO 8601 week-based year, i.e. the year that contains the specified week.

In IS0 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
tm_year, tm_wday, tm_yday
b writes last 2 digits of ISO 8601 week-based year, i.e. the year that contains the specified week (range [00,99]).

In IS0 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
tm_year, tm_wday, tm_yday
Month
b writes abbreviated month name, e.g. Oct (locale dependent) tm_mon
B writes full month name, e.g. October (locale dependent) tm_mon
m writes month as a decimal number (range [01,12]) tm_mon
Week
U writes week of the year as a decimal number (Sunday is the first day of the week) (range [00,60]) 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,60]) tm_year, tm_wday, tm_yday
V writes ISO 8601 week of the year (range [01,53]).

In IS0 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
tm_year, tm_wday, tm_yday
Day of the year/month
j writes day of the year as a decimal number (range [001,366]) tm_yday
d writes day of the month as a decimal number (range [01,31]) tm_mday
h equivalent to "%d" tm_mday
e writes day of the month as a decimal number (range [1,31]).

Single digit is preceded by a space.

tm_mday
j writes day of the year as a decimal number (range [001,366]) tm_yday
Day of the week
a writes abbreviated weekday name, e.g. Fri (locale dependent) tm_wday
A writes full weekday name, e.g. Friday (locale dependent) tm_wday
u writes weekday as a decimal number, where Monday is 1 (ISO 8601 format) (range [1-7]) tm_wday
w writes weekday as a decimal number, where Sunday is 0 (range [0-6]) tm_wday
Hour, minute, second
H writes hour as a decimal number, 24 hour clock (range [00-23]) tm_hour
I writes hour as a decimal number, 12 hour clock (range [01,12]) tm_hour
M writes minute as a decimal number (range [00,59]) tm_min
S writes second as a decimal number (range [00,60]) tm_sec
Other
c writes standard date and time string, e.g. Sun Oct 17 04:41:13 2010 (locale dependent) all
x writes localized date representation (locale dependent) all
X writes localized time representation (locale dependent) all
D equivalent to "%m/%d/%y" tm_mon, tm_mday, tm_year
F equivalent to "%Y-%m-%d" (the ISO 8601 date format) tm_mon, tm_mday, tm_year
r writes localized 12-hour clock time (locale dependent) tm_hour, tm_min, tm_sec
R equivalent to "%H:%M" tm_hour, tm_min
T equivalent to "%H:%M:%S" (the ISO 8601 time format) tm_hour, tm_min, tm_sec
p writes localized a.m. or p.m. (locale dependent) tm_hour
z writes offset from UTC in the ISO 8601 format (e.g. -0430), or no characters if the time zone information is not available tm_isdst
Z writes time zone name or abbreviation, or no characters if the time zone information is not available (locale dependent) tm_isdst


[edit] Return value

number of characters put into the character string pointed to by str including the terminating NULL character on success. If the character string was too small, 0 is returned and the contents are undefined.

[edit] Example

[edit] See also

asctime
converts a time_t object to a textual representation
(function)
ctime
converts a tm object to a textual representation
(function)