919 lines
18 KiB
HTML
919 lines
18 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of STRFTIME</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>STRFTIME</H1>
|
|
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
strftime - format date and time
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/time.h">time.h</A>></B>
|
|
|
|
<B>size_t strftime(char *</B><I>s</I><B>, size_t </B><I>max</I><B>, const char *</B><I>format</I><B>,</B>
|
|
<B> const struct tm *</B><I>tm</I><B>);</B>
|
|
</PRE>
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
The
|
|
<B>strftime</B>()
|
|
|
|
function formats the broken-down time
|
|
<I>tm</I>
|
|
|
|
according to the format specification
|
|
<I>format</I>
|
|
|
|
and places the
|
|
result in the character array
|
|
<I>s</I>
|
|
|
|
of size
|
|
<I>max</I>.
|
|
|
|
The broken-down time structure
|
|
<I>tm</I>
|
|
|
|
is defined in
|
|
<I><<A HREF="file:///usr/include/time.h">time.h</A>></I>.
|
|
|
|
See also
|
|
<B><A HREF="/cgi-bin/man/man2html?3+ctime">ctime</A></B>(3).
|
|
|
|
|
|
|
|
<P>
|
|
|
|
The format specification is a null-terminated string and may contain
|
|
special character sequences called
|
|
<I>conversion specifications,</I>
|
|
|
|
each of which is introduced by a '%' character and terminated by
|
|
some other character known as a
|
|
<I>conversion specifier character.</I>
|
|
|
|
All other character sequences are
|
|
<I>ordinary character sequences.</I>
|
|
|
|
<P>
|
|
|
|
The characters of ordinary character sequences (including the null byte)
|
|
are copied verbatim from
|
|
<I>format</I>
|
|
|
|
to
|
|
<I>s</I>.
|
|
|
|
However, the characters
|
|
of conversion specifications are replaced as shown in the list below.
|
|
In this list, the field(s) employed from the
|
|
<I>tm</I>
|
|
|
|
structure are also shown.
|
|
<DL COMPACT>
|
|
<DT id="1"><B>%a</B>
|
|
|
|
<DD>
|
|
The abbreviated name of the day of the week according to the current locale.
|
|
(Calculated from
|
|
<I>tm_wday</I>.)
|
|
|
|
<DT id="2"><B>%A</B>
|
|
|
|
<DD>
|
|
The full name of the day of the week according to the current locale.
|
|
(Calculated from
|
|
<I>tm_wday</I>.)
|
|
|
|
<DT id="3"><B>%b</B>
|
|
|
|
<DD>
|
|
The abbreviated month name according to the current locale.
|
|
(Calculated from
|
|
<I>tm_mon</I>.)
|
|
|
|
<DT id="4"><B>%B</B>
|
|
|
|
<DD>
|
|
The full month name according to the current locale.
|
|
(Calculated from
|
|
<I>tm_mon</I>.)
|
|
|
|
<DT id="5"><B>%c</B>
|
|
|
|
<DD>
|
|
The preferred date and time representation for the current locale.
|
|
<DT id="6"><B>%C</B>
|
|
|
|
<DD>
|
|
The century number (year/100) as a 2-digit integer. (SU)
|
|
(Calculated from
|
|
<I>tm_year</I>.)
|
|
|
|
<DT id="7"><B>%d</B>
|
|
|
|
<DD>
|
|
The day of the month as a decimal number (range 01 to 31).
|
|
(Calculated from
|
|
<I>tm_mday</I>.)
|
|
|
|
<DT id="8"><B>%D</B>
|
|
|
|
<DD>
|
|
Equivalent to
|
|
<B>%m/%d/%y</B>.
|
|
|
|
(Yecch---for Americans only.
|
|
Americans should note that in other countries
|
|
<B>%d/%m/%y</B>
|
|
|
|
is rather common.
|
|
This means that in international context this format is
|
|
ambiguous and should not be used.) (SU)
|
|
<DT id="9"><B>%e</B>
|
|
|
|
<DD>
|
|
Like
|
|
<B>%d</B>,
|
|
|
|
the day of the month as a decimal number, but a leading
|
|
zero is replaced by a space. (SU)
|
|
(Calculated from
|
|
<I>tm_mday</I>.)
|
|
|
|
<DT id="10"><B>%E</B>
|
|
|
|
<DD>
|
|
Modifier: use alternative format, see below. (SU)
|
|
<DT id="11"><B>%F</B>
|
|
|
|
<DD>
|
|
Equivalent to
|
|
<B>%Y-%m-%d</B>
|
|
|
|
(the ISO 8601 date format). (C99)
|
|
<DT id="12"><B>%G</B>
|
|
|
|
<DD>
|
|
The ISO 8601 week-based year (see NOTES) with century as a decimal number.
|
|
The 4-digit year corresponding to the ISO week number (see
|
|
<B>%V</B>).
|
|
|
|
This has the same format and value as
|
|
<B>%Y</B>,
|
|
|
|
except that if the ISO week number belongs to the previous or next year,
|
|
that year is used instead. (TZ)
|
|
(Calculated from
|
|
<I>tm_year</I>,
|
|
|
|
<I>tm_yday</I>,
|
|
|
|
and
|
|
<I>tm_wday</I>.)
|
|
|
|
<DT id="13"><B>%g</B>
|
|
|
|
<DD>
|
|
Like
|
|
<B>%G</B>,
|
|
|
|
but without century, that is, with a 2-digit year (00-99). (TZ)
|
|
(Calculated from
|
|
<I>tm_year</I>,
|
|
|
|
<I>tm_yday</I>,
|
|
|
|
and
|
|
<I>tm_wday</I>.)
|
|
|
|
<DT id="14"><B>%h</B>
|
|
|
|
<DD>
|
|
Equivalent to
|
|
<B>%b</B>.
|
|
|
|
(SU)
|
|
<DT id="15"><B>%H</B>
|
|
|
|
<DD>
|
|
The hour as a decimal number using a 24-hour clock (range 00 to 23).
|
|
(Calculated from
|
|
<I>tm_hour</I>.)
|
|
|
|
<DT id="16"><B>%I</B>
|
|
|
|
<DD>
|
|
The hour as a decimal number using a 12-hour clock (range 01 to 12).
|
|
(Calculated from
|
|
<I>tm_hour</I>.)
|
|
|
|
<DT id="17"><B>%j</B>
|
|
|
|
<DD>
|
|
The day of the year as a decimal number (range 001 to 366).
|
|
(Calculated from
|
|
<I>tm_yday</I>.)
|
|
|
|
<DT id="18"><B>%k</B>
|
|
|
|
<DD>
|
|
The hour (24-hour clock) as a decimal number (range 0 to 23);
|
|
single digits are preceded by a blank.
|
|
(See also
|
|
<B>%H</B>.)
|
|
|
|
(Calculated from
|
|
<I>tm_hour</I>.)
|
|
|
|
(TZ)
|
|
<DT id="19"><B>%l</B>
|
|
|
|
<DD>
|
|
The hour (12-hour clock) as a decimal number (range 1 to 12);
|
|
single digits are preceded by a blank.
|
|
(See also
|
|
<B>%I</B>.)
|
|
|
|
(Calculated from
|
|
<I>tm_hour</I>.)
|
|
|
|
(TZ)
|
|
<DT id="20"><B>%m</B>
|
|
|
|
<DD>
|
|
The month as a decimal number (range 01 to 12).
|
|
(Calculated from
|
|
<I>tm_mon</I>.)
|
|
|
|
<DT id="21"><B>%M</B>
|
|
|
|
<DD>
|
|
The minute as a decimal number (range 00 to 59).
|
|
(Calculated from
|
|
<I>tm_min</I>.)
|
|
|
|
<DT id="22"><B>%n</B>
|
|
|
|
<DD>
|
|
A newline character. (SU)
|
|
<DT id="23"><B>%O</B>
|
|
|
|
<DD>
|
|
Modifier: use alternative format, see below. (SU)
|
|
<DT id="24"><B>%p</B>
|
|
|
|
<DD>
|
|
Either "AM" or "PM" according to the given time value, or the
|
|
corresponding strings for the current locale.
|
|
Noon is treated as "PM" and midnight as "AM".
|
|
(Calculated from
|
|
<I>tm_hour</I>.)
|
|
|
|
<DT id="25"><B>%P</B>
|
|
|
|
<DD>
|
|
Like
|
|
<B>%p</B>
|
|
|
|
but in lowercase: "am" or "pm" or a corresponding
|
|
string for the current locale.
|
|
(Calculated from
|
|
<I>tm_hour</I>.)
|
|
|
|
(GNU)
|
|
<DT id="26"><B>%r</B>
|
|
|
|
<DD>
|
|
The time in a.m. or p.m. notation.
|
|
In the POSIX locale this is equivalent to
|
|
<B>%I:%M:%S %p</B>.
|
|
|
|
(SU)
|
|
<DT id="27"><B>%R</B>
|
|
|
|
<DD>
|
|
The time in 24-hour notation
|
|
(<B>%H:%M</B>).
|
|
|
|
(SU)
|
|
For a version including the seconds, see
|
|
<B>%T</B>
|
|
|
|
below.
|
|
<DT id="28"><B>%s</B>
|
|
|
|
<DD>
|
|
The number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). (TZ)
|
|
(Calculated from
|
|
<I>mktime(tm)</I>.)
|
|
|
|
<DT id="29"><B>%S</B>
|
|
|
|
<DD>
|
|
The second as a decimal number (range 00 to 60).
|
|
(The range is up to 60 to allow for occasional leap seconds.)
|
|
(Calculated from
|
|
<I>tm_sec</I>.)
|
|
|
|
<DT id="30"><B>%t</B>
|
|
|
|
<DD>
|
|
A tab character. (SU)
|
|
<DT id="31"><B>%T</B>
|
|
|
|
<DD>
|
|
The time in 24-hour notation
|
|
(<B>%H:%M:%S</B>).
|
|
|
|
(SU)
|
|
<DT id="32"><B>%u</B>
|
|
|
|
<DD>
|
|
The day of the week as a decimal, range 1 to 7, Monday being 1.
|
|
See also
|
|
<B>%w</B>.
|
|
|
|
(Calculated from
|
|
<I>tm_wday</I>.)
|
|
|
|
(SU)
|
|
<DT id="33"><B>%U</B>
|
|
|
|
<DD>
|
|
The week number of the current year as a decimal number,
|
|
range 00 to 53, starting with the first Sunday as the first day
|
|
of week 01.
|
|
See also
|
|
<B>%V</B>
|
|
|
|
and
|
|
<B>%W</B>.
|
|
|
|
(Calculated from
|
|
<I>tm_yday</I>
|
|
|
|
and
|
|
<I>tm_wday</I>.)
|
|
|
|
<DT id="34"><B>%V</B>
|
|
|
|
<DD>
|
|
The ISO 8601 week number (see NOTES) of the current year as a decimal number,
|
|
range 01 to 53, where week 1 is the first week that has at least
|
|
4 days in the new year.
|
|
See also
|
|
<B>%U</B>
|
|
|
|
and
|
|
<B>%W</B>.
|
|
|
|
(Calculated from
|
|
<I>tm_year</I>,
|
|
|
|
<I>tm_yday</I>,
|
|
|
|
and
|
|
<I>tm_wday</I>.)
|
|
|
|
(SU)
|
|
<DT id="35"><B>%w</B>
|
|
|
|
<DD>
|
|
The day of the week as a decimal, range 0 to 6, Sunday being 0.
|
|
See also
|
|
<B>%u</B>.
|
|
|
|
(Calculated from
|
|
<I>tm_wday</I>.)
|
|
|
|
<DT id="36"><B>%W</B>
|
|
|
|
<DD>
|
|
The week number of the current year as a decimal number,
|
|
range 00 to 53, starting with the first Monday as the first day of week 01.
|
|
(Calculated from
|
|
<I>tm_yday</I>
|
|
|
|
and
|
|
<I>tm_wday</I>.)
|
|
|
|
<DT id="37"><B>%x</B>
|
|
|
|
<DD>
|
|
The preferred date representation for the current locale without the time.
|
|
<DT id="38"><B>%X</B>
|
|
|
|
<DD>
|
|
The preferred time representation for the current locale without the date.
|
|
<DT id="39"><B>%y</B>
|
|
|
|
<DD>
|
|
The year as a decimal number without a century (range 00 to 99).
|
|
(Calculated from
|
|
<I>tm_year</I>)
|
|
|
|
<DT id="40"><B>%Y</B>
|
|
|
|
<DD>
|
|
The year as a decimal number including the century.
|
|
(Calculated from
|
|
<I>tm_year</I>)
|
|
|
|
<DT id="41"><B>%z</B>
|
|
|
|
<DD>
|
|
The
|
|
<I>+hhmm</I>
|
|
|
|
or
|
|
<I>-hhmm</I>
|
|
|
|
numeric timezone (that is, the hour and minute offset from UTC). (SU)
|
|
<DT id="42"><B>%Z</B>
|
|
|
|
<DD>
|
|
The timezone name or abbreviation.
|
|
<DT id="43"><B>%+</B>
|
|
|
|
<DD>
|
|
|
|
|
|
The date and time in
|
|
<B><A HREF="/cgi-bin/man/man2html?1+date">date</A></B>(1)
|
|
|
|
format. (TZ)
|
|
(Not supported in glibc2.)
|
|
<DT id="44"><B>%%</B>
|
|
|
|
<DD>
|
|
A literal '%' character.
|
|
</DL>
|
|
<P>
|
|
|
|
Some conversion specifications can be modified by preceding the
|
|
conversion specifier character by the
|
|
<B>E</B>
|
|
|
|
or
|
|
<B>O</B>
|
|
|
|
<I>modifier</I>
|
|
|
|
to indicate that an alternative format should be used.
|
|
If the alternative format or specification does not exist for
|
|
the current locale, the behavior will be as if the unmodified
|
|
conversion specification were used. (SU)
|
|
The Single UNIX Specification mentions
|
|
<B>%Ec</B>,
|
|
|
|
<B>%EC</B>,
|
|
|
|
<B>%Ex</B>,
|
|
|
|
<B>%EX</B>,
|
|
|
|
<B>%Ey</B>,
|
|
|
|
<B>%EY</B>,
|
|
|
|
<B>%Od</B>,
|
|
|
|
<B>%Oe</B>,
|
|
|
|
<B>%OH</B>,
|
|
|
|
<B>%OI</B>,
|
|
|
|
<B>%Om</B>,
|
|
|
|
<B>%OM</B>,
|
|
|
|
<B>%OS</B>,
|
|
|
|
<B>%Ou</B>,
|
|
|
|
<B>%OU</B>,
|
|
|
|
<B>%OV</B>,
|
|
|
|
<B>%Ow</B>,
|
|
|
|
<B>%OW</B>,
|
|
|
|
<B>%Oy</B>,
|
|
|
|
where the effect of the
|
|
<B>O</B>
|
|
|
|
modifier is to use
|
|
alternative numeric symbols (say, roman numerals), and that of the
|
|
E modifier is to use a locale-dependent alternative representation.
|
|
<A NAME="lbAE"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
Provided that the result string,
|
|
including the terminating null byte, does not exceed
|
|
<I>max</I>
|
|
|
|
bytes,
|
|
<B>strftime</B>()
|
|
|
|
returns the number of bytes (excluding the terminating null byte)
|
|
placed in the array
|
|
<I>s</I>.
|
|
|
|
If the length of the result string (including the terminating null byte)
|
|
would exceed
|
|
<I>max</I>
|
|
|
|
bytes, then
|
|
<B>strftime</B>()
|
|
|
|
returns 0, and the contents of the array are undefined.
|
|
|
|
|
|
|
|
|
|
|
|
<P>
|
|
|
|
Note that the return value 0 does not necessarily indicate an error.
|
|
For example, in many locales
|
|
<B>%p</B>
|
|
|
|
yields an empty string.
|
|
An empty
|
|
<I>format</I>
|
|
|
|
string will likewise yield an empty string.
|
|
<A NAME="lbAF"> </A>
|
|
<H2>ENVIRONMENT</H2>
|
|
|
|
The environment variables
|
|
<B>TZ</B>
|
|
|
|
and
|
|
<B>LC_TIME</B>
|
|
|
|
are used.
|
|
<A NAME="lbAG"> </A>
|
|
<H2>ATTRIBUTES</H2>
|
|
|
|
For an explanation of the terms used in this section, see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
|
|
|
|
<TABLE BORDER>
|
|
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
|
|
<TR VALIGN=top><TD>
|
|
<B>strftime</B>()
|
|
|
|
</TD><TD>Thread safety</TD><TD>MT-Safe env locale<BR></TD></TR>
|
|
</TABLE>
|
|
|
|
<A NAME="lbAH"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
SVr4, C89, C99.
|
|
|
|
|
|
|
|
There are strict inclusions between the set of conversions
|
|
given in ANSI C (unmarked), those given in the Single UNIX Specification
|
|
(marked SU), those given in Olson's timezone package (marked TZ),
|
|
and those given in glibc (marked GNU), except that
|
|
<B>%+</B>
|
|
|
|
is not supported in glibc2.
|
|
On the other hand glibc2 has several more extensions.
|
|
POSIX.1 only refers to ANSI C; POSIX.2 describes under
|
|
<B><A HREF="/cgi-bin/man/man2html?1+date">date</A></B>(1)
|
|
|
|
several extensions that could apply to
|
|
<B>strftime</B>()
|
|
|
|
as well.
|
|
The
|
|
<B>%F</B>
|
|
|
|
conversion is in C99 and POSIX.1-2001.
|
|
<P>
|
|
|
|
In SUSv2, the
|
|
<B>%S</B>
|
|
|
|
specifier allowed a range of 00 to 61,
|
|
to allow for the theoretical possibility of a minute that
|
|
included a double leap second
|
|
(there never has been such a minute).
|
|
<A NAME="lbAI"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
<A NAME="lbAJ"> </A>
|
|
<H3>ISO 8601 week dates</H3>
|
|
|
|
<B>%G</B>,
|
|
|
|
<B>%g</B>,
|
|
|
|
and
|
|
<B>%V</B>
|
|
|
|
yield values calculated from the week-based year defined by the
|
|
ISO 8601 standard.
|
|
In this system, weeks start on a Monday, and are numbered from 01,
|
|
for the first week, up to 52 or 53, for the last week.
|
|
Week 1 is the first week where four or more days fall within the
|
|
new year (or, synonymously, week 01 is:
|
|
the first week of the year that contains a Thursday;
|
|
or, the week that has 4 January in it).
|
|
When three of fewer days of the first calendar week of the new year fall
|
|
within that year,
|
|
then the ISO 8601 week-based system counts those days as part of week 53
|
|
of the preceding year.
|
|
For example, 1 January 2010 is a Friday,
|
|
meaning that just three days of that calendar week fall in 2010.
|
|
Thus, the ISO 8601 week-based system considers these days to be part of
|
|
week 53
|
|
(<B>%V</B>)
|
|
|
|
of the year 2009
|
|
(<B>%G</B>);
|
|
|
|
week 01 of ISO 8601 year 2010 starts on Monday, 4 January 2010.
|
|
<A NAME="lbAK"> </A>
|
|
<H3>Glibc notes</H3>
|
|
|
|
Glibc provides some extensions for conversion specifications.
|
|
(These extensions are not specified in POSIX.1-2001, but a few other
|
|
systems provide similar features.)
|
|
|
|
Between the '%' character and the conversion specifier character,
|
|
an optional
|
|
<I>flag</I>
|
|
|
|
and field
|
|
<I>width</I>
|
|
|
|
may be specified.
|
|
(These precede the
|
|
<B>E</B>
|
|
|
|
or
|
|
<B>O</B>
|
|
|
|
modifiers, if present.)
|
|
<P>
|
|
|
|
The following flag characters are permitted:
|
|
<DL COMPACT>
|
|
<DT id="45"><B>_</B>
|
|
|
|
<DD>
|
|
(underscore)
|
|
Pad a numeric result string with spaces.
|
|
<DT id="46"><B>-</B>
|
|
|
|
<DD>
|
|
(dash)
|
|
Do not pad a numeric result string.
|
|
<DT id="47"><B>0</B>
|
|
|
|
<DD>
|
|
Pad a numeric result string with zeros even if the conversion
|
|
specifier character uses space-padding by default.
|
|
<DT id="48"><B>^</B>
|
|
|
|
<DD>
|
|
Convert alphabetic characters in result string to uppercase.
|
|
<DT id="49"><B>#</B>
|
|
|
|
<DD>
|
|
Swap the case of the result string.
|
|
(This flag works only with certain conversion specifier characters,
|
|
and of these, it is only really useful with
|
|
<B>%Z</B>.)
|
|
|
|
</DL>
|
|
<P>
|
|
|
|
An optional decimal width specifier may follow the (possibly absent) flag.
|
|
If the natural size of the field is smaller than this width,
|
|
then the result string is padded (on the left) to the specified width.
|
|
<A NAME="lbAL"> </A>
|
|
<H2>BUGS</H2>
|
|
|
|
If the output string would exceed
|
|
<I>max</I>
|
|
|
|
bytes,
|
|
<I>errno</I>
|
|
|
|
is
|
|
<I>not</I>
|
|
|
|
set.
|
|
This makes it impossible to distinguish this error case from cases where the
|
|
<I>format</I>
|
|
|
|
string legitimately produces a zero-length output string.
|
|
POSIX.1-2001
|
|
does
|
|
<I>not</I>
|
|
|
|
specify any
|
|
<I>errno</I>
|
|
|
|
settings for
|
|
<B>strftime</B>().
|
|
|
|
<P>
|
|
|
|
Some buggy versions of
|
|
<B><A HREF="/cgi-bin/man/man2html?1+gcc">gcc</A></B>(1)
|
|
|
|
complain about the use of
|
|
<B>%c</B>:
|
|
|
|
<I>warning: `%c' yields only last 2 digits of year in some locales</I>.
|
|
|
|
Of course programmers are encouraged to use
|
|
<B>%c</B>,
|
|
|
|
as it gives the preferred date and time representation.
|
|
One meets all kinds of strange obfuscations
|
|
to circumvent this
|
|
<B><A HREF="/cgi-bin/man/man2html?1+gcc">gcc</A></B>(1)
|
|
|
|
problem.
|
|
A relatively clean one is to add an
|
|
intermediate function
|
|
<P>
|
|
|
|
|
|
|
|
size_t
|
|
my_strftime(char *s, size_t max, const char *fmt,
|
|
<BR> const struct tm *tm)
|
|
{
|
|
<BR> return strftime(s, max, fmt, tm);
|
|
}
|
|
|
|
|
|
<P>
|
|
|
|
Nowadays,
|
|
<B><A HREF="/cgi-bin/man/man2html?1+gcc">gcc</A></B>(1)
|
|
|
|
provides the
|
|
<I>-Wno-format-y2k</I>
|
|
|
|
option to prevent the warning,
|
|
so that the above workaround is no longer required.
|
|
<A NAME="lbAM"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
<B>RFC 2822-compliant date format</B>
|
|
|
|
(with an English locale for %a and %b)
|
|
<P>
|
|
|
|
|
|
"%a, %d %b %Y %T %z"
|
|
<P>
|
|
|
|
<B>RFC 822-compliant date format</B>
|
|
|
|
(with an English locale for %a and %b)
|
|
<P>
|
|
|
|
|
|
"%a, %d %b %y %T %z"
|
|
<A NAME="lbAN"> </A>
|
|
<H3>Example program</H3>
|
|
|
|
The program below can be used to experiment with
|
|
<B>strftime</B>().
|
|
|
|
<P>
|
|
|
|
Some examples of the result string produced by the glibc implementation of
|
|
<B>strftime</B>()
|
|
|
|
are as follows:
|
|
<P>
|
|
|
|
|
|
|
|
$<B> ./a.out '%m'</B>
|
|
|
|
Result string is "11"
|
|
$<B> ./a.out '%5m'</B>
|
|
|
|
Result string is "00011"
|
|
$<B> ./a.out '%_5m'</B>
|
|
|
|
Result string is " 11"
|
|
|
|
|
|
<A NAME="lbAO"> </A>
|
|
<H3>Program source</H3>
|
|
|
|
|
|
|
|
#include <<A HREF="file:///usr/include/time.h">time.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
<BR> char outstr[200];
|
|
<BR> time_t t;
|
|
<BR> struct tm *tmp;
|
|
<P>
|
|
<BR> t = time(NULL);
|
|
<BR> tmp = localtime(&t);
|
|
<BR> if (tmp == NULL) {
|
|
<BR> perror("localtime");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> if (strftime(outstr, sizeof(outstr), argv[1], tmp) == 0) {
|
|
<BR> fprintf(stderr, "strftime returned 0");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> printf("Result string is \"%s\"\n", outstr);
|
|
<BR> exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
<A NAME="lbAP"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?1+date">date</A></B>(1),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+time">time</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+ctime">ctime</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+setlocale">setlocale</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+sprintf">sprintf</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+strptime">strptime</A></B>(3)
|
|
|
|
<A NAME="lbAQ"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="50"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="51"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="52"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="53"><A HREF="#lbAE">RETURN VALUE</A><DD>
|
|
<DT id="54"><A HREF="#lbAF">ENVIRONMENT</A><DD>
|
|
<DT id="55"><A HREF="#lbAG">ATTRIBUTES</A><DD>
|
|
<DT id="56"><A HREF="#lbAH">CONFORMING TO</A><DD>
|
|
<DT id="57"><A HREF="#lbAI">NOTES</A><DD>
|
|
<DL>
|
|
<DT id="58"><A HREF="#lbAJ">ISO 8601 week dates</A><DD>
|
|
<DT id="59"><A HREF="#lbAK">Glibc notes</A><DD>
|
|
</DL>
|
|
<DT id="60"><A HREF="#lbAL">BUGS</A><DD>
|
|
<DT id="61"><A HREF="#lbAM">EXAMPLE</A><DD>
|
|
<DL>
|
|
<DT id="62"><A HREF="#lbAN">Example program</A><DD>
|
|
<DT id="63"><A HREF="#lbAO">Program source</A><DD>
|
|
</DL>
|
|
<DT id="64"><A HREF="#lbAP">SEE ALSO</A><DD>
|
|
<DT id="65"><A HREF="#lbAQ">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:58 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|