Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

strftime.html 15KB

3 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6. <title>strftime (The Red Hat newlib C Library)</title>
  7. <meta name="description" content="strftime (The Red Hat newlib C Library)">
  8. <meta name="keywords" content="strftime (The Red Hat newlib C Library)">
  9. <meta name="resource-type" content="document">
  10. <meta name="distribution" content="global">
  11. <meta name="Generator" content="makeinfo">
  12. <link href="index.html#Top" rel="start" title="Top">
  13. <link href="Document-Index.html#Document-Index" rel="index" title="Document Index">
  14. <link href="Document-Index.html#SEC_Contents" rel="contents" title="Table of Contents">
  15. <link href="Timefns.html#Timefns" rel="up" title="Timefns">
  16. <link href="time.html#time" rel="next" title="time">
  17. <link href="mktime.html#mktime" rel="prev" title="mktime">
  18. <style type="text/css">
  19. <!--
  20. a.summary-letter {text-decoration: none}
  21. blockquote.indentedblock {margin-right: 0em}
  22. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  23. blockquote.smallquotation {font-size: smaller}
  24. div.display {margin-left: 3.2em}
  25. div.example {margin-left: 3.2em}
  26. div.lisp {margin-left: 3.2em}
  27. div.smalldisplay {margin-left: 3.2em}
  28. div.smallexample {margin-left: 3.2em}
  29. div.smalllisp {margin-left: 3.2em}
  30. kbd {font-style: oblique}
  31. pre.display {font-family: inherit}
  32. pre.format {font-family: inherit}
  33. pre.menu-comment {font-family: serif}
  34. pre.menu-preformatted {font-family: serif}
  35. pre.smalldisplay {font-family: inherit; font-size: smaller}
  36. pre.smallexample {font-size: smaller}
  37. pre.smallformat {font-family: inherit; font-size: smaller}
  38. pre.smalllisp {font-size: smaller}
  39. span.nolinebreak {white-space: nowrap}
  40. span.roman {font-family: initial; font-weight: normal}
  41. span.sansserif {font-family: sans-serif; font-weight: normal}
  42. ul.no-bullet {list-style: none}
  43. -->
  44. </style>
  45. </head>
  46. <body lang="en">
  47. <a name="strftime"></a>
  48. <div class="header">
  49. <p>
  50. Next: <a href="time.html#time" accesskey="n" rel="next">time</a>, Previous: <a href="mktime.html#mktime" accesskey="p" rel="prev">mktime</a>, Up: <a href="Timefns.html#Timefns" accesskey="u" rel="up">Timefns</a> &nbsp; [<a href="Document-Index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Document-Index.html#Document-Index" title="Index" rel="index">Index</a>]</p>
  51. </div>
  52. <hr>
  53. <a name="strftime_002c-strftime_005fl_002d_002d_002dconvert-date-and-time-to-a-formatted-string"></a>
  54. <h3 class="section">8.8 <code>strftime</code>, <code>strftime_l</code>&mdash;convert date and time to a formatted string</h3>
  55. <a name="index-strftime"></a>
  56. <a name="index-strftime_005fl"></a>
  57. <p><strong>Synopsis</strong>
  58. </p><div class="example">
  59. <pre class="example">#include &lt;time.h&gt;
  60. size_t strftime(char *restrict <var>s</var>, size_t <var>maxsize</var>,
  61. const char *restrict <var>format</var>,
  62. const struct tm *restrict <var>timp</var>);
  63. size_t strftime_l(char *restrict <var>s</var>, size_t <var>maxsize</var>,
  64. const char *restrict <var>format</var>,
  65. const struct tm *restrict <var>timp</var>,
  66. locale_t <var>locale</var>);
  67. </pre></div>
  68. <p><strong>Description</strong><br>
  69. <code>strftime</code> converts a <code>struct tm</code> representation of the time (at
  70. <var>timp</var>) into a null-terminated string, starting at <var>s</var> and occupying
  71. no more than <var>maxsize</var> characters.
  72. </p>
  73. <p><code>strftime_l</code> is like <code>strftime</code> but creates a string in a format
  74. as expected in locale <var>locale</var>. If <var>locale</var> is LC_GLOBAL_LOCALE or
  75. not a valid locale object, the behaviour is undefined.
  76. </p>
  77. <p>You control the format of the output using the string at <var>format</var>.
  78. <code>*<var>format</var></code> can contain two kinds of specifications: text to be
  79. copied literally into the formatted string, and time conversion
  80. specifications. Time conversion specifications are two- and
  81. three-character sequences beginning with &lsquo;<code>%</code>&rsquo; (use &lsquo;<code>%%</code>&rsquo; to
  82. include a percent sign in the output). Each defined conversion
  83. specification selects only the specified field(s) of calendar time
  84. data from <code>*<var>timp</var></code>, and converts it to a string in one of the
  85. following ways:
  86. </p>
  87. <dl compact="compact">
  88. <dt><code>%a</code></dt>
  89. <dd><p>The abbreviated weekday name according to the current locale. [tm_wday]
  90. </p>
  91. </dd>
  92. <dt><code>%A</code></dt>
  93. <dd><p>The full weekday name according to the current locale.
  94. In the default &quot;C&quot; locale, one of &lsquo;<code>Sunday</code>&rsquo;, &lsquo;<code>Monday</code>&rsquo;, &lsquo;<code>Tuesday</code>&rsquo;,
  95. &lsquo;<code>Wednesday</code>&rsquo;, &lsquo;<code>Thursday</code>&rsquo;, &lsquo;<code>Friday</code>&rsquo;, &lsquo;<code>Saturday</code>&rsquo;. [tm_wday]
  96. </p>
  97. </dd>
  98. <dt><code>%b</code></dt>
  99. <dd><p>The abbreviated month name according to the current locale. [tm_mon]
  100. </p>
  101. </dd>
  102. <dt><code>%B</code></dt>
  103. <dd><p>The full month name according to the current locale.
  104. In the default &quot;C&quot; locale, one of &lsquo;<code>January</code>&rsquo;, &lsquo;<code>February</code>&rsquo;,
  105. &lsquo;<code>March</code>&rsquo;, &lsquo;<code>April</code>&rsquo;, &lsquo;<code>May</code>&rsquo;, &lsquo;<code>June</code>&rsquo;, &lsquo;<code>July</code>&rsquo;,
  106. &lsquo;<code>August</code>&rsquo;, &lsquo;<code>September</code>&rsquo;, &lsquo;<code>October</code>&rsquo;, &lsquo;<code>November</code>&rsquo;,
  107. &lsquo;<code>December</code>&rsquo;. [tm_mon]
  108. </p>
  109. </dd>
  110. <dt><code>%c</code></dt>
  111. <dd><p>The preferred date and time representation for the current locale.
  112. [tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday]
  113. </p>
  114. </dd>
  115. <dt><code>%C</code></dt>
  116. <dd><p>The century, that is, the year divided by 100 then truncated. For
  117. 4-digit years, the result is zero-padded and exactly two characters;
  118. but for other years, there may a negative sign or more digits. In
  119. this way, &lsquo;<code>%C%y</code>&rsquo; is equivalent to &lsquo;<code>%Y</code>&rsquo;. [tm_year]
  120. </p>
  121. </dd>
  122. <dt><code>%d</code></dt>
  123. <dd><p>The day of the month, formatted with two digits (from &lsquo;<code>01</code>&rsquo; to
  124. &lsquo;<code>31</code>&rsquo;). [tm_mday]
  125. </p>
  126. </dd>
  127. <dt><code>%D</code></dt>
  128. <dd><p>A string representing the date, in the form &lsquo;<code>&quot;%m/%d/%y&quot;</code>&rsquo;.
  129. [tm_mday, tm_mon, tm_year]
  130. </p>
  131. </dd>
  132. <dt><code>%e</code></dt>
  133. <dd><p>The day of the month, formatted with leading space if single digit
  134. (from &lsquo;<code>1</code>&rsquo; to &lsquo;<code>31</code>&rsquo;). [tm_mday]
  135. </p>
  136. </dd>
  137. <dt><code>%E<code>x</code></code></dt>
  138. <dd><p>In some locales, the E modifier selects alternative representations of
  139. certain modifiers <code>x</code>. In newlib, it is ignored, and treated as %<code>x</code>.
  140. </p>
  141. </dd>
  142. <dt><code>%F</code></dt>
  143. <dd><p>A string representing the ISO 8601:2000 date format, in the form
  144. &lsquo;<code>&quot;%Y-%m-%d&quot;</code>&rsquo;. [tm_mday, tm_mon, tm_year]
  145. </p>
  146. </dd>
  147. <dt><code>%g</code></dt>
  148. <dd><p>The last two digits of the week-based year, see specifier %G (from
  149. &lsquo;<code>00</code>&rsquo; to &lsquo;<code>99</code>&rsquo;). [tm_year, tm_wday, tm_yday]
  150. </p>
  151. </dd>
  152. <dt><code>%G</code></dt>
  153. <dd><p>The week-based year. In the ISO 8601:2000 calendar, week 1 of the year
  154. includes January 4th, and begin on Mondays. Therefore, if January 1st,
  155. 2nd, or 3rd falls on a Sunday, that day and earlier belong to the last
  156. week of the previous year; and if December 29th, 30th, or 31st falls
  157. on Monday, that day and later belong to week 1 of the next year. For
  158. consistency with %Y, it always has at least four characters.
  159. Example: &quot;%G&quot; for Saturday 2nd January 1999 gives &quot;1998&quot;, and for
  160. Tuesday 30th December 1997 gives &quot;1998&quot;. [tm_year, tm_wday, tm_yday]
  161. </p>
  162. </dd>
  163. <dt><code>%h</code></dt>
  164. <dd><p>Synonym for &quot;%b&quot;. [tm_mon]
  165. </p>
  166. </dd>
  167. <dt><code>%H</code></dt>
  168. <dd><p>The hour (on a 24-hour clock), formatted with two digits (from
  169. &lsquo;<code>00</code>&rsquo; to &lsquo;<code>23</code>&rsquo;). [tm_hour]
  170. </p>
  171. </dd>
  172. <dt><code>%I</code></dt>
  173. <dd><p>The hour (on a 12-hour clock), formatted with two digits (from
  174. &lsquo;<code>01</code>&rsquo; to &lsquo;<code>12</code>&rsquo;). [tm_hour]
  175. </p>
  176. </dd>
  177. <dt><code>%j</code></dt>
  178. <dd><p>The count of days in the year, formatted with three digits
  179. (from &lsquo;<code>001</code>&rsquo; to &lsquo;<code>366</code>&rsquo;). [tm_yday]
  180. </p>
  181. </dd>
  182. <dt><code>%k</code></dt>
  183. <dd><p>The hour (on a 24-hour clock), formatted with leading space if single
  184. digit (from &lsquo;<code>0</code>&rsquo; to &lsquo;<code>23</code>&rsquo;). Non-POSIX extension (c.p. %I). [tm_hour]
  185. </p>
  186. </dd>
  187. <dt><code>%l</code></dt>
  188. <dd><p>The hour (on a 12-hour clock), formatted with leading space if single
  189. digit (from &lsquo;<code>1</code>&rsquo; to &lsquo;<code>12</code>&rsquo;). Non-POSIX extension (c.p. %H). [tm_hour]
  190. </p>
  191. </dd>
  192. <dt><code>%m</code></dt>
  193. <dd><p>The month number, formatted with two digits (from &lsquo;<code>01</code>&rsquo; to &lsquo;<code>12</code>&rsquo;).
  194. [tm_mon]
  195. </p>
  196. </dd>
  197. <dt><code>%M</code></dt>
  198. <dd><p>The minute, formatted with two digits (from &lsquo;<code>00</code>&rsquo; to &lsquo;<code>59</code>&rsquo;). [tm_min]
  199. </p>
  200. </dd>
  201. <dt><code>%n</code></dt>
  202. <dd><p>A newline character (&lsquo;<code>\n</code>&rsquo;).
  203. </p>
  204. </dd>
  205. <dt><code>%O<code>x</code></code></dt>
  206. <dd><p>In some locales, the O modifier selects alternative digit characters
  207. for certain modifiers <code>x</code>. In newlib, it is ignored, and treated as %<code>x</code>.
  208. </p>
  209. </dd>
  210. <dt><code>%p</code></dt>
  211. <dd><p>Either &lsquo;<code>AM</code>&rsquo; or &lsquo;<code>PM</code>&rsquo; as appropriate, or the corresponding strings for
  212. the current locale. [tm_hour]
  213. </p>
  214. </dd>
  215. <dt><code>%P</code></dt>
  216. <dd><p>Same as &rsquo;<code>%p</code>&rsquo;, but in lowercase. This is a GNU extension. [tm_hour]
  217. </p>
  218. </dd>
  219. <dt><code>%r</code></dt>
  220. <dd><p>Replaced by the time in a.m. and p.m. notation. In the &quot;C&quot; locale this
  221. is equivalent to &quot;%I:%M:%S %p&quot;. In locales which don&rsquo;t define a.m./p.m.
  222. notations, the result is an empty string. [tm_sec, tm_min, tm_hour]
  223. </p>
  224. </dd>
  225. <dt><code>%R</code></dt>
  226. <dd><p>The 24-hour time, to the minute. Equivalent to &quot;%H:%M&quot;. [tm_min, tm_hour]
  227. </p>
  228. </dd>
  229. <dt><code>%s</code></dt>
  230. <dd><p>The time elapsed, in seconds, since the start of the Unix epoch at
  231. 1970-01-01 00:00:00 UTC.
  232. </p>
  233. </dd>
  234. <dt><code>%S</code></dt>
  235. <dd><p>The second, formatted with two digits (from &lsquo;<code>00</code>&rsquo; to &lsquo;<code>60</code>&rsquo;). The
  236. value 60 accounts for the occasional leap second. [tm_sec]
  237. </p>
  238. </dd>
  239. <dt><code>%t</code></dt>
  240. <dd><p>A tab character (&lsquo;<code>\t</code>&rsquo;).
  241. </p>
  242. </dd>
  243. <dt><code>%T</code></dt>
  244. <dd><p>The 24-hour time, to the second. Equivalent to &quot;%H:%M:%S&quot;. [tm_sec,
  245. tm_min, tm_hour]
  246. </p>
  247. </dd>
  248. <dt><code>%u</code></dt>
  249. <dd><p>The weekday as a number, 1-based from Monday (from &lsquo;<code>1</code>&rsquo; to
  250. &lsquo;<code>7</code>&rsquo;). [tm_wday]
  251. </p>
  252. </dd>
  253. <dt><code>%U</code></dt>
  254. <dd><p>The week number, where weeks start on Sunday, week 1 contains the first
  255. Sunday in a year, and earlier days are in week 0. Formatted with two
  256. digits (from &lsquo;<code>00</code>&rsquo; to &lsquo;<code>53</code>&rsquo;). See also <code>%W</code>. [tm_wday, tm_yday]
  257. </p>
  258. </dd>
  259. <dt><code>%V</code></dt>
  260. <dd><p>The week number, where weeks start on Monday, week 1 contains January 4th,
  261. and earlier days are in the previous year. Formatted with two digits
  262. (from &lsquo;<code>01</code>&rsquo; to &lsquo;<code>53</code>&rsquo;). See also <code>%G</code>. [tm_year, tm_wday, tm_yday]
  263. </p>
  264. </dd>
  265. <dt><code>%w</code></dt>
  266. <dd><p>The weekday as a number, 0-based from Sunday (from &lsquo;<code>0</code>&rsquo; to &lsquo;<code>6</code>&rsquo;).
  267. [tm_wday]
  268. </p>
  269. </dd>
  270. <dt><code>%W</code></dt>
  271. <dd><p>The week number, where weeks start on Monday, week 1 contains the first
  272. Monday in a year, and earlier days are in week 0. Formatted with two
  273. digits (from &lsquo;<code>00</code>&rsquo; to &lsquo;<code>53</code>&rsquo;). [tm_wday, tm_yday]
  274. </p>
  275. </dd>
  276. <dt><code>%x</code></dt>
  277. <dd><p>Replaced by the preferred date representation in the current locale.
  278. In the &quot;C&quot; locale this is equivalent to &quot;%m/%d/%y&quot;.
  279. [tm_mon, tm_mday, tm_year]
  280. </p>
  281. </dd>
  282. <dt><code>%X</code></dt>
  283. <dd><p>Replaced by the preferred time representation in the current locale.
  284. In the &quot;C&quot; locale this is equivalent to &quot;%H:%M:%S&quot;. [tm_sec, tm_min, tm_hour]
  285. </p>
  286. </dd>
  287. <dt><code>%y</code></dt>
  288. <dd><p>The last two digits of the year (from &lsquo;<code>00</code>&rsquo; to &lsquo;<code>99</code>&rsquo;). [tm_year]
  289. (Implementation interpretation: always positive, even for negative years.)
  290. </p>
  291. </dd>
  292. <dt><code>%Y</code></dt>
  293. <dd><p>The full year, equivalent to <code>%C%y</code>. It will always have at least four
  294. characters, but may have more. The year is accurate even when tm_year
  295. added to the offset of 1900 overflows an int. [tm_year]
  296. </p>
  297. </dd>
  298. <dt><code>%z</code></dt>
  299. <dd><p>The offset from UTC. The format consists of a sign (negative is west of
  300. Greewich), two characters for hour, then two characters for minutes
  301. (-hhmm or +hhmm). If tm_isdst is negative, the offset is unknown and no
  302. output is generated; if it is zero, the offset is the standard offset for
  303. the current time zone; and if it is positive, the offset is the daylight
  304. savings offset for the current timezone. The offset is determined from
  305. the TZ environment variable, as if by calling tzset(). [tm_isdst]
  306. </p>
  307. </dd>
  308. <dt><code>%Z</code></dt>
  309. <dd><p>The time zone name. If tm_isdst is negative, no output is generated.
  310. Otherwise, the time zone name is based on the TZ environment variable,
  311. as if by calling tzset(). [tm_isdst]
  312. </p>
  313. </dd>
  314. <dt><code>%%</code></dt>
  315. <dd><p>A single character, &lsquo;<code>%</code>&rsquo;.
  316. </p></dd>
  317. </dl>
  318. <br>
  319. <p><strong>Returns</strong><br>
  320. When the formatted time takes up no more than <var>maxsize</var> characters,
  321. the result is the length of the formatted string. Otherwise, if the
  322. formatting operation was abandoned due to lack of room, the result is
  323. <code>0</code>, and the string starting at <var>s</var> corresponds to just those
  324. parts of <code>*<var>format</var></code> that could be completely filled in within the
  325. <var>maxsize</var> limit.
  326. </p>
  327. <br>
  328. <p><strong>Portability</strong><br>
  329. ANSI C requires <code>strftime</code>, but does not specify the contents of
  330. <code>*<var>s</var></code> when the formatted string would require more than
  331. <var>maxsize</var> characters. Unrecognized specifiers and fields of
  332. <code>timp</code> that are out of range cause undefined results. Since some
  333. formats expand to 0 bytes, it is wise to set <code>*<var>s</var></code> to a nonzero
  334. value beforehand to distinguish between failure and an empty string.
  335. This implementation does not support <code>s</code> being NULL, nor overlapping
  336. <code>s</code> and <code>format</code>.
  337. </p>
  338. <p><code>strftime_l</code> is POSIX-1.2008.
  339. </p>
  340. <p><code>strftime</code> and <code>strftime_l</code> require no supporting OS subroutines.
  341. </p>
  342. <br>
  343. <p><strong>Bugs</strong><br>
  344. (NOT Cygwin:) <code>strftime</code> ignores the LC_TIME category of the current
  345. locale, hard-coding the &quot;C&quot; locale settings.
  346. </p>
  347. <br>
  348. <hr>
  349. <div class="header">
  350. <p>
  351. Next: <a href="time.html#time" accesskey="n" rel="next">time</a>, Previous: <a href="mktime.html#mktime" accesskey="p" rel="prev">mktime</a>, Up: <a href="Timefns.html#Timefns" accesskey="u" rel="up">Timefns</a> &nbsp; [<a href="Document-Index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Document-Index.html#Document-Index" title="Index" rel="index">Index</a>]</p>
  352. </div>
  353. </body>
  354. </html>