Blame man3/strftime.3

Packit 7cfc04
.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM)
Packit 7cfc04
.\" Permission is granted to make and distribute verbatim copies of this
Packit 7cfc04
.\" manual provided the copyright notice and this permission notice are
Packit 7cfc04
.\" preserved on all copies.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Permission is granted to copy and distribute modified versions of this
Packit 7cfc04
.\" manual under the conditions for verbatim copying, provided that the
Packit 7cfc04
.\" entire resulting derived work is distributed under the terms of a
Packit 7cfc04
.\" permission notice identical to this one.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Since the Linux kernel and libraries are constantly changing, this
Packit 7cfc04
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
Packit 7cfc04
.\" responsibility for errors or omissions, or for damages resulting from
Packit 7cfc04
.\" the use of the information contained herein.  The author(s) may not
Packit 7cfc04
.\" have taken the same level of care in the production of this manual,
Packit 7cfc04
.\" which is licensed free of charge, as they might when working
Packit 7cfc04
.\" professionally.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Formatted or processed versions of this manual, if unaccompanied by
Packit 7cfc04
.\" the source, must acknowledge the copyright and authors of this work.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\" References consulted:
Packit 7cfc04
.\"     Linux libc source code
Packit 7cfc04
.\"     Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
Packit 7cfc04
.\"     386BSD man pages
Packit 7cfc04
.\"     GNU texinfo documentation on glibc date/time functions.
Packit 7cfc04
.\" Modified Sat Jul 24 18:03:44 1993 by Rik Faith (faith@cs.unc.edu)
Packit 7cfc04
.\" Applied fix by Wolfgang Franke, aeb, 961011
Packit 7cfc04
.\" Corrected return value, aeb, 970307
Packit 7cfc04
.\" Added Single UNIX Spec conversions and %z, aeb/esr, 990329.
Packit 7cfc04
.\" 2005-11-22 mtk, added Glibc Notes covering optional 'flag' and
Packit 7cfc04
.\"           'width' components of conversion specifications.
Packit 7cfc04
.\"
Packit 7cfc04
.TH STRFTIME 3  2017-09-15 "GNU" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
strftime \- format date and time
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.nf
Packit 7cfc04
.B #include <time.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "size_t strftime(char *" s ", size_t " max ", const char *" format ,
Packit 7cfc04
.BI "                const struct tm *" tm );
Packit 7cfc04
.fi
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
The
Packit 7cfc04
.BR strftime ()
Packit 7cfc04
function formats the broken-down time
Packit 7cfc04
.I tm
Packit 7cfc04
according to the format specification
Packit 7cfc04
.I format
Packit 7cfc04
and places the
Packit 7cfc04
result in the character array
Packit 7cfc04
.I s
Packit 7cfc04
of size
Packit 7cfc04
.IR max .
Packit 7cfc04
The broken-down time structure
Packit 7cfc04
.I tm
Packit 7cfc04
is defined in
Packit 7cfc04
.IR <time.h> .
Packit 7cfc04
See also
Packit 7cfc04
.BR ctime (3).
Packit 7cfc04
.\" FIXME . POSIX says: Local timezone information is used as though
Packit 7cfc04
.\" strftime() called tzset().  But this doesn't appear to be the case
Packit 7cfc04
.PP
Packit 7cfc04
The format specification is a null-terminated string and may contain
Packit 7cfc04
special character sequences called
Packit 7cfc04
.IR "conversion specifications",
Packit 7cfc04
each of which is introduced by a \(aq%\(aq character and terminated by
Packit 7cfc04
some other character known as a
Packit 7cfc04
.IR "conversion specifier character".
Packit 7cfc04
All other character sequences are
Packit 7cfc04
.IR "ordinary character sequences".
Packit 7cfc04
.PP
Packit 7cfc04
The characters of ordinary character sequences (including the null byte)
Packit 7cfc04
are copied verbatim from
Packit 7cfc04
.I format
Packit 7cfc04
to
Packit 7cfc04
.IR s .
Packit 7cfc04
However, the characters
Packit 7cfc04
of conversion specifications are replaced as shown in the list below.
Packit 7cfc04
In this list, the field(s) employed from the
Packit 7cfc04
.I tm
Packit 7cfc04
structure are also shown.
Packit 7cfc04
.TP
Packit 7cfc04
.B %a
Packit 7cfc04
The abbreviated name of the day of the week according to the current locale.
Packit 7cfc04
(Calculated from
Packit 7cfc04
.IR tm_wday .)
Packit 7cfc04
.TP
Packit 7cfc04
.B %A
Packit 7cfc04
The full name of the day of the week according to the current locale.
Packit 7cfc04
(Calculated from
Packit 7cfc04
.IR tm_wday .)
Packit 7cfc04
.TP
Packit 7cfc04
.B %b
Packit 7cfc04
The abbreviated month name according to the current locale.
Packit 7cfc04
(Calculated from
Packit 7cfc04
.IR tm_mon .)
Packit 7cfc04
.TP
Packit 7cfc04
.B %B
Packit 7cfc04
The full month name according to the current locale.
Packit 7cfc04
(Calculated from
Packit 7cfc04
.IR tm_mon .)
Packit 7cfc04
.TP
Packit 7cfc04
.B %c
Packit 7cfc04
The preferred date and time representation for the current locale.
Packit 7cfc04
.TP
Packit 7cfc04
.B %C
Packit 7cfc04
The century number (year/100) as a 2-digit integer. (SU)
Packit 7cfc04
(Calculated from
Packit 7cfc04
.IR tm_year .)
Packit 7cfc04
.TP
Packit 7cfc04
.B %d
Packit 7cfc04
The day of the month as a decimal number (range 01 to 31).
Packit 7cfc04
(Calculated from
Packit 7cfc04
.IR tm_mday .)
Packit 7cfc04
.TP
Packit 7cfc04
.B %D
Packit 7cfc04
Equivalent to
Packit 7cfc04
.BR %m/%d/%y .
Packit 7cfc04
(Yecch\(emfor Americans only.
Packit 7cfc04
Americans should note that in other countries
Packit 7cfc04
.B %d/%m/%y
Packit 7cfc04
is rather common.
Packit 7cfc04
This means that in international context this format is
Packit 7cfc04
ambiguous and should not be used.) (SU)
Packit 7cfc04
.TP
Packit 7cfc04
.B %e
Packit 7cfc04
Like
Packit 7cfc04
.BR %d ,
Packit 7cfc04
the day of the month as a decimal number, but a leading
Packit 7cfc04
zero is replaced by a space. (SU)
Packit 7cfc04
(Calculated from
Packit 7cfc04
.IR tm_mday .)
Packit 7cfc04
.TP
Packit 7cfc04
.B %E
Packit 7cfc04
Modifier: use alternative format, see below. (SU)
Packit 7cfc04
.TP
Packit 7cfc04
.B %F
Packit 7cfc04
Equivalent to
Packit 7cfc04
.B %Y-%m-%d
Packit 7cfc04
(the ISO\ 8601 date format). (C99)
Packit 7cfc04
.TP
Packit 7cfc04
.B %G
Packit 7cfc04
The ISO\ 8601 week-based year (see NOTES) with century as a decimal number.
Packit 7cfc04
The 4-digit year corresponding to the ISO week number (see
Packit 7cfc04
.BR %V ).
Packit 7cfc04
This has the same format and value as
Packit 7cfc04
.BR %Y ,
Packit 7cfc04
except that if the ISO week number belongs to the previous or next year,
Packit 7cfc04
that year is used instead. (TZ)
Packit 7cfc04
(Calculated from
Packit 7cfc04
.IR tm_year ,
Packit 7cfc04
.IR tm_yday ,
Packit 7cfc04
and
Packit 7cfc04
.IR tm_wday .)
Packit 7cfc04
.TP
Packit 7cfc04
.B %g
Packit 7cfc04
Like
Packit 7cfc04
.BR %G ,
Packit 7cfc04
but without century, that is, with a 2-digit year (00\(en99). (TZ)
Packit 7cfc04
(Calculated from
Packit 7cfc04
.IR tm_year ,
Packit 7cfc04
.IR tm_yday ,
Packit 7cfc04
and
Packit 7cfc04
.IR tm_wday .)
Packit 7cfc04
.TP
Packit 7cfc04
.B %h
Packit 7cfc04
Equivalent to
Packit 7cfc04
.BR %b .
Packit 7cfc04
(SU)
Packit 7cfc04
.TP
Packit 7cfc04
.B %H
Packit 7cfc04
The hour as a decimal number using a 24-hour clock (range 00 to 23).
Packit 7cfc04
(Calculated from
Packit 7cfc04
.IR tm_hour .)
Packit 7cfc04
.TP
Packit 7cfc04
.B %I
Packit 7cfc04
The hour as a decimal number using a 12-hour clock (range 01 to 12).
Packit 7cfc04
(Calculated from
Packit 7cfc04
.IR tm_hour .)
Packit 7cfc04
.TP
Packit 7cfc04
.B %j
Packit 7cfc04
The day of the year as a decimal number (range 001 to 366).
Packit 7cfc04
(Calculated from
Packit 7cfc04
.IR tm_yday .)
Packit 7cfc04
.TP
Packit 7cfc04
.B %k
Packit 7cfc04
The hour (24-hour clock) as a decimal number (range 0 to 23);
Packit 7cfc04
single digits are preceded by a blank.
Packit 7cfc04
(See also
Packit 7cfc04
.BR %H .)
Packit 7cfc04
(Calculated from
Packit 7cfc04
.IR tm_hour .)
Packit 7cfc04
(TZ)
Packit 7cfc04
.TP
Packit 7cfc04
.B %l
Packit 7cfc04
The hour (12-hour clock) as a decimal number (range 1 to 12);
Packit 7cfc04
single digits are preceded by a blank.
Packit 7cfc04
(See also
Packit 7cfc04
.BR %I .)
Packit 7cfc04
(Calculated from
Packit 7cfc04
.IR tm_hour .)
Packit 7cfc04
(TZ)
Packit 7cfc04
.TP
Packit 7cfc04
.B %m
Packit 7cfc04
The month as a decimal number (range 01 to 12).
Packit 7cfc04
(Calculated from
Packit 7cfc04
.IR tm_mon .)
Packit 7cfc04
.TP
Packit 7cfc04
.B %M
Packit 7cfc04
The minute as a decimal number (range 00 to 59).
Packit 7cfc04
(Calculated from
Packit 7cfc04
.IR tm_min .)
Packit 7cfc04
.TP
Packit 7cfc04
.B %n
Packit 7cfc04
A newline character. (SU)
Packit 7cfc04
.TP
Packit 7cfc04
.B %O
Packit 7cfc04
Modifier: use alternative format, see below. (SU)
Packit 7cfc04
.TP
Packit 7cfc04
.B %p
Packit 7cfc04
Either "AM" or "PM" according to the given time value, or the
Packit 7cfc04
corresponding strings for the current locale.
Packit 7cfc04
Noon is treated as "PM" and midnight as "AM".
Packit 7cfc04
(Calculated from
Packit 7cfc04
.IR tm_hour .)
Packit 7cfc04
.TP
Packit 7cfc04
.B %P
Packit 7cfc04
Like
Packit 7cfc04
.B %p
Packit 7cfc04
but in lowercase: "am" or "pm" or a corresponding
Packit 7cfc04
string for the current locale.
Packit 7cfc04
(Calculated from
Packit 7cfc04
.IR tm_hour .)
Packit 7cfc04
(GNU)
Packit 7cfc04
.TP
Packit 7cfc04
.B %r
Packit 7cfc04
The time in a.m. or p.m. notation.
Packit 7cfc04
In the POSIX locale this is equivalent to
Packit 7cfc04
.BR "%I:%M:%S %p" .
Packit 7cfc04
(SU)
Packit 7cfc04
.TP
Packit 7cfc04
.B %R
Packit 7cfc04
The time in 24-hour notation
Packit 7cfc04
.RB ( %H:%M ).
Packit 7cfc04
(SU)
Packit 7cfc04
For a version including the seconds, see
Packit 7cfc04
.B %T
Packit 7cfc04
below.
Packit 7cfc04
.TP
Packit 7cfc04
.B %s
Packit 7cfc04
The number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). (TZ)
Packit 7cfc04
(Calculated from
Packit 7cfc04
.IR mktime(tm) .)
Packit 7cfc04
.TP
Packit 7cfc04
.B %S
Packit 7cfc04
The second as a decimal number (range 00 to 60).
Packit 7cfc04
(The range is up to 60 to allow for occasional leap seconds.)
Packit 7cfc04
(Calculated from
Packit 7cfc04
.IR tm_sec .)
Packit 7cfc04
.TP
Packit 7cfc04
.B %t
Packit 7cfc04
A tab character. (SU)
Packit 7cfc04
.TP
Packit 7cfc04
.B %T
Packit 7cfc04
The time in 24-hour notation
Packit 7cfc04
.RB ( %H:%M:%S ).
Packit 7cfc04
(SU)
Packit 7cfc04
.TP
Packit 7cfc04
.B %u
Packit 7cfc04
The day of the week as a decimal, range 1 to 7, Monday being 1.
Packit 7cfc04
See also
Packit 7cfc04
.BR %w .
Packit 7cfc04
(Calculated from
Packit 7cfc04
.IR tm_wday .)
Packit 7cfc04
(SU)
Packit 7cfc04
.TP
Packit 7cfc04
.B %U
Packit 7cfc04
The week number of the current year as a decimal number,
Packit 7cfc04
range 00 to 53, starting with the first Sunday as the first day
Packit 7cfc04
of week 01.
Packit 7cfc04
See also
Packit 7cfc04
.B %V
Packit 7cfc04
and
Packit 7cfc04
.BR %W .
Packit 7cfc04
(Calculated from
Packit 7cfc04
.IR tm_yday
Packit 7cfc04
and
Packit 7cfc04
.IR tm_wday .)
Packit 7cfc04
.TP
Packit 7cfc04
.B %V
Packit 7cfc04
The ISO\ 8601 week number (see NOTES) of the current year as a decimal number,
Packit 7cfc04
range 01 to 53, where week 1 is the first week that has at least
Packit 7cfc04
4 days in the new year.
Packit 7cfc04
See also
Packit 7cfc04
.B %U
Packit 7cfc04
and
Packit 7cfc04
.BR %W .
Packit 7cfc04
(Calculated from
Packit 7cfc04
.IR tm_year ,
Packit 7cfc04
.IR tm_yday ,
Packit 7cfc04
and
Packit 7cfc04
.IR tm_wday .)
Packit 7cfc04
(SU)
Packit 7cfc04
.TP
Packit 7cfc04
.B %w
Packit 7cfc04
The day of the week as a decimal, range 0 to 6, Sunday being 0.
Packit 7cfc04
See also
Packit 7cfc04
.BR %u .
Packit 7cfc04
(Calculated from
Packit 7cfc04
.IR tm_wday .)
Packit 7cfc04
.TP
Packit 7cfc04
.B %W
Packit 7cfc04
The week number of the current year as a decimal number,
Packit 7cfc04
range 00 to 53, starting with the first Monday as the first day of week 01.
Packit 7cfc04
(Calculated from
Packit 7cfc04
.IR tm_yday
Packit 7cfc04
and
Packit 7cfc04
.IR tm_wday .)
Packit 7cfc04
.TP
Packit 7cfc04
.B %x
Packit 7cfc04
The preferred date representation for the current locale without the time.
Packit 7cfc04
.TP
Packit 7cfc04
.B %X
Packit 7cfc04
The preferred time representation for the current locale without the date.
Packit 7cfc04
.TP
Packit 7cfc04
.B %y
Packit 7cfc04
The year as a decimal number without a century (range 00 to 99).
Packit 7cfc04
(Calculated from
Packit 7cfc04
.IR tm_year )
Packit 7cfc04
.TP
Packit 7cfc04
.B %Y
Packit 7cfc04
The year as a decimal number including the century.
Packit 7cfc04
(Calculated from
Packit 7cfc04
.IR tm_year )
Packit 7cfc04
.TP
Packit 7cfc04
.B %z
Packit 7cfc04
The
Packit 7cfc04
.I +hhmm
Packit 7cfc04
or
Packit 7cfc04
.I -hhmm
Packit 7cfc04
numeric timezone (that is, the hour and minute offset from UTC). (SU)
Packit 7cfc04
.TP
Packit 7cfc04
.B %Z
Packit 7cfc04
The timezone name or abbreviation.
Packit 7cfc04
.TP
Packit 7cfc04
.B %+
Packit 7cfc04
.\" Nov 05 -- Not in Linux/glibc, but is in some BSDs (according to
Packit 7cfc04
.\" their man pages)
Packit 7cfc04
The date and time in
Packit 7cfc04
.BR date (1)
Packit 7cfc04
format. (TZ)
Packit 7cfc04
(Not supported in glibc2.)
Packit 7cfc04
.TP
Packit 7cfc04
.B %%
Packit 7cfc04
A literal \(aq%\(aq character.
Packit 7cfc04
.PP
Packit 7cfc04
Some conversion specifications can be modified by preceding the
Packit 7cfc04
conversion specifier character by the
Packit 7cfc04
.B E
Packit 7cfc04
or
Packit 7cfc04
.B O
Packit 7cfc04
.I modifier
Packit 7cfc04
to indicate that an alternative format should be used.
Packit 7cfc04
If the alternative format or specification does not exist for
Packit 7cfc04
the current locale, the behavior will be as if the unmodified
Packit 7cfc04
conversion specification were used. (SU)
Packit 7cfc04
The Single UNIX Specification mentions
Packit 7cfc04
.BR %Ec ,
Packit 7cfc04
.BR %EC ,
Packit 7cfc04
.BR %Ex ,
Packit 7cfc04
.BR %EX ,
Packit 7cfc04
.BR %Ey ,
Packit 7cfc04
.BR %EY ,
Packit 7cfc04
.BR %Od ,
Packit 7cfc04
.BR %Oe ,
Packit 7cfc04
.BR %OH ,
Packit 7cfc04
.BR %OI ,
Packit 7cfc04
.BR %Om ,
Packit 7cfc04
.BR %OM ,
Packit 7cfc04
.BR %OS ,
Packit 7cfc04
.BR %Ou ,
Packit 7cfc04
.BR %OU ,
Packit 7cfc04
.BR %OV ,
Packit 7cfc04
.BR %Ow ,
Packit 7cfc04
.BR %OW ,
Packit 7cfc04
.BR %Oy ,
Packit 7cfc04
where the effect of the
Packit 7cfc04
.B O
Packit 7cfc04
modifier is to use
Packit 7cfc04
alternative numeric symbols (say, roman numerals), and that of the
Packit 7cfc04
E modifier is to use a locale-dependent alternative representation.
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
Provided that the result string,
Packit 7cfc04
including the terminating null byte, does not exceed
Packit 7cfc04
.I max
Packit 7cfc04
bytes,
Packit 7cfc04
.BR strftime ()
Packit 7cfc04
returns the number of bytes (excluding the terminating null byte)
Packit 7cfc04
placed in the array
Packit 7cfc04
.IR s .
Packit 7cfc04
If the length of the result string (including the terminating null byte)
Packit 7cfc04
would exceed
Packit 7cfc04
.I max
Packit 7cfc04
bytes, then
Packit 7cfc04
.BR strftime ()
Packit 7cfc04
returns 0, and the contents of the array are undefined.
Packit 7cfc04
.\" (This behavior applies since at least libc 4.4.4;
Packit 7cfc04
.\" very old versions of libc, such as libc 4.4.1,
Packit 7cfc04
.\" would return
Packit 7cfc04
.\" .I max
Packit 7cfc04
.\" if the array was too small.)
Packit 7cfc04
.PP
Packit 7cfc04
Note that the return value 0 does not necessarily indicate an error.
Packit 7cfc04
For example, in many locales
Packit 7cfc04
.B %p
Packit 7cfc04
yields an empty string.
Packit 7cfc04
An empty
Packit 7cfc04
.I format
Packit 7cfc04
string will likewise yield an empty string.
Packit 7cfc04
.SH ENVIRONMENT
Packit 7cfc04
The environment variables
Packit 7cfc04
.B TZ
Packit 7cfc04
and
Packit 7cfc04
.B LC_TIME
Packit 7cfc04
are used.
Packit 7cfc04
.SH ATTRIBUTES
Packit 7cfc04
For an explanation of the terms used in this section, see
Packit 7cfc04
.BR attributes (7).
Packit 7cfc04
.TS
Packit 7cfc04
allbox;
Packit 7cfc04
lb lb lb
Packit 7cfc04
l l l.
Packit 7cfc04
Interface	Attribute	Value
Packit 7cfc04
T{
Packit 7cfc04
.BR strftime ()
Packit 7cfc04
T}	Thread safety	MT-Safe env locale
Packit 7cfc04
.TE
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
SVr4, C89, C99.
Packit 7cfc04
.\" FIXME strftime() is in POSIX.1-2001 and POSIX.1-2008, but the details
Packit 7cfc04
.\" in the standards changed across versions. Investigate and
Packit 7cfc04
.\" write up.
Packit 7cfc04
There are strict inclusions between the set of conversions
Packit 7cfc04
given in ANSI C (unmarked), those given in the Single UNIX Specification
Packit 7cfc04
(marked SU), those given in Olson's timezone package (marked TZ),
Packit 7cfc04
and those given in glibc (marked GNU), except that
Packit 7cfc04
.B %+
Packit 7cfc04
is not supported in glibc2.
Packit 7cfc04
On the other hand glibc2 has several more extensions.
Packit 7cfc04
POSIX.1 only refers to ANSI C; POSIX.2 describes under
Packit 7cfc04
.BR date (1)
Packit 7cfc04
several extensions that could apply to
Packit 7cfc04
.BR strftime ()
Packit 7cfc04
as well.
Packit 7cfc04
The
Packit 7cfc04
.B %F
Packit 7cfc04
conversion is in C99 and POSIX.1-2001.
Packit 7cfc04
.PP
Packit 7cfc04
In SUSv2, the
Packit 7cfc04
.B %S
Packit 7cfc04
specifier allowed a range of 00 to 61,
Packit 7cfc04
to allow for the theoretical possibility of a minute that
Packit 7cfc04
included a double leap second
Packit 7cfc04
(there never has been such a minute).
Packit 7cfc04
.SH NOTES
Packit 7cfc04
.SS ISO 8601 week dates
Packit 7cfc04
.BR %G ,
Packit 7cfc04
.BR %g ,
Packit 7cfc04
and
Packit 7cfc04
.BR %V
Packit 7cfc04
yield values calculated from the week-based year defined by the
Packit 7cfc04
ISO\ 8601 standard.
Packit 7cfc04
In this system, weeks start on a Monday, and are numbered from 01,
Packit 7cfc04
for the first week, up to 52 or 53, for the last week.
Packit 7cfc04
Week 1 is the first week where four or more days fall within the
Packit 7cfc04
new year (or, synonymously, week 01 is:
Packit 7cfc04
the first week of the year that contains a Thursday;
Packit 7cfc04
or, the week that has 4 January in it).
Packit 7cfc04
When three of fewer days of the first calendar week of the new year fall
Packit 7cfc04
within that year,
Packit 7cfc04
then the ISO 8601 week-based system counts those days as part of week 53
Packit 7cfc04
of the preceding year.
Packit 7cfc04
For example, 1 January 2010 is a Friday,
Packit 7cfc04
meaning that just three days of that calendar week fall in 2010.
Packit 7cfc04
Thus, the ISO\ 8601 week-based system considers these days to be part of
Packit 7cfc04
week 53
Packit 7cfc04
.RB ( %V )
Packit 7cfc04
of the year 2009
Packit 7cfc04
.RB ( %G );
Packit 7cfc04
week 01 of ISO\ 8601 year 2010 starts on Monday, 4 January 2010.
Packit 7cfc04
.SS Glibc notes
Packit 7cfc04
Glibc provides some extensions for conversion specifications.
Packit 7cfc04
(These extensions are not specified in POSIX.1-2001, but a few other
Packit 7cfc04
systems provide similar features.)
Packit 7cfc04
.\" HP-UX and Tru64 also have features like this.
Packit 7cfc04
Between the \(aq%\(aq character and the conversion specifier character,
Packit 7cfc04
an optional
Packit 7cfc04
.I flag
Packit 7cfc04
and field
Packit 7cfc04
.I width
Packit 7cfc04
may be specified.
Packit 7cfc04
(These precede the
Packit 7cfc04
.B E
Packit 7cfc04
or
Packit 7cfc04
.B O
Packit 7cfc04
modifiers, if present.)
Packit 7cfc04
.PP
Packit 7cfc04
The following flag characters are permitted:
Packit 7cfc04
.TP
Packit 7cfc04
.B _
Packit 7cfc04
(underscore)
Packit 7cfc04
Pad a numeric result string with spaces.
Packit 7cfc04
.TP
Packit 7cfc04
.B \-
Packit 7cfc04
(dash)
Packit 7cfc04
Do not pad a numeric result string.
Packit 7cfc04
.TP
Packit 7cfc04
.B 0
Packit 7cfc04
Pad a numeric result string with zeros even if the conversion
Packit 7cfc04
specifier character uses space-padding by default.
Packit 7cfc04
.TP
Packit 7cfc04
.B ^
Packit 7cfc04
Convert alphabetic characters in result string to uppercase.
Packit 7cfc04
.TP
Packit 7cfc04
.B #
Packit 7cfc04
Swap the case of the result string.
Packit 7cfc04
(This flag works only with certain conversion specifier characters,
Packit 7cfc04
and of these, it is only really useful with
Packit 7cfc04
.BR %Z .)
Packit 7cfc04
.PP
Packit 7cfc04
An optional decimal width specifier may follow the (possibly absent) flag.
Packit 7cfc04
If the natural size of the field is smaller than this width,
Packit 7cfc04
then the result string is padded (on the left) to the specified width.
Packit 7cfc04
.SH BUGS
Packit 7cfc04
If the output string would exceed
Packit 7cfc04
.I max
Packit 7cfc04
bytes,
Packit 7cfc04
.I errno
Packit 7cfc04
is
Packit 7cfc04
.I not
Packit 7cfc04
set.
Packit 7cfc04
This makes it impossible to distinguish this error case from cases where the
Packit 7cfc04
.I format
Packit 7cfc04
string legitimately produces a zero-length output string.
Packit 7cfc04
POSIX.1-2001
Packit 7cfc04
does
Packit 7cfc04
.I not
Packit 7cfc04
specify any
Packit 7cfc04
.I errno
Packit 7cfc04
settings for
Packit 7cfc04
.BR strftime ().
Packit 7cfc04
.PP
Packit 7cfc04
Some buggy versions of
Packit 7cfc04
.BR gcc (1)
Packit 7cfc04
complain about the use of
Packit 7cfc04
.BR %c :
Packit 7cfc04
.IR "warning: `%c' yields only last 2 digits of year in some locales" .
Packit 7cfc04
Of course programmers are encouraged to use
Packit 7cfc04
.BR %c ,
Packit 7cfc04
it gives the preferred date and time representation.
Packit 7cfc04
One meets all kinds of strange obfuscations
Packit 7cfc04
to circumvent this
Packit 7cfc04
.BR gcc (1)
Packit 7cfc04
problem.
Packit 7cfc04
A relatively clean one is to add an
Packit 7cfc04
intermediate function
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
size_t
Packit 7cfc04
my_strftime(char *s, size_t max, const char *fmt,
Packit 7cfc04
            const struct tm *tm)
Packit 7cfc04
{
Packit 7cfc04
    return strftime(s, max, fmt, tm);
Packit 7cfc04
}
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
Nowadays,
Packit 7cfc04
.BR gcc (1)
Packit 7cfc04
provides the
Packit 7cfc04
.IR \-Wno\-format\-y2k
Packit 7cfc04
option to prevent the warning,
Packit 7cfc04
so that the above workaround is no longer required.
Packit 7cfc04
.SH EXAMPLE
Packit 7cfc04
.BR "RFC\ 2822-compliant date format"
Packit 7cfc04
(with an English locale for %a and %b)
Packit 7cfc04
.PP
Packit 7cfc04
.in +2n
Packit 7cfc04
"%a,\ %d\ %b\ %Y\ %T\ %z"
Packit 7cfc04
.PP
Packit 7cfc04
.BR "RFC\ 822-compliant date format"
Packit 7cfc04
(with an English locale for %a and %b)
Packit 7cfc04
.PP
Packit 7cfc04
.in +2n
Packit 7cfc04
"%a,\ %d\ %b\ %y\ %T\ %z"
Packit 7cfc04
.SS Example program
Packit 7cfc04
The program below can be used to experiment with
Packit 7cfc04
.BR strftime ().
Packit 7cfc04
.PP
Packit 7cfc04
Some examples of the result string produced by the glibc implementation of
Packit 7cfc04
.BR strftime ()
Packit 7cfc04
are as follows:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
.RB "$" " ./a.out \(aq%m\(aq"
Packit 7cfc04
Result string is "11"
Packit 7cfc04
.RB "$" " ./a.out \(aq%5m\(aq"
Packit 7cfc04
Result string is "00011"
Packit 7cfc04
.RB "$" " ./a.out \(aq%_5m\(aq"
Packit 7cfc04
Result string is "   11"
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.SS Program source
Packit 7cfc04
\&
Packit 7cfc04
.EX
Packit 7cfc04
#include <time.h>
Packit 7cfc04
#include <stdio.h>
Packit 7cfc04
#include <stdlib.h>
Packit 7cfc04
Packit 7cfc04
int
Packit 7cfc04
main(int argc, char *argv[])
Packit 7cfc04
{
Packit 7cfc04
    char outstr[200];
Packit 7cfc04
    time_t t;
Packit 7cfc04
    struct tm *tmp;
Packit 7cfc04
Packit 7cfc04
    t = time(NULL);
Packit 7cfc04
    tmp = localtime(&t);
Packit 7cfc04
    if (tmp == NULL) {
Packit 7cfc04
        perror("localtime");
Packit 7cfc04
        exit(EXIT_FAILURE);
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    if (strftime(outstr, sizeof(outstr), argv[1], tmp) == 0) {
Packit 7cfc04
        fprintf(stderr, "strftime returned 0");
Packit 7cfc04
        exit(EXIT_FAILURE);
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    printf("Result string is \\"%s\\"\\n", outstr);
Packit 7cfc04
    exit(EXIT_SUCCESS);
Packit 7cfc04
}
Packit 7cfc04
.EE
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR date (1),
Packit 7cfc04
.BR time (2),
Packit 7cfc04
.BR ctime (3),
Packit 7cfc04
.BR setlocale (3),
Packit 7cfc04
.BR sprintf (3),
Packit 7cfc04
.BR strptime (3)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.