Files
oldlinux-files/Ref-docs/POSIX/susv3/functions/strftime.html
2024-02-19 00:21:47 -05:00

531 lines
20 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 -->
<!-- Copyright (c) 2001 The Open Group, All Rights Reserved -->
<title>strftime</title>
</head>
<body bgcolor="white">
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
</script>
<basefont size="3"> <a name="strftime"></a> <a name="tag_03_729"></a><!-- strftime -->
<!--header start-->
<center><font size="2">The Open Group Base Specifications Issue 6<br>
IEEE Std 1003.1-2001<br>
Copyright &copy; 2001 The IEEE and The Open Group, All Rights reserved.</font></center>
<!--header end-->
<hr size="2" noshade>
<h4><a name="tag_03_729_01"></a>NAME</h4>
<blockquote>strftime - convert date and time to a string</blockquote>
<h4><a name="tag_03_729_02"></a>SYNOPSIS</h4>
<blockquote class="synopsis">
<p><code><tt>#include &lt;<a href="../basedefs/time.h.html">time.h</a>&gt;<br>
<br>
size_t strftime(char *restrict</tt> <i>s</i><tt>, size_t</tt> <i>maxsize</i><tt>,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const char *restrict</tt> <i>format</i><tt>, const struct tm *restrict</tt>
<i>timeptr</i><tt>);<br>
</tt></code></p>
</blockquote>
<h4><a name="tag_03_729_03"></a>DESCRIPTION</h4>
<blockquote>
<div class="box"><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]"
border="0"> The functionality described on this reference page is aligned with the ISO&nbsp;C standard. Any conflict between the
requirements described here and the ISO&nbsp;C standard is unintentional. This volume of IEEE&nbsp;Std&nbsp;1003.1-2001 defers to
the ISO&nbsp;C standard. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></div>
<p>The <i>strftime</i>() function shall place bytes into the array pointed to by <i>s</i> as controlled by the string pointed to by
<i>format</i>. The format is a character string, beginning and ending in its initial shift state, if any. The <i>format</i> string
consists of zero or more conversion specifications and ordinary characters. A conversion specification consists of a <tt>'%'</tt>
character, possibly followed by an <tt>E</tt> or <tt>O</tt> modifier, and a terminating conversion specifier character that
determines the conversion specification's behavior. All ordinary characters (including the terminating null byte) are copied
unchanged into the array. If copying takes place between objects that overlap, the behavior is undefined. No more than
<i>maxsize</i> bytes are placed into the array. Each conversion specifier is replaced by appropriate characters as described in the
following list. The appropriate characters are determined using the <i>LC_TIME</i> category of the current locale and by the values
of zero or more members of the broken-down time structure pointed to by <i>timeptr</i>, as specified in brackets in the
description. If any of the specified values are outside the normal range, the characters stored are unspecified.</p>
<p><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">
Local timezone information is used as though <i>strftime</i>() called <a href="../functions/tzset.html"><i>tzset</i>()</a>. <img
src="../images/opt-end.gif" alt="[Option End]" border="0"></p>
<p>The following conversion specifications are supported:</p>
<dl compact>
<dt><tt>%a</tt></dt>
<dd>Replaced by the locale's abbreviated weekday name. [ <i>tm_wday</i>]</dd>
<dt><tt>%A</tt></dt>
<dd>Replaced by the locale's full weekday name. [ <i>tm_wday</i>]</dd>
<dt><tt>%b</tt></dt>
<dd>Replaced by the locale's abbreviated month name. [ <i>tm_mon</i>]</dd>
<dt><tt>%B</tt></dt>
<dd>Replaced by the locale's full month name. [ <i>tm_mon</i>]</dd>
<dt><tt>%c</tt></dt>
<dd>Replaced by the locale's appropriate date and time representation. (See the Base Definitions volume of
IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/time.h.html"><i>&lt;time.h&gt;</i></a>.)</dd>
<dt><tt>%C</tt></dt>
<dd>Replaced by the year divided by 100 and truncated to an integer, as a decimal number [00,99]. [ <i>tm_year</i>]</dd>
<dt><tt>%d</tt></dt>
<dd>Replaced by the day of the month as a decimal number [01,31]. [ <i>tm_mday</i>]</dd>
<dt><tt>%D</tt></dt>
<dd>Equivalent to <tt>%m</tt> / <tt>%d</tt> / <tt>%y</tt> . [ <i>tm_mon</i>, <i>tm_mday</i>, <i>tm_year</i>]</dd>
<dt><tt>%e</tt></dt>
<dd>Replaced by the day of the month as a decimal number [1,31]; a single digit is preceded by a space. [ <i>tm_mday</i>]</dd>
<dt><tt>%F</tt></dt>
<dd>Equivalent to <tt>%Y</tt> - <tt>%m</tt> - <tt>%d</tt> (the ISO&nbsp;8601:2000 standard date format). [ <i>tm_year</i>,
<i>tm_mon</i>, <i>tm_mday</i>]</dd>
<dt><tt>%g</tt></dt>
<dd>Replaced by the last 2 digits of the week-based year (see below) as a decimal number [00,99]. [ <i>tm_year</i>, <i>tm_wday</i>,
<i>tm_yday</i>]</dd>
<dt><tt>%G</tt></dt>
<dd>Replaced by the week-based year (see below) as a decimal number (for example, 1977). [ <i>tm_year</i>, <i>tm_wday</i>,
<i>tm_yday</i>]</dd>
<dt><tt>%h</tt></dt>
<dd>Equivalent to <tt>%b</tt> . [ <i>tm_mon</i>]</dd>
<dt><tt>%H</tt></dt>
<dd>Replaced by the hour (24-hour clock) as a decimal number [00,23]. [ <i>tm_hour</i>]</dd>
<dt><tt>%I</tt></dt>
<dd>Replaced by the hour (12-hour clock) as a decimal number [01,12]. [ <i>tm_hour</i>]</dd>
<dt><tt>%j</tt></dt>
<dd>Replaced by the day of the year as a decimal number [001,366]. [ <i>tm_yday</i>]</dd>
<dt><tt>%m</tt></dt>
<dd>Replaced by the month as a decimal number [01,12]. [ <i>tm_mon</i>]</dd>
<dt><tt>%M</tt></dt>
<dd>Replaced by the minute as a decimal number [00,59]. [ <i>tm_min</i>]</dd>
<dt><tt>%n</tt></dt>
<dd>Replaced by a &lt;newline&gt;.</dd>
<dt><tt>%p</tt></dt>
<dd>Replaced by the locale's equivalent of either a.m. or p.m. [ <i>tm_hour</i>]</dd>
<dt><tt>%r</tt></dt>
<dd>Replaced by the time in a.m. and p.m. notation; <sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src=
"../images/opt-start.gif" alt="[Option Start]" border="0"> &nbsp;in the POSIX locale this shall be equivalent to <tt>%I</tt> :
<tt>%M</tt> : <tt>%S</tt> <tt>%p</tt> . <img src="../images/opt-end.gif" alt="[Option End]" border="0"> [ <i>tm_hour</i>,
<i>tm_min</i>, <i>tm_sec</i>]</dd>
<dt><tt>%R</tt></dt>
<dd>Replaced by the time in 24-hour notation ( <tt>%H</tt> : <tt>%M</tt> ). [ <i>tm_hour</i>, <i>tm_min</i>]</dd>
<dt><tt>%S</tt></dt>
<dd>Replaced by the second as a decimal number [00,60]. [ <i>tm_sec</i>]</dd>
<dt><tt>%t</tt></dt>
<dd>Replaced by a &lt;tab&gt;.</dd>
<dt><tt>%T</tt></dt>
<dd>Replaced by the time ( <tt>%H</tt> : <tt>%M</tt> : <tt>%S</tt> ). [ <i>tm_hour</i>, <i>tm_min</i>, <i>tm_sec</i>]</dd>
<dt><tt>%u</tt></dt>
<dd>Replaced by the weekday as a decimal number [1,7], with 1 representing Monday. [ <i>tm_wday</i>]</dd>
<dt><tt>%U</tt></dt>
<dd>Replaced by the week number of the year as a decimal number [00,53]. The first Sunday of January is the first day of week 1;
days in the new year before this are in week 0. [ <i>tm_year</i>, <i>tm_wday</i>, <i>tm_yday</i>]</dd>
<dt><tt>%V</tt></dt>
<dd>Replaced by the week number of the year (Monday as the first day of the week) as a decimal number [01,53]. If the week
containing 1 January has four or more days in the new year, then it is considered week 1. Otherwise, it is the last week of the
previous year, and the next week is week 1. Both January 4th and the first Thursday of January are always in week 1. [
<i>tm_year</i>, <i>tm_wday</i>, <i>tm_yday</i>]</dd>
<dt><tt>%w</tt></dt>
<dd>Replaced by the weekday as a decimal number [0,6], with 0 representing Sunday. [ <i>tm_wday</i>]</dd>
<dt><tt>%W</tt></dt>
<dd>Replaced by the week number of the year as a decimal number [00,53]. The first Monday of January is the first day of week 1;
days in the new year before this are in week 0. [ <i>tm_year</i>, <i>tm_wday</i>, <i>tm_yday</i>]</dd>
<dt><tt>%x</tt></dt>
<dd>Replaced by the locale's appropriate date representation. (See the Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001,
<a href="../basedefs/time.h.html"><i>&lt;time.h&gt;</i></a>.)</dd>
<dt><tt>%X</tt></dt>
<dd>Replaced by the locale's appropriate time representation. (See the Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001,
<a href="../basedefs/time.h.html"><i>&lt;time.h&gt;</i></a>.)</dd>
<dt><tt>%y</tt></dt>
<dd>Replaced by the last two digits of the year as a decimal number [00,99]. [ <i>tm_year</i>]</dd>
<dt><tt>%Y</tt></dt>
<dd>Replaced by the year as a decimal number (for example, 1997). [ <i>tm_year</i>]</dd>
<dt><tt>%z</tt></dt>
<dd>Replaced by the offset from UTC in the ISO&nbsp;8601:2000 standard format ( <tt>+hhmm</tt> or <tt>-hhmm</tt> ), or by no
characters if no timezone is determinable. For example, <tt>"-0430"</tt> means 4 hours 30 minutes behind UTC (west of Greenwich).
<sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">
&nbsp;If <i>tm_isdst</i> is zero, the standard time offset is used. If <i>tm_isdst</i> is greater than zero, the daylight savings
time offset is used. If <i>tm_isdst</i> is negative, no characters are returned. <img src="../images/opt-end.gif" alt=
"[Option End]" border="0"> [ <i>tm_isdst</i>]</dd>
<dt><tt>%Z</tt></dt>
<dd>Replaced by the timezone name or abbreviation, or by no bytes if no timezone information exists. [ <i>tm_isdst</i>]</dd>
<dt><tt>%%</tt></dt>
<dd>Replaced by <tt>%</tt> .</dd>
</dl>
<p>If a conversion specification does not correspond to any of the above, the behavior is undefined.</p>
<h5><a name="tag_03_729_03_01"></a>Modified Conversion Specifiers</h5>
<p>Some conversion specifiers can be modified by the <tt>E</tt> or <tt>O</tt> modifier characters to indicate that an alternative
format or specification should be used rather than the one normally used by the unmodified conversion specifier. If the alternative
format or specification does not exist for the current locale (see ERA in the Base Definitions volume of
IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/xbd_chap07.html#tag_07_03_05">Section 7.3.5, LC_TIME</a>), the behavior shall
be as if the unmodified conversion specification were used.</p>
<dl compact>
<dt><tt>%Ec</tt></dt>
<dd>Replaced by the locale's alternative appropriate date and time representation.</dd>
<dt><tt>%EC</tt></dt>
<dd>Replaced by the name of the base year (period) in the locale's alternative representation.</dd>
<dt><tt>%Ex</tt></dt>
<dd>Replaced by the locale's alternative date representation.</dd>
<dt><tt>%EX</tt></dt>
<dd>Replaced by the locale's alternative time representation.</dd>
<dt><tt>%Ey</tt></dt>
<dd>Replaced by the offset from <tt>%EC</tt> (year only) in the locale's alternative representation.</dd>
<dt><tt>%EY</tt></dt>
<dd>Replaced by the full alternative year representation.</dd>
<dt><tt>%Od</tt></dt>
<dd>Replaced by the day of the month, using the locale's alternative numeric symbols, filled as needed with leading zeros if there
is any alternative symbol for zero; otherwise, with leading spaces.</dd>
<dt><tt>%Oe</tt></dt>
<dd>Replaced by the day of the month, using the locale's alternative numeric symbols, filled as needed with leading spaces.</dd>
<dt><tt>%OH</tt></dt>
<dd>Replaced by the hour (24-hour clock) using the locale's alternative numeric symbols.</dd>
<dt><tt>%OI</tt></dt>
<dd>Replaced by the hour (12-hour clock) using the locale's alternative numeric symbols.</dd>
<dt><tt>%Om</tt></dt>
<dd>Replaced by the month using the locale's alternative numeric symbols.</dd>
<dt><tt>%OM</tt></dt>
<dd>Replaced by the minutes using the locale's alternative numeric symbols.</dd>
<dt><tt>%OS</tt></dt>
<dd>Replaced by the seconds using the locale's alternative numeric symbols.</dd>
<dt><tt>%Ou</tt></dt>
<dd>Replaced by the weekday as a number in the locale's alternative representation (Monday=1).</dd>
<dt><tt>%OU</tt></dt>
<dd>Replaced by the week number of the year (Sunday as the first day of the week, rules corresponding to <tt>%U</tt> ) using the
locale's alternative numeric symbols.</dd>
<dt><tt>%OV</tt></dt>
<dd>Replaced by the week number of the year (Monday as the first day of the week, rules corresponding to <tt>%V</tt> ) using the
locale's alternative numeric symbols.</dd>
<dt><tt>%Ow</tt></dt>
<dd>Replaced by the number of the weekday (Sunday=0) using the locale's alternative numeric symbols.</dd>
<dt><tt>%OW</tt></dt>
<dd>Replaced by the week number of the year (Monday as the first day of the week) using the locale's alternative numeric
symbols.</dd>
<dt><tt>%Oy</tt></dt>
<dd>Replaced by the year (offset from <tt>%C</tt> ) using the locale's alternative numeric symbols.</dd>
</dl>
<p><tt>%g</tt> , <tt>%G</tt> , and <tt>%V</tt> give values according to the ISO&nbsp;8601:2000 standard week-based year. In this
system, weeks begin on a Monday and week 1 of the year is the week that includes January 4th, which is also the week that includes
the first Thursday of the year, and is also the first week that contains at least four days in the year. If the first Monday of
January is the 2nd, 3rd, or 4th, the preceding days are part of the last week of the preceding year; thus, for Saturday 2nd January
1999, <tt>%G</tt> is replaced by 1998 and <tt>%V</tt> is replaced by 53. If December 29th, 30th, or 31st is a Monday, it and any
following days are part of week 1 of the following year. Thus, for Tuesday 30th December 1997, <tt>%G</tt> is replaced by 1998 and
<tt>%V</tt> is replaced by 01.</p>
<p>If a conversion specifier is not one of the above, the behavior is undefined.</p>
</blockquote>
<h4><a name="tag_03_729_04"></a>RETURN VALUE</h4>
<blockquote>
<p>If the total number of resulting bytes including the terminating null byte is not more than <i>maxsize</i>, <i>strftime</i>()
shall return the number of bytes placed into the array pointed to by <i>s</i>, not including the terminating null byte. Otherwise,
0 shall be returned and the contents of the array are unspecified.</p>
</blockquote>
<h4><a name="tag_03_729_05"></a>ERRORS</h4>
<blockquote>
<p>No errors are defined.</p>
</blockquote>
<hr>
<div class="box"><em>The following sections are informative.</em></div>
<h4><a name="tag_03_729_06"></a>EXAMPLES</h4>
<blockquote>
<h5><a name="tag_03_729_06_01"></a>Getting a Localized Date String</h5>
<p>The following example first sets the locale to the user's default. The locale information will be used in the <a href=
"../functions/nl_langinfo.html"><i>nl_langinfo</i>()</a> and <i>strftime</i>() functions. The <a href=
"../functions/nl_langinfo.html"><i>nl_langinfo</i>()</a> function returns the localized date string which specifies how the date is
laid out. The <i>strftime</i>() function takes this information and, using the <b>tm</b> structure for values, places the date and
time information into <i>datestring</i>.</p>
<pre>
<tt>#include &lt;time.h&gt;
#include &lt;locale.h&gt;
#include &lt;langinfo.h&gt;
...
struct tm *tm;
char datestring[256];
...
setlocale (LC_ALL, "");
...
strftime (datestring, sizeof(datestring), nl_langinfo (D_T_FMT), tm);
...
</tt>
</pre>
</blockquote>
<h4><a name="tag_03_729_07"></a>APPLICATION USAGE</h4>
<blockquote>
<p>The range of values for <tt>%S</tt> is [00,60] rather than [00,59] to allow for the occasional leap second.</p>
<p>Some of the conversion specifications are duplicates of others. They are included for compatibility with <i>nl_cxtime</i>() and
<i>nl_ascxtime</i>(), which were published in Issue 2.</p>
<p>Applications should use <tt>%Y</tt> (4-digit years) in preference to <tt>%y</tt> (2-digit years).</p>
<p>In the C locale, the <tt>E</tt> and <tt>O</tt> modifiers are ignored and the replacement strings for the following specifiers
are:</p>
<dl compact>
<dt><tt>%a</tt></dt>
<dd>The first three characters of <tt>%A</tt> .</dd>
<dt><tt>%A</tt></dt>
<dd>One of Sunday, Monday, ..., Saturday.</dd>
<dt><tt>%b</tt></dt>
<dd>The first three characters of <tt>%B</tt> .</dd>
<dt><tt>%B</tt></dt>
<dd>One of January, February, ..., December.</dd>
<dt><tt>%c</tt></dt>
<dd>Equivalent to <tt>%a</tt> <tt>%b</tt> <tt>%e</tt> <tt>%T</tt> <tt>%Y</tt> .</dd>
<dt><tt>%p</tt></dt>
<dd>One of AM or PM.</dd>
<dt><tt>%r</tt></dt>
<dd>Equivalent to <tt>%I</tt> : <tt>%M</tt> : <tt>%S</tt> <tt>%p</tt> .</dd>
<dt><tt>%x</tt></dt>
<dd>Equivalent to <tt>%m</tt> / <tt>%d</tt> / <tt>%y</tt> .</dd>
<dt><tt>%X</tt></dt>
<dd>Equivalent to <tt>%T</tt> .</dd>
<dt><tt>%Z</tt></dt>
<dd>Implementation-defined.</dd>
</dl>
</blockquote>
<h4><a name="tag_03_729_08"></a>RATIONALE</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_729_09"></a>FUTURE DIRECTIONS</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_729_10"></a>SEE ALSO</h4>
<blockquote>
<p><a href="asctime.html"><i>asctime</i>()</a> , <a href="clock.html"><i>clock</i>()</a> , <a href="ctime.html"><i>ctime</i>()</a>
, <a href="difftime.html"><i>difftime</i>()</a> , <a href="getdate.html"><i>getdate</i>()</a> , <a href=
"gmtime.html"><i>gmtime</i>()</a> , <a href="localtime.html"><i>localtime</i>()</a> , <a href="mktime.html"><i>mktime</i>()</a> ,
<a href="strptime.html"><i>strptime</i>()</a> , <a href="time.html"><i>time</i>()</a> , <a href="tzset.html"><i>tzset</i>()</a> ,
<a href="utime.html"><i>utime</i>()</a> , Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href=
"../basedefs/xbd_chap07.html#tag_07_03_05">Section 7.3.5, LC_TIME</a>, <a href=
"../basedefs/time.h.html"><i>&lt;time.h&gt;</i></a></p>
</blockquote>
<h4><a name="tag_03_729_11"></a>CHANGE HISTORY</h4>
<blockquote>
<p>First released in Issue 3.</p>
</blockquote>
<h4><a name="tag_03_729_12"></a>Issue 5</h4>
<blockquote>
<p>The description of <tt>%OV</tt> is changed to be consistent with <tt>%V</tt> and defines Monday as the first day of the
week.</p>
<p>The description of <tt>%Oy</tt> is clarified.</p>
</blockquote>
<h4><a name="tag_03_729_13"></a>Issue 6</h4>
<blockquote>
<p>Extensions beyond the ISO&nbsp;C standard are marked.</p>
<p>The Open Group Corrigendum U033/8 is applied. The <tt>%V</tt> conversion specifier is changed from &quot;Otherwise, it is week 53 of
the previous year, and the next week is week 1&quot; to &quot;Otherwise, it is the last week of the previous year, and the next week is
week 1&quot;.</p>
<p>The following new requirements on POSIX implementations derive from alignment with the Single UNIX Specification:</p>
<ul>
<li>
<p>The <tt>%C</tt> , <tt>%D</tt> , <tt>%e</tt> , <tt>%h</tt> , <tt>%n</tt> , <tt>%r</tt> , <tt>%R</tt> , <tt>%t</tt> , and
<tt>%T</tt> conversion specifiers are added.</p>
</li>
<li>
<p>The modified conversion specifiers are added for consistency with the ISO&nbsp;POSIX-2 standard <a href=
"../utilities/date.html"><i>date</i></a> utility.</p>
</li>
</ul>
<p>The following changes are made for alignment with the ISO/IEC&nbsp;9899:1999 standard:</p>
<ul>
<li>
<p>The <i>strftime</i>() prototype is updated.</p>
</li>
<li>
<p>The DESCRIPTION is extensively revised.</p>
</li>
<li>
<p>The <tt>%z</tt> conversion specifier is added.</p>
</li>
</ul>
<p>A new example is added.</p>
</blockquote>
<div class="box"><em>End of informative text.</em></div>
<hr>
<hr size="2" noshade>
<center><font size="2"><!--footer start-->
UNIX &reg; is a registered Trademark of The Open Group.<br>
POSIX &reg; is a registered Trademark of The IEEE.<br>
[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href=
"../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>
]</font></center>
<!--footer end-->
<hr size="2" noshade>
</body>
</html>