~ubuntu-branches/ubuntu/maverick/eglibc/maverick-proposed

« back to all changes in this revision

Viewing changes to manual/libc_21.html

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-05-30 11:05:12 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20100530110512-axurnekxr2yygcf2
Tags: 2.12-0ubuntu1
* Build eglibc_2.12.orig.tar.gz, based on 2.12 branch (r10591).
* Merge with Debian (r4299, trunk).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd">
2
 
<html>
3
 
<!-- This file documents the GNU C library.
4
 
 
5
 
This is Edition 0.12, last updated 2007-10-27,
6
 
of The GNU C Library Reference Manual, for version
7
 
2.8 (Ubuntu EGLIBC 2.12~20100519-0ubuntu1~ppa1) .
8
 
 
9
 
Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002,
10
 
2003, 2007, 2008 Free Software Foundation, Inc.
11
 
 
12
 
Permission is granted to copy, distribute and/or modify this document
13
 
under the terms of the GNU Free Documentation License, Version 1.2 or
14
 
any later version published by the Free Software Foundation; with the
15
 
Invariant Sections being "Free Software Needs Free Documentation"
16
 
and "GNU Lesser General Public License", the Front-Cover texts being
17
 
"A GNU Manual", and with the Back-Cover Texts as in (a) below.  A
18
 
copy of the license is included in the section entitled "GNU Free
19
 
Documentation License".
20
 
 
21
 
(a) The FSF's Back-Cover Text is: "You have the freedom to
22
 
copy and modify this GNU manual.  Buying copies from the FSF
23
 
supports it in developing GNU and promoting software freedom."
24
 
 -->
25
 
<!-- Created on May 20, 2010 by texi2html 1.82
26
 
texi2html was written by: 
27
 
            Lionel Cons <Lionel.Cons@cern.ch> (original author)
28
 
            Karl Berry  <karl@freefriends.org>
29
 
            Olaf Bachmann <obachman@mathematik.uni-kl.de>
30
 
            and many others.
31
 
Maintained by: Many creative people.
32
 
Send bugs and suggestions to <texi2html-bug@nongnu.org>
33
 
-->
34
 
<head>
35
 
<title>The GNU C Library: 21. Date and Time</title>
36
 
 
37
 
<meta name="description" content="The GNU C Library: 21. Date and Time">
38
 
<meta name="keywords" content="The GNU C Library: 21. Date and Time">
39
 
<meta name="resource-type" content="document">
40
 
<meta name="distribution" content="global">
41
 
<meta name="Generator" content="texi2html 1.82">
42
 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
43
 
<style type="text/css">
44
 
<!--
45
 
a.summary-letter {text-decoration: none}
46
 
blockquote.smallquotation {font-size: smaller}
47
 
pre.display {font-family: serif}
48
 
pre.format {font-family: serif}
49
 
pre.menu-comment {font-family: serif}
50
 
pre.menu-preformatted {font-family: serif}
51
 
pre.smalldisplay {font-family: serif; font-size: smaller}
52
 
pre.smallexample {font-size: smaller}
53
 
pre.smallformat {font-family: serif; font-size: smaller}
54
 
pre.smalllisp {font-size: smaller}
55
 
span.roman {font-family:serif; font-weight:normal;}
56
 
span.sansserif {font-family:sans-serif; font-weight:normal;}
57
 
ul.toc {list-style: none}
58
 
-->
59
 
</style>
60
 
 
61
 
 
62
 
</head>
63
 
 
64
 
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
65
 
 
66
 
<a name="Date-and-Time"></a>
67
 
<table cellpadding="1" cellspacing="1" border="0">
68
 
<tr><td valign="middle" align="left">[<a href="libc_20.html#System-V-Number-Conversion" title="Previous section in reading order"> &lt; </a>]</td>
69
 
<td valign="middle" align="left">[<a href="#Time-Basics" title="Next section in reading order"> &gt; </a>]</td>
70
 
<td valign="middle" align="left"> &nbsp; </td>
71
 
<td valign="middle" align="left">[<a href="libc_20.html#Arithmetic" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
72
 
<td valign="middle" align="left">[<a href="libc.html#Top" title="Up section"> Up </a>]</td>
73
 
<td valign="middle" align="left">[<a href="libc_22.html#Resource-Usage-And-Limitation" title="Next chapter"> &gt;&gt; </a>]</td>
74
 
<td valign="middle" align="left"> &nbsp; </td>
75
 
<td valign="middle" align="left"> &nbsp; </td>
76
 
<td valign="middle" align="left"> &nbsp; </td>
77
 
<td valign="middle" align="left"> &nbsp; </td>
78
 
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
79
 
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
80
 
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
81
 
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
82
 
</tr></table>
83
 
<a name="Date-and-Time-1"></a>
84
 
<h1 class="chapter">21. Date and Time</h1>
85
 
 
86
 
<p>This chapter describes functions for manipulating dates and times,
87
 
including functions for determining what time it is and conversion
88
 
between different time representations.
89
 
</p>
90
 
<table class="menu" border="0" cellspacing="0">
91
 
<tr><td align="left" valign="top"><a href="#Time-Basics">21.1 Time Basics</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">                 Concepts and definitions.
92
 
</td></tr>
93
 
<tr><td align="left" valign="top"><a href="#Elapsed-Time">21.2 Elapsed Time</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">                Data types to represent elapsed times
94
 
</td></tr>
95
 
<tr><td align="left" valign="top"><a href="#Processor-And-CPU-Time">21.3 Processor And CPU Time</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">      Time a program has spent executing.
96
 
</td></tr>
97
 
<tr><td align="left" valign="top"><a href="#Calendar-Time">21.4 Calendar Time</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">               Manipulation of &ldquo;real&rdquo; dates and times.
98
 
</td></tr>
99
 
<tr><td align="left" valign="top"><a href="#Setting-an-Alarm">21.5 Setting an Alarm</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">            Sending a signal after a specified time.
100
 
</td></tr>
101
 
<tr><td align="left" valign="top"><a href="#Sleeping">21.6 Sleeping</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">                    Waiting for a period of time.
102
 
</td></tr>
103
 
</table>
104
 
 
105
 
 
106
 
<hr size="6">
107
 
<a name="Time-Basics"></a>
108
 
<table cellpadding="1" cellspacing="1" border="0">
109
 
<tr><td valign="middle" align="left">[<a href="#Date-and-Time" title="Previous section in reading order"> &lt; </a>]</td>
110
 
<td valign="middle" align="left">[<a href="#Elapsed-Time" title="Next section in reading order"> &gt; </a>]</td>
111
 
<td valign="middle" align="left"> &nbsp; </td>
112
 
<td valign="middle" align="left">[<a href="#Date-and-Time" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
113
 
<td valign="middle" align="left">[<a href="#Date-and-Time" title="Up section"> Up </a>]</td>
114
 
<td valign="middle" align="left">[<a href="libc_22.html#Resource-Usage-And-Limitation" title="Next chapter"> &gt;&gt; </a>]</td>
115
 
<td valign="middle" align="left"> &nbsp; </td>
116
 
<td valign="middle" align="left"> &nbsp; </td>
117
 
<td valign="middle" align="left"> &nbsp; </td>
118
 
<td valign="middle" align="left"> &nbsp; </td>
119
 
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
120
 
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
121
 
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
122
 
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
123
 
</tr></table>
124
 
<a name="Time-Basics-1"></a>
125
 
<h2 class="section">21.1 Time Basics</h2>
126
 
<a name="index-time"></a>
127
 
 
128
 
<p>Discussing time in a technical manual can be difficult because the word
129
 
&ldquo;time&rdquo; in English refers to lots of different things.  In this manual,
130
 
we use a rigorous terminology to avoid confusion, and the only thing we
131
 
use the simple word &ldquo;time&rdquo; for is to talk about the abstract concept.
132
 
</p>
133
 
<p>A <em>calendar time</em> is a point in the time continuum, for example
134
 
November 4, 1990 at 18:02.5 UTC.  Sometimes this is called &ldquo;absolute
135
 
time&rdquo;.
136
 
<a name="index-calendar-time"></a>
137
 
</p>
138
 
<p>We don&rsquo;t speak of a &ldquo;date&rdquo;, because that is inherent in a calendar
139
 
time.
140
 
<a name="index-date"></a>
141
 
</p>
142
 
<p>An <em>interval</em> is a contiguous part of the time continuum between two
143
 
calendar times, for example the hour between 9:00 and 10:00 on July 4,
144
 
1980.
145
 
<a name="index-interval"></a>
146
 
</p>
147
 
<p>An <em>elapsed time</em> is the length of an interval, for example, 35
148
 
minutes.  People sometimes sloppily use the word &ldquo;interval&rdquo; to refer
149
 
to the elapsed time of some interval.
150
 
<a name="index-elapsed-time"></a>
151
 
<a name="index-time_002c-elapsed"></a>
152
 
</p>
153
 
<p>An <em>amount of time</em> is a sum of elapsed times, which need not be of
154
 
any specific intervals.  For example, the amount of time it takes to
155
 
read a book might be 9 hours, independently of when and in how many
156
 
sittings it is read.
157
 
</p>
158
 
<p>A <em>period</em> is the elapsed time of an interval between two events,
159
 
especially when they are part of a sequence of regularly repeating
160
 
events.
161
 
<a name="index-period-of-time"></a>
162
 
</p>
163
 
<p><em>CPU time</em> is like calendar time, except that it is based on the
164
 
subset of the time continuum when a particular process is actively
165
 
using a CPU.  CPU time is, therefore, relative to a process.
166
 
<a name="index-CPU-time"></a>
167
 
</p>
168
 
<p><em>Processor time</em> is an amount of time that a CPU is in use.  In
169
 
fact, it&rsquo;s a basic system resource, since there&rsquo;s a limit to how much
170
 
can exist in any given interval (that limit is the elapsed time of the
171
 
interval times the number of CPUs in the processor).  People often call
172
 
this CPU time, but we reserve the latter term in this manual for the
173
 
definition above.
174
 
<a name="index-processor-time"></a>
175
 
</p>
176
 
<hr size="6">
177
 
<a name="Elapsed-Time"></a>
178
 
<table cellpadding="1" cellspacing="1" border="0">
179
 
<tr><td valign="middle" align="left">[<a href="#Time-Basics" title="Previous section in reading order"> &lt; </a>]</td>
180
 
<td valign="middle" align="left">[<a href="#Processor-And-CPU-Time" title="Next section in reading order"> &gt; </a>]</td>
181
 
<td valign="middle" align="left"> &nbsp; </td>
182
 
<td valign="middle" align="left">[<a href="#Date-and-Time" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
183
 
<td valign="middle" align="left">[<a href="#Date-and-Time" title="Up section"> Up </a>]</td>
184
 
<td valign="middle" align="left">[<a href="libc_22.html#Resource-Usage-And-Limitation" title="Next chapter"> &gt;&gt; </a>]</td>
185
 
<td valign="middle" align="left"> &nbsp; </td>
186
 
<td valign="middle" align="left"> &nbsp; </td>
187
 
<td valign="middle" align="left"> &nbsp; </td>
188
 
<td valign="middle" align="left"> &nbsp; </td>
189
 
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
190
 
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
191
 
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
192
 
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
193
 
</tr></table>
194
 
<a name="Elapsed-Time-1"></a>
195
 
<h2 class="section">21.2 Elapsed Time</h2>
196
 
<a name="index-elapsed-time-1"></a>
197
 
 
198
 
<p>One way to represent an elapsed time is with a simple arithmetic data
199
 
type, as with the following function to compute the elapsed time between
200
 
two calendar times.  This function is declared in &lsquo;<tt>time.h</tt>&rsquo;.
201
 
</p>
202
 
<dl>
203
 
<dt><a name="index-difftime"></a><u>Function:</u> double <b>difftime</b><i> (time_t <var>time1</var>, time_t <var>time0</var>)</i></dt>
204
 
<dd><p>The <code>difftime</code> function returns the number of seconds of elapsed
205
 
time between calendar time <var>time1</var> and calendar time <var>time0</var>, as
206
 
a value of type <code>double</code>.  The difference ignores leap seconds
207
 
unless leap second support is enabled.
208
 
</p>
209
 
<p>In the GNU system, you can simply subtract <code>time_t</code> values.  But on
210
 
other systems, the <code>time_t</code> data type might use some other encoding
211
 
where subtraction doesn&rsquo;t work directly.
212
 
</p></dd></dl>
213
 
 
214
 
<p>The GNU C library provides two data types specifically for representing
215
 
an elapsed time.  They are used by various GNU C library functions, and
216
 
you can use them for your own purposes too.  They&rsquo;re exactly the same
217
 
except that one has a resolution in microseconds, and the other, newer
218
 
one, is in nanoseconds.
219
 
</p>
220
 
<dl>
221
 
<dt><a name="index-struct-timeval"></a><u>Data Type:</u> <b>struct timeval</b></dt>
222
 
<dd><a name="index-timeval"></a>
223
 
<p>The <code>struct timeval</code> structure represents an elapsed time.  It is
224
 
declared in &lsquo;<tt>sys/time.h</tt>&rsquo; and has the following members:
225
 
</p>
226
 
<dl compact="compact">
227
 
<dt> <code>long int tv_sec</code></dt>
228
 
<dd><p>This represents the number of whole seconds of elapsed time.
229
 
</p>
230
 
</dd>
231
 
<dt> <code>long int tv_usec</code></dt>
232
 
<dd><p>This is the rest of the elapsed time (a fraction of a second),
233
 
represented as the number of microseconds.  It is always less than one
234
 
million.
235
 
</p>
236
 
</dd>
237
 
</dl>
238
 
</dd></dl>
239
 
 
240
 
<dl>
241
 
<dt><a name="index-struct-timespec"></a><u>Data Type:</u> <b>struct timespec</b></dt>
242
 
<dd><a name="index-timespec"></a>
243
 
<p>The <code>struct timespec</code> structure represents an elapsed time.  It is
244
 
declared in &lsquo;<tt>time.h</tt>&rsquo; and has the following members:
245
 
</p>
246
 
<dl compact="compact">
247
 
<dt> <code>long int tv_sec</code></dt>
248
 
<dd><p>This represents the number of whole seconds of elapsed time.
249
 
</p>
250
 
</dd>
251
 
<dt> <code>long int tv_nsec</code></dt>
252
 
<dd><p>This is the rest of the elapsed time (a fraction of a second),
253
 
represented as the number of nanoseconds.  It is always less than one
254
 
billion.
255
 
</p>
256
 
</dd>
257
 
</dl>
258
 
</dd></dl>
259
 
 
260
 
<p>It is often necessary to subtract two values of type <code>struct
261
 
timeval</code> or <code>struct timespec</code>.  Here is the best way to do
262
 
this.  It works even on some peculiar operating systems where the
263
 
<code>tv_sec</code> member has an unsigned type.
264
 
</p>
265
 
<table><tr><td>&nbsp;</td><td><pre class="smallexample">/* <span class="roman">Subtract the `struct timeval' values X and Y,</span>
266
 
   <span class="roman">storing the result in RESULT.</span>
267
 
   <span class="roman">Return 1 if the difference is negative, otherwise 0.</span>  */
268
 
 
269
 
int
270
 
timeval_subtract (result, x, y)
271
 
     struct timeval *result, *x, *y;
272
 
{
273
 
  /* <span class="roman">Perform the carry for the later subtraction by updating <var>y</var>.</span> */
274
 
  if (x-&gt;tv_usec &lt; y-&gt;tv_usec) {
275
 
    int nsec = (y-&gt;tv_usec - x-&gt;tv_usec) / 1000000 + 1;
276
 
    y-&gt;tv_usec -= 1000000 * nsec;
277
 
    y-&gt;tv_sec += nsec;
278
 
  }
279
 
  if (x-&gt;tv_usec - y-&gt;tv_usec &gt; 1000000) {
280
 
    int nsec = (x-&gt;tv_usec - y-&gt;tv_usec) / 1000000;
281
 
    y-&gt;tv_usec += 1000000 * nsec;
282
 
    y-&gt;tv_sec -= nsec;
283
 
  }
284
 
 
285
 
  /* <span class="roman">Compute the time remaining to wait.</span>
286
 
     <span class="roman"><code>tv_usec</code> is certainly positive.</span> */
287
 
  result-&gt;tv_sec = x-&gt;tv_sec - y-&gt;tv_sec;
288
 
  result-&gt;tv_usec = x-&gt;tv_usec - y-&gt;tv_usec;
289
 
 
290
 
  /* <span class="roman">Return 1 if result is negative.</span> */
291
 
  return x-&gt;tv_sec &lt; y-&gt;tv_sec;
292
 
}
293
 
</pre></td></tr></table>
294
 
 
295
 
<p>Common functions that use <code>struct timeval</code> are <code>gettimeofday</code>
296
 
and <code>settimeofday</code>.
297
 
</p>
298
 
 
299
 
<p>There are no GNU C library functions specifically oriented toward
300
 
dealing with elapsed times, but the calendar time, processor time, and
301
 
alarm and sleeping functions have a lot to do with them.
302
 
</p>
303
 
 
304
 
<hr size="6">
305
 
<a name="Processor-And-CPU-Time"></a>
306
 
<table cellpadding="1" cellspacing="1" border="0">
307
 
<tr><td valign="middle" align="left">[<a href="#Elapsed-Time" title="Previous section in reading order"> &lt; </a>]</td>
308
 
<td valign="middle" align="left">[<a href="#CPU-Time" title="Next section in reading order"> &gt; </a>]</td>
309
 
<td valign="middle" align="left"> &nbsp; </td>
310
 
<td valign="middle" align="left">[<a href="#Date-and-Time" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
311
 
<td valign="middle" align="left">[<a href="#Date-and-Time" title="Up section"> Up </a>]</td>
312
 
<td valign="middle" align="left">[<a href="libc_22.html#Resource-Usage-And-Limitation" title="Next chapter"> &gt;&gt; </a>]</td>
313
 
<td valign="middle" align="left"> &nbsp; </td>
314
 
<td valign="middle" align="left"> &nbsp; </td>
315
 
<td valign="middle" align="left"> &nbsp; </td>
316
 
<td valign="middle" align="left"> &nbsp; </td>
317
 
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
318
 
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
319
 
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
320
 
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
321
 
</tr></table>
322
 
<a name="Processor-And-CPU-Time-1"></a>
323
 
<h2 class="section">21.3 Processor And CPU Time</h2>
324
 
 
325
 
<p>If you&rsquo;re trying to optimize your program or measure its efficiency,
326
 
it&rsquo;s very useful to know how much processor time it uses.  For that,
327
 
calendar time and elapsed times are useless because a process may spend
328
 
time waiting for I/O or for other processes to use the CPU.  However,
329
 
you can get the information with the functions in this section.
330
 
</p>
331
 
<p>CPU time (see section <a href="#Time-Basics">Time Basics</a>) is represented by the data type
332
 
<code>clock_t</code>, which is a number of <em>clock ticks</em>.  It gives the
333
 
total amount of time a process has actively used a CPU since some
334
 
arbitrary event.  On the GNU system, that event is the creation of the
335
 
process.  While arbitrary in general, the event is always the same event
336
 
for any particular process, so you can always measure how much time on
337
 
the CPU a particular computation takes by examining the process&rsquo; CPU
338
 
time before and after the computation.
339
 
<a name="index-CPU-time-1"></a>
340
 
<a name="index-clock-ticks"></a>
341
 
<a name="index-ticks_002c-clock"></a>
342
 
</p>
343
 
<p>In the GNU system, <code>clock_t</code> is equivalent to <code>long int</code> and
344
 
<code>CLOCKS_PER_SEC</code> is an integer value.  But in other systems, both
345
 
<code>clock_t</code> and the macro <code>CLOCKS_PER_SEC</code> can be either integer
346
 
or floating-point types.  Casting CPU time values to <code>double</code>, as
347
 
in the example above, makes sure that operations such as arithmetic and
348
 
printing work properly and consistently no matter what the underlying
349
 
representation is.
350
 
</p>
351
 
<p>Note that the clock can wrap around.  On a 32bit system with
352
 
<code>CLOCKS_PER_SEC</code> set to one million this function will return the
353
 
same value approximately every 72 minutes.
354
 
</p>
355
 
<p>For additional functions to examine a process&rsquo; use of processor time,
356
 
and to control it, see <a href="libc_22.html#Resource-Usage-And-Limitation">Resource Usage And Limitation</a>.
357
 
</p>
358
 
 
359
 
<table class="menu" border="0" cellspacing="0">
360
 
<tr><td align="left" valign="top"><a href="#CPU-Time">21.3.1 CPU Time Inquiry</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">                    The <code>clock</code> function.
361
 
</td></tr>
362
 
<tr><td align="left" valign="top"><a href="#Processor-Time">21.3.2 Processor Time Inquiry</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">              The <code>times</code> function.
363
 
</td></tr>
364
 
</table>
365
 
 
366
 
<hr size="6">
367
 
<a name="CPU-Time"></a>
368
 
<table cellpadding="1" cellspacing="1" border="0">
369
 
<tr><td valign="middle" align="left">[<a href="#Processor-And-CPU-Time" title="Previous section in reading order"> &lt; </a>]</td>
370
 
<td valign="middle" align="left">[<a href="#Processor-Time" title="Next section in reading order"> &gt; </a>]</td>
371
 
<td valign="middle" align="left"> &nbsp; </td>
372
 
<td valign="middle" align="left">[<a href="#Date-and-Time" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
373
 
<td valign="middle" align="left">[<a href="#Processor-And-CPU-Time" title="Up section"> Up </a>]</td>
374
 
<td valign="middle" align="left">[<a href="libc_22.html#Resource-Usage-And-Limitation" title="Next chapter"> &gt;&gt; </a>]</td>
375
 
<td valign="middle" align="left"> &nbsp; </td>
376
 
<td valign="middle" align="left"> &nbsp; </td>
377
 
<td valign="middle" align="left"> &nbsp; </td>
378
 
<td valign="middle" align="left"> &nbsp; </td>
379
 
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
380
 
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
381
 
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
382
 
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
383
 
</tr></table>
384
 
<a name="CPU-Time-Inquiry"></a>
385
 
<h3 class="subsection">21.3.1 CPU Time Inquiry</h3>
386
 
 
387
 
<p>To get a process&rsquo; CPU time, you can use the <code>clock</code> function.  This
388
 
facility is declared in the header file &lsquo;<tt>time.h</tt>&rsquo;.
389
 
<a name="index-time_002eh-1"></a>
390
 
</p>
391
 
<p>In typical usage, you call the <code>clock</code> function at the beginning
392
 
and end of the interval you want to time, subtract the values, and then
393
 
divide by <code>CLOCKS_PER_SEC</code> (the number of clock ticks per second)
394
 
to get processor time, like this:
395
 
</p>
396
 
<table><tr><td>&nbsp;</td><td><pre class="smallexample">#include &lt;time.h&gt;
397
 
 
398
 
clock_t start, end;
399
 
double cpu_time_used;
400
 
 
401
 
start = clock();
402
 
&hellip; /* <span class="roman">Do the work.</span> */
403
 
end = clock();
404
 
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
405
 
</pre></td></tr></table>
406
 
 
407
 
<p>Do not use a single CPU time as an amount of time; it doesn&rsquo;t work that
408
 
way.  Either do a subtraction as shown above or query processor time
409
 
directly.  See section <a href="#Processor-Time">Processor Time Inquiry</a>.
410
 
</p>
411
 
<p>Different computers and operating systems vary wildly in how they keep
412
 
track of CPU time.  It&rsquo;s common for the internal processor clock
413
 
to have a resolution somewhere between a hundredth and millionth of a
414
 
second.
415
 
</p>
416
 
<dl>
417
 
<dt><a name="index-CLOCKS_005fPER_005fSEC"></a><u>Macro:</u> int <b>CLOCKS_PER_SEC</b></dt>
418
 
<dd><p>The value of this macro is the number of clock ticks per second measured
419
 
by the <code>clock</code> function.  POSIX requires that this value be one
420
 
million independent of the actual resolution.
421
 
</p></dd></dl>
422
 
 
423
 
<dl>
424
 
<dt><a name="index-CLK_005fTCK"></a><u>Macro:</u> int <b>CLK_TCK</b></dt>
425
 
<dd><p>This is an obsolete name for <code>CLOCKS_PER_SEC</code>.
426
 
</p></dd></dl>
427
 
 
428
 
<dl>
429
 
<dt><a name="index-clock_005ft"></a><u>Data Type:</u> <b>clock_t</b></dt>
430
 
<dd><p>This is the type of the value returned by the <code>clock</code> function.
431
 
Values of type <code>clock_t</code> are numbers of clock ticks.
432
 
</p></dd></dl>
433
 
 
434
 
<dl>
435
 
<dt><a name="index-clock"></a><u>Function:</u> clock_t <b>clock</b><i> (void)</i></dt>
436
 
<dd><p>This function returns the calling process&rsquo; current CPU time.  If the CPU
437
 
time is not available or cannot be represented, <code>clock</code> returns the
438
 
value <code>(clock_t)(-1)</code>.
439
 
</p></dd></dl>
440
 
 
441
 
 
442
 
<hr size="6">
443
 
<a name="Processor-Time"></a>
444
 
<table cellpadding="1" cellspacing="1" border="0">
445
 
<tr><td valign="middle" align="left">[<a href="#CPU-Time" title="Previous section in reading order"> &lt; </a>]</td>
446
 
<td valign="middle" align="left">[<a href="#Calendar-Time" title="Next section in reading order"> &gt; </a>]</td>
447
 
<td valign="middle" align="left"> &nbsp; </td>
448
 
<td valign="middle" align="left">[<a href="#Date-and-Time" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
449
 
<td valign="middle" align="left">[<a href="#Processor-And-CPU-Time" title="Up section"> Up </a>]</td>
450
 
<td valign="middle" align="left">[<a href="libc_22.html#Resource-Usage-And-Limitation" title="Next chapter"> &gt;&gt; </a>]</td>
451
 
<td valign="middle" align="left"> &nbsp; </td>
452
 
<td valign="middle" align="left"> &nbsp; </td>
453
 
<td valign="middle" align="left"> &nbsp; </td>
454
 
<td valign="middle" align="left"> &nbsp; </td>
455
 
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
456
 
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
457
 
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
458
 
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
459
 
</tr></table>
460
 
<a name="Processor-Time-Inquiry"></a>
461
 
<h3 class="subsection">21.3.2 Processor Time Inquiry</h3>
462
 
 
463
 
<p>The <code>times</code> function returns information about a process&rsquo;
464
 
consumption of processor time in a <code>struct tms</code> object, in
465
 
addition to the process&rsquo; CPU time.  See section <a href="#Time-Basics">Time Basics</a>.  You should
466
 
include the header file &lsquo;<tt>sys/times.h</tt>&rsquo; to use this facility.
467
 
<a name="index-processor-time-1"></a>
468
 
<a name="index-CPU-time-2"></a>
469
 
<a name="index-sys_002ftimes_002eh-1"></a>
470
 
</p>
471
 
<dl>
472
 
<dt><a name="index-struct-tms"></a><u>Data Type:</u> <b>struct tms</b></dt>
473
 
<dd><p>The <code>tms</code> structure is used to return information about process
474
 
times.  It contains at least the following members:
475
 
</p>
476
 
<dl compact="compact">
477
 
<dt> <code>clock_t tms_utime</code></dt>
478
 
<dd><p>This is the total processor time the calling process has used in
479
 
executing the instructions of its program.
480
 
</p>
481
 
</dd>
482
 
<dt> <code>clock_t tms_stime</code></dt>
483
 
<dd><p>This is the processor time the system has used on behalf of the calling
484
 
process.
485
 
</p>
486
 
</dd>
487
 
<dt> <code>clock_t tms_cutime</code></dt>
488
 
<dd><p>This is the sum of the <code>tms_utime</code> values and the <code>tms_cutime</code>
489
 
values of all terminated child processes of the calling process, whose
490
 
status has been reported to the parent process by <code>wait</code> or
491
 
<code>waitpid</code>; see <a href="libc_26.html#Process-Completion">Process Completion</a>.  In other words, it
492
 
represents the total processor time used in executing the instructions
493
 
of all the terminated child processes of the calling process, excluding
494
 
child processes which have not yet been reported by <code>wait</code> or
495
 
<code>waitpid</code>.
496
 
<a name="index-child-process"></a>
497
 
</p>
498
 
</dd>
499
 
<dt> <code>clock_t tms_cstime</code></dt>
500
 
<dd><p>This is similar to <code>tms_cutime</code>, but represents the total processor
501
 
time system has used on behalf of all the terminated child processes
502
 
of the calling process.
503
 
</p></dd>
504
 
</dl>
505
 
 
506
 
<p>All of the times are given in numbers of clock ticks.  Unlike CPU time,
507
 
these are the actual amounts of time; not relative to any event.
508
 
See section <a href="libc_26.html#Creating-a-Process">Creating a Process</a>.
509
 
</p></dd></dl>
510
 
 
511
 
<dl>
512
 
<dt><a name="index-times"></a><u>Function:</u> clock_t <b>times</b><i> (struct tms *<var>buffer</var>)</i></dt>
513
 
<dd><p>The <code>times</code> function stores the processor time information for
514
 
the calling process in <var>buffer</var>.
515
 
</p>
516
 
<p>The return value is the calling process&rsquo; CPU time (the same value you
517
 
get from <code>clock()</code>.  <code>times</code> returns <code>(clock_t)(-1)</code> to
518
 
indicate failure.
519
 
</p></dd></dl>
520
 
 
521
 
<p><strong>Portability Note:</strong> The <code>clock</code> function described in
522
 
<a href="#CPU-Time">CPU Time Inquiry</a> is specified by the ISO C standard.  The
523
 
<code>times</code> function is a feature of POSIX.1.  In the GNU system, the
524
 
CPU time is defined to be equivalent to the sum of the <code>tms_utime</code>
525
 
and <code>tms_stime</code> fields returned by <code>times</code>.
526
 
</p>
527
 
<hr size="6">
528
 
<a name="Calendar-Time"></a>
529
 
<table cellpadding="1" cellspacing="1" border="0">
530
 
<tr><td valign="middle" align="left">[<a href="#Processor-Time" title="Previous section in reading order"> &lt; </a>]</td>
531
 
<td valign="middle" align="left">[<a href="#Simple-Calendar-Time" title="Next section in reading order"> &gt; </a>]</td>
532
 
<td valign="middle" align="left"> &nbsp; </td>
533
 
<td valign="middle" align="left">[<a href="#Date-and-Time" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
534
 
<td valign="middle" align="left">[<a href="#Date-and-Time" title="Up section"> Up </a>]</td>
535
 
<td valign="middle" align="left">[<a href="libc_22.html#Resource-Usage-And-Limitation" title="Next chapter"> &gt;&gt; </a>]</td>
536
 
<td valign="middle" align="left"> &nbsp; </td>
537
 
<td valign="middle" align="left"> &nbsp; </td>
538
 
<td valign="middle" align="left"> &nbsp; </td>
539
 
<td valign="middle" align="left"> &nbsp; </td>
540
 
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
541
 
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
542
 
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
543
 
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
544
 
</tr></table>
545
 
<a name="Calendar-Time-1"></a>
546
 
<h2 class="section">21.4 Calendar Time</h2>
547
 
 
548
 
<p>This section describes facilities for keeping track of calendar time.
549
 
See section <a href="#Time-Basics">Time Basics</a>.
550
 
</p>
551
 
<p>The GNU C library represents calendar time three ways:
552
 
</p>
553
 
<ul>
554
 
<li>
555
 
<em>Simple time</em> (the <code>time_t</code> data type) is a compact
556
 
representation, typically giving the number of seconds of elapsed time
557
 
since some implementation-specific base time.
558
 
<a name="index-simple-time"></a>
559
 
 
560
 
</li><li>
561
 
There is also a &quot;high-resolution time&quot; representation.  Like simple
562
 
time, this represents a calendar time as an elapsed time since a base
563
 
time, but instead of measuring in whole seconds, it uses a <code>struct
564
 
timeval</code> data type, which includes fractions of a second.  Use this time
565
 
representation instead of simple time when you need greater precision.
566
 
<a name="index-high_002dresolution-time"></a>
567
 
 
568
 
</li><li>
569
 
<em>Local time</em> or <em>broken-down time</em> (the <code>struct tm</code> data
570
 
type) represents a calendar time as a set of components specifying the
571
 
year, month, and so on in the Gregorian calendar, for a specific time
572
 
zone.  This calendar time representation is usually used only to
573
 
communicate with people.
574
 
<a name="index-local-time"></a>
575
 
<a name="index-broken_002ddown-time"></a>
576
 
<a name="index-Gregorian-calendar"></a>
577
 
<a name="index-calendar_002c-Gregorian"></a>
578
 
</li></ul>
579
 
 
580
 
<table class="menu" border="0" cellspacing="0">
581
 
<tr><td align="left" valign="top"><a href="#Simple-Calendar-Time">21.4.1 Simple Calendar Time</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">        Facilities for manipulating calendar time.
582
 
</td></tr>
583
 
<tr><td align="left" valign="top"><a href="#High_002dResolution-Calendar">21.4.2 High-Resolution Calendar</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">    A time representation with greater precision.
584
 
</td></tr>
585
 
<tr><td align="left" valign="top"><a href="#Broken_002ddown-Time">21.4.3 Broken-down Time</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">            Facilities for manipulating local time.
586
 
</td></tr>
587
 
<tr><td align="left" valign="top"><a href="#High-Accuracy-Clock">21.4.4 High Accuracy Clock</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">         Maintaining a high accuracy system clock.
588
 
</td></tr>
589
 
<tr><td align="left" valign="top"><a href="#Formatting-Calendar-Time">21.4.5 Formatting Calendar Time</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">    Converting times to strings.
590
 
</td></tr>
591
 
<tr><td align="left" valign="top"><a href="#Parsing-Date-and-Time">21.4.6 Convert textual time and date information back</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">       Convert textual time and date information back
592
 
                                 into broken-down time values.
593
 
</td></tr>
594
 
<tr><td align="left" valign="top"><a href="#TZ-Variable">21.4.7 Specifying the Time Zone with <code>TZ</code></a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">                 How users specify the time zone.
595
 
</td></tr>
596
 
<tr><td align="left" valign="top"><a href="#Time-Zone-Functions">21.4.8 Functions and Variables for Time Zones</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">         Functions to examine or specify the time zone.
597
 
</td></tr>
598
 
<tr><td align="left" valign="top"><a href="#Time-Functions-Example">21.4.9 Time Functions Example</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">      An example program showing use of some of
599
 
                                 the time functions.
600
 
</td></tr>
601
 
</table>
602
 
 
603
 
<hr size="6">
604
 
<a name="Simple-Calendar-Time"></a>
605
 
<table cellpadding="1" cellspacing="1" border="0">
606
 
<tr><td valign="middle" align="left">[<a href="#Calendar-Time" title="Previous section in reading order"> &lt; </a>]</td>
607
 
<td valign="middle" align="left">[<a href="#High_002dResolution-Calendar" title="Next section in reading order"> &gt; </a>]</td>
608
 
<td valign="middle" align="left"> &nbsp; </td>
609
 
<td valign="middle" align="left">[<a href="#Date-and-Time" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
610
 
<td valign="middle" align="left">[<a href="#Calendar-Time" title="Up section"> Up </a>]</td>
611
 
<td valign="middle" align="left">[<a href="libc_22.html#Resource-Usage-And-Limitation" title="Next chapter"> &gt;&gt; </a>]</td>
612
 
<td valign="middle" align="left"> &nbsp; </td>
613
 
<td valign="middle" align="left"> &nbsp; </td>
614
 
<td valign="middle" align="left"> &nbsp; </td>
615
 
<td valign="middle" align="left"> &nbsp; </td>
616
 
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
617
 
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
618
 
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
619
 
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
620
 
</tr></table>
621
 
<a name="Simple-Calendar-Time-1"></a>
622
 
<h3 class="subsection">21.4.1 Simple Calendar Time</h3>
623
 
 
624
 
<p>This section describes the <code>time_t</code> data type for representing calendar
625
 
time as simple time, and the functions which operate on simple time objects.
626
 
These facilities are declared in the header file &lsquo;<tt>time.h</tt>&rsquo;.
627
 
<a name="index-time_002eh-2"></a>
628
 
</p>
629
 
<a name="index-epoch"></a>
630
 
<dl>
631
 
<dt><a name="index-time_005ft"></a><u>Data Type:</u> <b>time_t</b></dt>
632
 
<dd><p>This is the data type used to represent simple time.  Sometimes, it also
633
 
represents an elapsed time.  When interpreted as a calendar time value,
634
 
it represents the number of seconds elapsed since 00:00:00 on January 1,
635
 
1970, Coordinated Universal Time.  (This calendar time is sometimes
636
 
referred to as the <em>epoch</em>.)  POSIX requires that this count not
637
 
include leap seconds, but on some systems this count includes leap seconds
638
 
if you set <code>TZ</code> to certain values (see section <a href="#TZ-Variable">Specifying the Time Zone with <code>TZ</code></a>).
639
 
</p>
640
 
<p>Note that a simple time has no concept of local time zone.  Calendar
641
 
Time <var>T</var> is the same instant in time regardless of where on the
642
 
globe the computer is.
643
 
</p>
644
 
<p>In the GNU C library, <code>time_t</code> is equivalent to <code>long int</code>.
645
 
In other systems, <code>time_t</code> might be either an integer or
646
 
floating-point type.
647
 
</p></dd></dl>
648
 
 
649
 
<p>The function <code>difftime</code> tells you the elapsed time between two
650
 
simple calendar times, which is not always as easy to compute as just
651
 
subtracting.  See section <a href="#Elapsed-Time">Elapsed Time</a>.
652
 
</p>
653
 
<dl>
654
 
<dt><a name="index-time-1"></a><u>Function:</u> time_t <b>time</b><i> (time_t *<var>result</var>)</i></dt>
655
 
<dd><p>The <code>time</code> function returns the current calendar time as a value of
656
 
type <code>time_t</code>.  If the argument <var>result</var> is not a null pointer,
657
 
the calendar time value is also stored in <code>*<var>result</var></code>.  If the
658
 
current calendar time is not available, the value
659
 
<code>(time_t)(-1)</code> is returned.
660
 
</p></dd></dl>
661
 
 
662
 
<dl>
663
 
<dt><a name="index-stime"></a><u>Function:</u> int <b>stime</b><i> (time_t *<var>newtime</var>)</i></dt>
664
 
<dd><p><code>stime</code> sets the system clock, i.e., it tells the system that the
665
 
current calendar time is <var>newtime</var>, where <code>newtime</code> is
666
 
interpreted as described in the above definition of <code>time_t</code>.
667
 
</p>
668
 
<p><code>settimeofday</code> is a newer function which sets the system clock to
669
 
better than one second precision.  <code>settimeofday</code> is generally a
670
 
better choice than <code>stime</code>.  See section <a href="#High_002dResolution-Calendar">High-Resolution Calendar</a>.
671
 
</p>
672
 
<p>Only the superuser can set the system clock.
673
 
</p>
674
 
<p>If the function succeeds, the return value is zero.  Otherwise, it is
675
 
<code>-1</code> and <code>errno</code> is set accordingly:
676
 
</p>
677
 
<dl compact="compact">
678
 
<dt> <code>EPERM</code></dt>
679
 
<dd><p>The process is not superuser.
680
 
</p></dd>
681
 
</dl>
682
 
</dd></dl>
683
 
 
684
 
 
685
 
 
686
 
<hr size="6">
687
 
<a name="High_002dResolution-Calendar"></a>
688
 
<table cellpadding="1" cellspacing="1" border="0">
689
 
<tr><td valign="middle" align="left">[<a href="#Simple-Calendar-Time" title="Previous section in reading order"> &lt; </a>]</td>
690
 
<td valign="middle" align="left">[<a href="#Broken_002ddown-Time" title="Next section in reading order"> &gt; </a>]</td>
691
 
<td valign="middle" align="left"> &nbsp; </td>
692
 
<td valign="middle" align="left">[<a href="#Date-and-Time" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
693
 
<td valign="middle" align="left">[<a href="#Calendar-Time" title="Up section"> Up </a>]</td>
694
 
<td valign="middle" align="left">[<a href="libc_22.html#Resource-Usage-And-Limitation" title="Next chapter"> &gt;&gt; </a>]</td>
695
 
<td valign="middle" align="left"> &nbsp; </td>
696
 
<td valign="middle" align="left"> &nbsp; </td>
697
 
<td valign="middle" align="left"> &nbsp; </td>
698
 
<td valign="middle" align="left"> &nbsp; </td>
699
 
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
700
 
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
701
 
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
702
 
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
703
 
</tr></table>
704
 
<a name="High_002dResolution-Calendar-1"></a>
705
 
<h3 class="subsection">21.4.2 High-Resolution Calendar</h3>
706
 
 
707
 
<p>The <code>time_t</code> data type used to represent simple times has a
708
 
resolution of only one second.  Some applications need more precision.
709
 
</p>
710
 
<p>So, the GNU C library also contains functions which are capable of
711
 
representing calendar times to a higher resolution than one second.  The
712
 
functions and the associated data types described in this section are
713
 
declared in &lsquo;<tt>sys/time.h</tt>&rsquo;.
714
 
<a name="index-sys_002ftime_002eh-1"></a>
715
 
</p>
716
 
<dl>
717
 
<dt><a name="index-struct-timezone"></a><u>Data Type:</u> <b>struct timezone</b></dt>
718
 
<dd><p>The <code>struct timezone</code> structure is used to hold minimal information
719
 
about the local time zone.  It has the following members:
720
 
</p>
721
 
<dl compact="compact">
722
 
<dt> <code>int tz_minuteswest</code></dt>
723
 
<dd><p>This is the number of minutes west of UTC.
724
 
</p>
725
 
</dd>
726
 
<dt> <code>int tz_dsttime</code></dt>
727
 
<dd><p>If nonzero, Daylight Saving Time applies during some part of the year.
728
 
</p></dd>
729
 
</dl>
730
 
 
731
 
<p>The <code>struct timezone</code> type is obsolete and should never be used.
732
 
Instead, use the facilities described in <a href="#Time-Zone-Functions">Functions and Variables for Time Zones</a>.
733
 
</p></dd></dl>
734
 
 
735
 
<dl>
736
 
<dt><a name="index-gettimeofday"></a><u>Function:</u> int <b>gettimeofday</b><i> (struct timeval *<var>tp</var>, struct timezone *<var>tzp</var>)</i></dt>
737
 
<dd><p>The <code>gettimeofday</code> function returns the current calendar time as
738
 
the elapsed time since the epoch in the <code>struct timeval</code> structure
739
 
indicated by <var>tp</var>.  (see section <a href="#Elapsed-Time">Elapsed Time</a> for a description of
740
 
<code>struct timeval</code>).  Information about the time zone is returned in
741
 
the structure pointed at <var>tzp</var>.  If the <var>tzp</var> argument is a null
742
 
pointer, time zone information is ignored.
743
 
</p>
744
 
<p>The return value is <code>0</code> on success and <code>-1</code> on failure.  The
745
 
following <code>errno</code> error condition is defined for this function:
746
 
</p>
747
 
<dl compact="compact">
748
 
<dt> <code>ENOSYS</code></dt>
749
 
<dd><p>The operating system does not support getting time zone information, and
750
 
<var>tzp</var> is not a null pointer.  The GNU operating system does not
751
 
support using <code>struct timezone</code> to represent time zone
752
 
information; that is an obsolete feature of 4.3 BSD.
753
 
Instead, use the facilities described in <a href="#Time-Zone-Functions">Functions and Variables for Time Zones</a>.
754
 
</p></dd>
755
 
</dl>
756
 
</dd></dl>
757
 
 
758
 
<dl>
759
 
<dt><a name="index-settimeofday"></a><u>Function:</u> int <b>settimeofday</b><i> (const struct timeval *<var>tp</var>, const struct timezone *<var>tzp</var>)</i></dt>
760
 
<dd><p>The <code>settimeofday</code> function sets the current calendar time in the
761
 
system clock according to the arguments.  As for <code>gettimeofday</code>,
762
 
the calendar time is represented as the elapsed time since the epoch.
763
 
As for <code>gettimeofday</code>, time zone information is ignored if
764
 
<var>tzp</var> is a null pointer.
765
 
</p>
766
 
<p>You must be a privileged user in order to use <code>settimeofday</code>.
767
 
</p>
768
 
<p>Some kernels automatically set the system clock from some source such as
769
 
a hardware clock when they start up.  Others, including Linux, place the
770
 
system clock in an &ldquo;invalid&rdquo; state (in which attempts to read the clock
771
 
fail).  A call of <code>stime</code> removes the system clock from an invalid
772
 
state, and system startup scripts typically run a program that calls
773
 
<code>stime</code>.
774
 
</p>
775
 
<p><code>settimeofday</code> causes a sudden jump forwards or backwards, which
776
 
can cause a variety of problems in a system.  Use <code>adjtime</code> (below)
777
 
to make a smooth transition from one time to another by temporarily
778
 
speeding up or slowing down the clock.
779
 
</p>
780
 
<p>With a Linux kernel, <code>adjtimex</code> does the same thing and can also
781
 
make permanent changes to the speed of the system clock so it doesn&rsquo;t
782
 
need to be corrected as often.
783
 
</p>
784
 
<p>The return value is <code>0</code> on success and <code>-1</code> on failure.  The
785
 
following <code>errno</code> error conditions are defined for this function:
786
 
</p>
787
 
<dl compact="compact">
788
 
<dt> <code>EPERM</code></dt>
789
 
<dd><p>This process cannot set the clock because it is not privileged.
790
 
</p>
791
 
</dd>
792
 
<dt> <code>ENOSYS</code></dt>
793
 
<dd><p>The operating system does not support setting time zone information, and
794
 
<var>tzp</var> is not a null pointer.
795
 
</p></dd>
796
 
</dl>
797
 
</dd></dl>
798
 
 
799
 
<dl>
800
 
<dt><a name="index-adjtime"></a><u>Function:</u> int <b>adjtime</b><i> (const struct timeval *<var>delta</var>, struct timeval *<var>olddelta</var>)</i></dt>
801
 
<dd><p>This function speeds up or slows down the system clock in order to make
802
 
a gradual adjustment.  This ensures that the calendar time reported by
803
 
the system clock is always monotonically increasing, which might not
804
 
happen if you simply set the clock.
805
 
</p>
806
 
<p>The <var>delta</var> argument specifies a relative adjustment to be made to
807
 
the clock time.  If negative, the system clock is slowed down for a
808
 
while until it has lost this much elapsed time.  If positive, the system
809
 
clock is speeded up for a while.
810
 
</p>
811
 
<p>If the <var>olddelta</var> argument is not a null pointer, the <code>adjtime</code>
812
 
function returns information about any previous time adjustment that
813
 
has not yet completed.
814
 
</p>
815
 
<p>This function is typically used to synchronize the clocks of computers
816
 
in a local network.  You must be a privileged user to use it.
817
 
</p>
818
 
<p>With a Linux kernel, you can use the <code>adjtimex</code> function to
819
 
permanently change the clock speed.
820
 
</p>
821
 
<p>The return value is <code>0</code> on success and <code>-1</code> on failure.  The
822
 
following <code>errno</code> error condition is defined for this function:
823
 
</p>
824
 
<dl compact="compact">
825
 
<dt> <code>EPERM</code></dt>
826
 
<dd><p>You do not have privilege to set the time.
827
 
</p></dd>
828
 
</dl>
829
 
</dd></dl>
830
 
 
831
 
<p><strong>Portability Note:</strong>  The <code>gettimeofday</code>, <code>settimeofday</code>,
832
 
and <code>adjtime</code> functions are derived from BSD.
833
 
</p>
834
 
 
835
 
<p>Symbols for the following function are declared in &lsquo;<tt>sys/timex.h</tt>&rsquo;.
836
 
</p>
837
 
<dl>
838
 
<dt><a name="index-adjtimex"></a><u>Function:</u> int <b>adjtimex</b><i> (struct timex *<var>timex</var>)</i></dt>
839
 
<dd>
840
 
<p><code>adjtimex</code> is functionally identical to <code>ntp_adjtime</code>.
841
 
See section <a href="#High-Accuracy-Clock">High Accuracy Clock</a>.
842
 
</p>
843
 
<p>This function is present only with a Linux kernel.
844
 
</p>
845
 
</dd></dl>
846
 
 
847
 
<hr size="6">
848
 
<a name="Broken_002ddown-Time"></a>
849
 
<table cellpadding="1" cellspacing="1" border="0">
850
 
<tr><td valign="middle" align="left">[<a href="#High_002dResolution-Calendar" title="Previous section in reading order"> &lt; </a>]</td>
851
 
<td valign="middle" align="left">[<a href="#High-Accuracy-Clock" title="Next section in reading order"> &gt; </a>]</td>
852
 
<td valign="middle" align="left"> &nbsp; </td>
853
 
<td valign="middle" align="left">[<a href="#Date-and-Time" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
854
 
<td valign="middle" align="left">[<a href="#Calendar-Time" title="Up section"> Up </a>]</td>
855
 
<td valign="middle" align="left">[<a href="libc_22.html#Resource-Usage-And-Limitation" title="Next chapter"> &gt;&gt; </a>]</td>
856
 
<td valign="middle" align="left"> &nbsp; </td>
857
 
<td valign="middle" align="left"> &nbsp; </td>
858
 
<td valign="middle" align="left"> &nbsp; </td>
859
 
<td valign="middle" align="left"> &nbsp; </td>
860
 
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
861
 
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
862
 
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
863
 
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
864
 
</tr></table>
865
 
<a name="Broken_002ddown-Time-1"></a>
866
 
<h3 class="subsection">21.4.3 Broken-down Time</h3>
867
 
<a name="index-broken_002ddown-time-1"></a>
868
 
<a name="index-calendar-time-and-broken_002ddown-time"></a>
869
 
 
870
 
<p>Calendar time is represented by the usual GNU C library functions as an
871
 
elapsed time since a fixed base calendar time.  This is convenient for
872
 
computation, but has no relation to the way people normally think of
873
 
calendar time.  By contrast, <em>broken-down time</em> is a binary
874
 
representation of calendar time separated into year, month, day, and so
875
 
on.  Broken-down time values are not useful for calculations, but they
876
 
are useful for printing human readable time information.
877
 
</p>
878
 
<p>A broken-down time value is always relative to a choice of time
879
 
zone, and it also indicates which time zone that is.
880
 
</p>
881
 
<p>The symbols in this section are declared in the header file &lsquo;<tt>time.h</tt>&rsquo;.
882
 
</p>
883
 
<dl>
884
 
<dt><a name="index-struct-tm"></a><u>Data Type:</u> <b>struct tm</b></dt>
885
 
<dd><p>This is the data type used to represent a broken-down time.  The structure
886
 
contains at least the following members, which can appear in any order.
887
 
</p>
888
 
<dl compact="compact">
889
 
<dt> <code>int tm_sec</code></dt>
890
 
<dd><p>This is the number of full seconds since the top of the minute (normally
891
 
in the range <code>0</code> through <code>59</code>, but the actual upper limit is
892
 
<code>60</code>, to allow for leap seconds if leap second support is
893
 
available).
894
 
<a name="index-leap-second"></a>
895
 
</p>
896
 
</dd>
897
 
<dt> <code>int tm_min</code></dt>
898
 
<dd><p>This is the number of full minutes since the top of the hour (in the
899
 
range <code>0</code> through <code>59</code>).
900
 
</p>
901
 
</dd>
902
 
<dt> <code>int tm_hour</code></dt>
903
 
<dd><p>This is the number of full hours past midnight (in the range <code>0</code> through
904
 
<code>23</code>).
905
 
</p>
906
 
</dd>
907
 
<dt> <code>int tm_mday</code></dt>
908
 
<dd><p>This is the ordinal day of the month (in the range <code>1</code> through <code>31</code>).
909
 
Watch out for this one!  As the only ordinal number in the structure, it is
910
 
inconsistent with the rest of the structure.
911
 
</p>
912
 
</dd>
913
 
<dt> <code>int tm_mon</code></dt>
914
 
<dd><p>This is the number of full calendar months since the beginning of the
915
 
year (in the range <code>0</code> through <code>11</code>).  Watch out for this one!
916
 
People usually use ordinal numbers for month-of-year (where January = 1).
917
 
</p>
918
 
</dd>
919
 
<dt> <code>int tm_year</code></dt>
920
 
<dd><p>This is the number of full calendar years since 1900.
921
 
</p>
922
 
</dd>
923
 
<dt> <code>int tm_wday</code></dt>
924
 
<dd><p>This is the number of full days since Sunday (in the range <code>0</code> through
925
 
<code>6</code>).
926
 
</p>
927
 
</dd>
928
 
<dt> <code>int tm_yday</code></dt>
929
 
<dd><p>This is the number of full days since the beginning of the year (in the
930
 
range <code>0</code> through <code>365</code>).
931
 
</p>
932
 
</dd>
933
 
<dt> <code>int tm_isdst</code></dt>
934
 
<dd><a name="index-Daylight-Saving-Time"></a>
935
 
<a name="index-summer-time"></a>
936
 
<p>This is a flag that indicates whether Daylight Saving Time is (or was, or
937
 
will be) in effect at the time described.  The value is positive if
938
 
Daylight Saving Time is in effect, zero if it is not, and negative if the
939
 
information is not available.
940
 
</p>
941
 
</dd>
942
 
<dt> <code>long int tm_gmtoff</code></dt>
943
 
<dd><p>This field describes the time zone that was used to compute this
944
 
broken-down time value, including any adjustment for daylight saving; it
945
 
is the number of seconds that you must add to UTC to get local time.
946
 
You can also think of this as the number of seconds east of UTC.  For
947
 
example, for U.S. Eastern Standard Time, the value is <code>-5*60*60</code>.
948
 
The <code>tm_gmtoff</code> field is derived from BSD and is a GNU library
949
 
extension; it is not visible in a strict ISO C environment.
950
 
</p>
951
 
</dd>
952
 
<dt> <code>const char *tm_zone</code></dt>
953
 
<dd><p>This field is the name for the time zone that was used to compute this
954
 
broken-down time value.  Like <code>tm_gmtoff</code>, this field is a BSD and
955
 
GNU extension, and is not visible in a strict ISO C environment.
956
 
</p></dd>
957
 
</dl>
958
 
</dd></dl>
959
 
 
960
 
 
961
 
<dl>
962
 
<dt><a name="index-localtime"></a><u>Function:</u> struct tm * <b>localtime</b><i> (const time_t *<var>time</var>)</i></dt>
963
 
<dd><p>The <code>localtime</code> function converts the simple time pointed to by
964
 
<var>time</var> to broken-down time representation, expressed relative to the
965
 
user&rsquo;s specified time zone.
966
 
</p>
967
 
<p>The return value is a pointer to a static broken-down time structure, which
968
 
might be overwritten by subsequent calls to <code>ctime</code>, <code>gmtime</code>,
969
 
or <code>localtime</code>.  (But no other library function overwrites the contents
970
 
of this object.)
971
 
</p>
972
 
<p>The return value is the null pointer if <var>time</var> cannot be represented
973
 
as a broken-down time; typically this is because the year cannot fit into
974
 
an <code>int</code>.
975
 
</p>
976
 
<p>Calling <code>localtime</code> has one other effect: it sets the variable
977
 
<code>tzname</code> with information about the current time zone.  See section <a href="#Time-Zone-Functions">Functions and Variables for Time Zones</a>.
978
 
</p></dd></dl>
979
 
 
980
 
<p>Using the <code>localtime</code> function is a big problem in multi-threaded
981
 
programs.  The result is returned in a static buffer and this is used in
982
 
all threads.  POSIX.1c introduced a variant of this function.
983
 
</p>
984
 
<dl>
985
 
<dt><a name="index-localtime_005fr"></a><u>Function:</u> struct tm * <b>localtime_r</b><i> (const time_t *<var>time</var>, struct tm *<var>resultp</var>)</i></dt>
986
 
<dd><p>The <code>localtime_r</code> function works just like the <code>localtime</code>
987
 
function.  It takes a pointer to a variable containing a simple time
988
 
and converts it to the broken-down time format.
989
 
</p>
990
 
<p>But the result is not placed in a static buffer.  Instead it is placed
991
 
in the object of type <code>struct tm</code> to which the parameter
992
 
<var>resultp</var> points.
993
 
</p>
994
 
<p>If the conversion is successful the function returns a pointer to the
995
 
object the result was written into, i.e., it returns <var>resultp</var>.
996
 
</p></dd></dl>
997
 
 
998
 
 
999
 
<dl>
1000
 
<dt><a name="index-gmtime"></a><u>Function:</u> struct tm * <b>gmtime</b><i> (const time_t *<var>time</var>)</i></dt>
1001
 
<dd><p>This function is similar to <code>localtime</code>, except that the broken-down
1002
 
time is expressed as Coordinated Universal Time (UTC) (formerly called
1003
 
Greenwich Mean Time (GMT)) rather than relative to a local time zone.
1004
 
</p>
1005
 
</dd></dl>
1006
 
 
1007
 
<p>As for the <code>localtime</code> function we have the problem that the result
1008
 
is placed in a static variable.  POSIX.1c also provides a replacement for
1009
 
<code>gmtime</code>.
1010
 
</p>
1011
 
<dl>
1012
 
<dt><a name="index-gmtime_005fr"></a><u>Function:</u> struct tm * <b>gmtime_r</b><i> (const time_t *<var>time</var>, struct tm *<var>resultp</var>)</i></dt>
1013
 
<dd><p>This function is similar to <code>localtime_r</code>, except that it converts
1014
 
just like <code>gmtime</code> the given time as Coordinated Universal Time.
1015
 
</p>
1016
 
<p>If the conversion is successful the function returns a pointer to the
1017
 
object the result was written into, i.e., it returns <var>resultp</var>.
1018
 
</p></dd></dl>
1019
 
 
1020
 
 
1021
 
<dl>
1022
 
<dt><a name="index-mktime"></a><u>Function:</u> time_t <b>mktime</b><i> (struct tm *<var>brokentime</var>)</i></dt>
1023
 
<dd><p>The <code>mktime</code> function is used to convert a broken-down time structure
1024
 
to a simple time representation.  It also &ldquo;normalizes&rdquo; the contents of
1025
 
the broken-down time structure, by filling in the day of week and day of
1026
 
year based on the other date and time components.
1027
 
</p>
1028
 
<p>The <code>mktime</code> function ignores the specified contents of the
1029
 
<code>tm_wday</code> and <code>tm_yday</code> members of the broken-down time
1030
 
structure.  It uses the values of the other components to determine the
1031
 
calendar time; it&rsquo;s permissible for these components to have
1032
 
unnormalized values outside their normal ranges.  The last thing that
1033
 
<code>mktime</code> does is adjust the components of the <var>brokentime</var>
1034
 
structure (including the <code>tm_wday</code> and <code>tm_yday</code>).
1035
 
</p>
1036
 
<p>If the specified broken-down time cannot be represented as a simple time,
1037
 
<code>mktime</code> returns a value of <code>(time_t)(-1)</code> and does not modify
1038
 
the contents of <var>brokentime</var>.
1039
 
</p>
1040
 
<p>Calling <code>mktime</code> also sets the variable <code>tzname</code> with
1041
 
information about the current time zone.  See section <a href="#Time-Zone-Functions">Functions and Variables for Time Zones</a>.
1042
 
</p></dd></dl>
1043
 
 
1044
 
<dl>
1045
 
<dt><a name="index-timelocal"></a><u>Function:</u> time_t <b>timelocal</b><i> (struct tm *<var>brokentime</var>)</i></dt>
1046
 
<dd>
1047
 
<p><code>timelocal</code> is functionally identical to <code>mktime</code>, but more
1048
 
mnemonically named.  Note that it is the inverse of the <code>localtime</code>
1049
 
function.
1050
 
</p>
1051
 
<p><strong>Portability note:</strong>  <code>mktime</code> is essentially universally
1052
 
available.  <code>timelocal</code> is rather rare.
1053
 
</p>
1054
 
</dd></dl>
1055
 
 
1056
 
<dl>
1057
 
<dt><a name="index-timegm"></a><u>Function:</u> time_t <b>timegm</b><i> (struct tm *<var>brokentime</var>)</i></dt>
1058
 
<dd>
1059
 
<p><code>timegm</code> is functionally identical to <code>mktime</code> except it
1060
 
always takes the input values to be Coordinated Universal Time (UTC)
1061
 
regardless of any local time zone setting.
1062
 
</p>
1063
 
<p>Note that <code>timegm</code> is the inverse of <code>gmtime</code>.
1064
 
</p>
1065
 
<p><strong>Portability note:</strong>  <code>mktime</code> is essentially universally
1066
 
available.  <code>timegm</code> is rather rare.  For the most portable
1067
 
conversion from a UTC broken-down time to a simple time, set
1068
 
the <code>TZ</code> environment variable to UTC, call <code>mktime</code>, then set
1069
 
<code>TZ</code> back.
1070
 
</p>
1071
 
</dd></dl>
1072
 
 
1073
 
 
1074
 
 
1075
 
<hr size="6">
1076
 
<a name="High-Accuracy-Clock"></a>
1077
 
<table cellpadding="1" cellspacing="1" border="0">
1078
 
<tr><td valign="middle" align="left">[<a href="#Broken_002ddown-Time" title="Previous section in reading order"> &lt; </a>]</td>
1079
 
<td valign="middle" align="left">[<a href="#Formatting-Calendar-Time" title="Next section in reading order"> &gt; </a>]</td>
1080
 
<td valign="middle" align="left"> &nbsp; </td>
1081
 
<td valign="middle" align="left">[<a href="#Date-and-Time" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
1082
 
<td valign="middle" align="left">[<a href="#Calendar-Time" title="Up section"> Up </a>]</td>
1083
 
<td valign="middle" align="left">[<a href="libc_22.html#Resource-Usage-And-Limitation" title="Next chapter"> &gt;&gt; </a>]</td>
1084
 
<td valign="middle" align="left"> &nbsp; </td>
1085
 
<td valign="middle" align="left"> &nbsp; </td>
1086
 
<td valign="middle" align="left"> &nbsp; </td>
1087
 
<td valign="middle" align="left"> &nbsp; </td>
1088
 
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
1089
 
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
1090
 
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
1091
 
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
1092
 
</tr></table>
1093
 
<a name="High-Accuracy-Clock-1"></a>
1094
 
<h3 class="subsection">21.4.4 High Accuracy Clock</h3>
1095
 
 
1096
 
<a name="index-time_002c-high-precision"></a>
1097
 
<a name="index-clock_002c-high-accuracy"></a>
1098
 
<a name="index-sys_002ftimex_002eh"></a>
1099
 
<p>The <code>ntp_gettime</code> and <code>ntp_adjtime</code> functions provide an
1100
 
interface to monitor and manipulate the system clock to maintain high
1101
 
accuracy time.  For example, you can fine tune the speed of the clock
1102
 
or synchronize it with another time source.
1103
 
</p>
1104
 
<p>A typical use of these functions is by a server implementing the Network
1105
 
Time Protocol to synchronize the clocks of multiple systems and high
1106
 
precision clocks.
1107
 
</p>
1108
 
<p>These functions are declared in &lsquo;<tt>sys/timex.h</tt>&rsquo;.
1109
 
</p>
1110
 
<a name="index-struct-ntptimeval"></a>
1111
 
<dl>
1112
 
<dt><a name="index-struct-ntptimeval-1"></a><u>Data Type:</u> <b>struct ntptimeval</b></dt>
1113
 
<dd><p>This structure is used for information about the system clock.  It
1114
 
contains the following members:
1115
 
</p><dl compact="compact">
1116
 
<dt> <code>struct timeval time</code></dt>
1117
 
<dd><p>This is the current calendar time, expressed as the elapsed time since
1118
 
the epoch.  The <code>struct timeval</code> data type is described in
1119
 
<a href="#Elapsed-Time">Elapsed Time</a>.
1120
 
</p>
1121
 
</dd>
1122
 
<dt> <code>long int maxerror</code></dt>
1123
 
<dd><p>This is the maximum error, measured in microseconds.  Unless updated
1124
 
via <code>ntp_adjtime</code> periodically, this value will reach some
1125
 
platform-specific maximum value.
1126
 
</p>
1127
 
</dd>
1128
 
<dt> <code>long int esterror</code></dt>
1129
 
<dd><p>This is the estimated error, measured in microseconds.  This value can
1130
 
be set by <code>ntp_adjtime</code> to indicate the estimated offset of the
1131
 
system clock from the true calendar time.
1132
 
</p></dd>
1133
 
</dl>
1134
 
</dd></dl>
1135
 
 
1136
 
<dl>
1137
 
<dt><a name="index-ntp_005fgettime"></a><u>Function:</u> int <b>ntp_gettime</b><i> (struct ntptimeval *<var>tptr</var>)</i></dt>
1138
 
<dd><p>The <code>ntp_gettime</code> function sets the structure pointed to by
1139
 
<var>tptr</var> to current values.  The elements of the structure afterwards
1140
 
contain the values the timer implementation in the kernel assumes.  They
1141
 
might or might not be correct.  If they are not a <code>ntp_adjtime</code>
1142
 
call is necessary.
1143
 
</p>
1144
 
<p>The return value is <code>0</code> on success and other values on failure.  The
1145
 
following <code>errno</code> error conditions are defined for this function:
1146
 
</p>
1147
 
<dl compact="compact">
1148
 
<dt> <code>TIME_ERROR</code></dt>
1149
 
<dd><p>The precision clock model is not properly set up at the moment, thus the
1150
 
clock must be considered unsynchronized, and the values should be
1151
 
treated with care.
1152
 
</p></dd>
1153
 
</dl>
1154
 
</dd></dl>
1155
 
 
1156
 
<a name="index-struct-timex"></a>
1157
 
<dl>
1158
 
<dt><a name="index-struct-timex-1"></a><u>Data Type:</u> <b>struct timex</b></dt>
1159
 
<dd><p>This structure is used to control and monitor the system clock.  It
1160
 
contains the following members:
1161
 
</p><dl compact="compact">
1162
 
<dt> <code>unsigned int modes</code></dt>
1163
 
<dd><p>This variable controls whether and which values are set.  Several
1164
 
symbolic constants have to be combined with <em>binary or</em> to specify
1165
 
the effective mode.  These constants start with <code>MOD_</code>.
1166
 
</p>
1167
 
</dd>
1168
 
<dt> <code>long int offset</code></dt>
1169
 
<dd><p>This value indicates the current offset of the system clock from the true
1170
 
calendar time.  The value is given in microseconds.  If bit
1171
 
<code>MOD_OFFSET</code> is set in <code>modes</code>, the offset (and possibly other
1172
 
dependent values) can be set.  The offset&rsquo;s absolute value must not
1173
 
exceed <code>MAXPHASE</code>.
1174
 
</p>
1175
 
 
1176
 
</dd>
1177
 
<dt> <code>long int frequency</code></dt>
1178
 
<dd><p>This value indicates the difference in frequency between the true
1179
 
calendar time and the system clock.  The value is expressed as scaled
1180
 
PPM (parts per million, 0.0001%).  The scaling is <code>1 &lt;&lt;
1181
 
SHIFT_USEC</code>.  The value can be set with bit <code>MOD_FREQUENCY</code>, but
1182
 
the absolute value must not exceed <code>MAXFREQ</code>.
1183
 
</p>
1184
 
</dd>
1185
 
<dt> <code>long int maxerror</code></dt>
1186
 
<dd><p>This is the maximum error, measured in microseconds.  A new value can be
1187
 
set using bit <code>MOD_MAXERROR</code>.  Unless updated via
1188
 
<code>ntp_adjtime</code> periodically, this value will increase steadily
1189
 
and reach some platform-specific maximum value.
1190
 
</p>
1191
 
</dd>
1192
 
<dt> <code>long int esterror</code></dt>
1193
 
<dd><p>This is the estimated error, measured in microseconds.  This value can
1194
 
be set using bit <code>MOD_ESTERROR</code>.
1195
 
</p>
1196
 
</dd>
1197
 
<dt> <code>int status</code></dt>
1198
 
<dd><p>This variable reflects the various states of the clock machinery.  There
1199
 
are symbolic constants for the significant bits, starting with
1200
 
<code>STA_</code>.  Some of these flags can be updated using the
1201
 
<code>MOD_STATUS</code> bit.
1202
 
</p>
1203
 
</dd>
1204
 
<dt> <code>long int constant</code></dt>
1205
 
<dd><p>This value represents the bandwidth or stiffness of the PLL (phase
1206
 
locked loop) implemented in the kernel.  The value can be changed using
1207
 
bit <code>MOD_TIMECONST</code>.
1208
 
</p>
1209
 
</dd>
1210
 
<dt> <code>long int precision</code></dt>
1211
 
<dd><p>This value represents the accuracy or the maximum error when reading the
1212
 
system clock.  The value is expressed in microseconds.
1213
 
</p>
1214
 
</dd>
1215
 
<dt> <code>long int tolerance</code></dt>
1216
 
<dd><p>This value represents the maximum frequency error of the system clock in
1217
 
scaled PPM.  This value is used to increase the <code>maxerror</code> every
1218
 
second.
1219
 
</p>
1220
 
</dd>
1221
 
<dt> <code>struct timeval time</code></dt>
1222
 
<dd><p>The current calendar time.
1223
 
</p>
1224
 
</dd>
1225
 
<dt> <code>long int tick</code></dt>
1226
 
<dd><p>The elapsed time between clock ticks in microseconds.  A clock tick is a
1227
 
periodic timer interrupt on which the system clock is based.
1228
 
</p>
1229
 
</dd>
1230
 
<dt> <code>long int ppsfreq</code></dt>
1231
 
<dd><p>This is the first of a few optional variables that are present only if
1232
 
the system clock can use a PPS (pulse per second) signal to discipline
1233
 
the system clock.  The value is expressed in scaled PPM and it denotes
1234
 
the difference in frequency between the system clock and the PPS signal.
1235
 
</p>
1236
 
</dd>
1237
 
<dt> <code>long int jitter</code></dt>
1238
 
<dd><p>This value expresses a median filtered average of the PPS signal&rsquo;s
1239
 
dispersion in microseconds.
1240
 
</p>
1241
 
</dd>
1242
 
<dt> <code>int shift</code></dt>
1243
 
<dd><p>This value is a binary exponent for the duration of the PPS calibration
1244
 
interval, ranging from <code>PPS_SHIFT</code> to <code>PPS_SHIFTMAX</code>.
1245
 
</p>
1246
 
</dd>
1247
 
<dt> <code>long int stabil</code></dt>
1248
 
<dd><p>This value represents the median filtered dispersion of the PPS
1249
 
frequency in scaled PPM.
1250
 
</p>
1251
 
</dd>
1252
 
<dt> <code>long int jitcnt</code></dt>
1253
 
<dd><p>This counter represents the number of pulses where the jitter exceeded
1254
 
the allowed maximum <code>MAXTIME</code>.
1255
 
</p>
1256
 
</dd>
1257
 
<dt> <code>long int calcnt</code></dt>
1258
 
<dd><p>This counter reflects the number of successful calibration intervals.
1259
 
</p>
1260
 
</dd>
1261
 
<dt> <code>long int errcnt</code></dt>
1262
 
<dd><p>This counter represents the number of calibration errors (caused by
1263
 
large offsets or jitter).
1264
 
</p>
1265
 
</dd>
1266
 
<dt> <code>long int stbcnt</code></dt>
1267
 
<dd><p>This counter denotes the number of calibrations where the stability
1268
 
exceeded the threshold.
1269
 
</p></dd>
1270
 
</dl>
1271
 
</dd></dl>
1272
 
 
1273
 
<dl>
1274
 
<dt><a name="index-ntp_005fadjtime"></a><u>Function:</u> int <b>ntp_adjtime</b><i> (struct timex *<var>tptr</var>)</i></dt>
1275
 
<dd><p>The <code>ntp_adjtime</code> function sets the structure specified by
1276
 
<var>tptr</var> to current values.
1277
 
</p>
1278
 
<p>In addition, <code>ntp_adjtime</code> updates some settings to match what you
1279
 
pass to it in *<var>tptr</var>.  Use the <code>modes</code> element of *<var>tptr</var>
1280
 
to select what settings to update.  You can set <code>offset</code>,
1281
 
<code>freq</code>, <code>maxerror</code>, <code>esterror</code>, <code>status</code>,
1282
 
<code>constant</code>, and <code>tick</code>.
1283
 
</p>
1284
 
<p><code>modes</code> = zero means set nothing.
1285
 
</p>
1286
 
<p>Only the superuser can update settings.
1287
 
</p>
1288
 
 
1289
 
<p>The return value is <code>0</code> on success and other values on failure.  The
1290
 
following <code>errno</code> error conditions are defined for this function:
1291
 
</p>
1292
 
<dl compact="compact">
1293
 
<dt> <code>TIME_ERROR</code></dt>
1294
 
<dd><p>The high accuracy clock model is not properly set up at the moment, thus the
1295
 
clock must be considered unsynchronized, and the values should be
1296
 
treated with care.  Another reason could be that the specified new values
1297
 
are not allowed.
1298
 
</p>
1299
 
</dd>
1300
 
<dt> <code>EPERM</code></dt>
1301
 
<dd><p>The process specified a settings update, but is not superuser.
1302
 
</p>
1303
 
</dd>
1304
 
</dl>
1305
 
 
1306
 
<p>For more details see RFC1305 (Network Time Protocol, Version 3) and
1307
 
related documents.
1308
 
</p>
1309
 
<p><strong>Portability note:</strong> Early versions of the GNU C library did not
1310
 
have this function but did have the synonymous <code>adjtimex</code>.
1311
 
</p>
1312
 
</dd></dl>
1313
 
 
1314
 
 
1315
 
<hr size="6">
1316
 
<a name="Formatting-Calendar-Time"></a>
1317
 
<table cellpadding="1" cellspacing="1" border="0">
1318
 
<tr><td valign="middle" align="left">[<a href="#High-Accuracy-Clock" title="Previous section in reading order"> &lt; </a>]</td>
1319
 
<td valign="middle" align="left">[<a href="#Parsing-Date-and-Time" title="Next section in reading order"> &gt; </a>]</td>
1320
 
<td valign="middle" align="left"> &nbsp; </td>
1321
 
<td valign="middle" align="left">[<a href="#Date-and-Time" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
1322
 
<td valign="middle" align="left">[<a href="#Calendar-Time" title="Up section"> Up </a>]</td>
1323
 
<td valign="middle" align="left">[<a href="libc_22.html#Resource-Usage-And-Limitation" title="Next chapter"> &gt;&gt; </a>]</td>
1324
 
<td valign="middle" align="left"> &nbsp; </td>
1325
 
<td valign="middle" align="left"> &nbsp; </td>
1326
 
<td valign="middle" align="left"> &nbsp; </td>
1327
 
<td valign="middle" align="left"> &nbsp; </td>
1328
 
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
1329
 
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
1330
 
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
1331
 
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
1332
 
</tr></table>
1333
 
<a name="Formatting-Calendar-Time-1"></a>
1334
 
<h3 class="subsection">21.4.5 Formatting Calendar Time</h3>
1335
 
 
1336
 
<p>The functions described in this section format calendar time values as
1337
 
strings.  These functions are declared in the header file &lsquo;<tt>time.h</tt>&rsquo;.
1338
 
<a name="index-time_002eh-3"></a>
1339
 
</p>
1340
 
<dl>
1341
 
<dt><a name="index-asctime"></a><u>Function:</u> char * <b>asctime</b><i> (const struct tm *<var>brokentime</var>)</i></dt>
1342
 
<dd><p>The <code>asctime</code> function converts the broken-down time value that
1343
 
<var>brokentime</var> points to into a string in a standard format:
1344
 
</p>
1345
 
<table><tr><td>&nbsp;</td><td><pre class="smallexample">&quot;Tue May 21 13:46:22 1991\n&quot;
1346
 
</pre></td></tr></table>
1347
 
 
1348
 
<p>The abbreviations for the days of week are: &lsquo;<samp>Sun</samp>&rsquo;, &lsquo;<samp>Mon</samp>&rsquo;,
1349
 
&lsquo;<samp>Tue</samp>&rsquo;, &lsquo;<samp>Wed</samp>&rsquo;, &lsquo;<samp>Thu</samp>&rsquo;, &lsquo;<samp>Fri</samp>&rsquo;, and &lsquo;<samp>Sat</samp>&rsquo;.
1350
 
</p>
1351
 
<p>The abbreviations for the months are: &lsquo;<samp>Jan</samp>&rsquo;, &lsquo;<samp>Feb</samp>&rsquo;,
1352
 
&lsquo;<samp>Mar</samp>&rsquo;, &lsquo;<samp>Apr</samp>&rsquo;, &lsquo;<samp>May</samp>&rsquo;, &lsquo;<samp>Jun</samp>&rsquo;, &lsquo;<samp>Jul</samp>&rsquo;, &lsquo;<samp>Aug</samp>&rsquo;,
1353
 
&lsquo;<samp>Sep</samp>&rsquo;, &lsquo;<samp>Oct</samp>&rsquo;, &lsquo;<samp>Nov</samp>&rsquo;, and &lsquo;<samp>Dec</samp>&rsquo;.
1354
 
</p>
1355
 
<p>The return value points to a statically allocated string, which might be
1356
 
overwritten by subsequent calls to <code>asctime</code> or <code>ctime</code>.
1357
 
(But no other library function overwrites the contents of this
1358
 
string.)
1359
 
</p></dd></dl>
1360
 
 
1361
 
<dl>
1362
 
<dt><a name="index-asctime_005fr"></a><u>Function:</u> char * <b>asctime_r</b><i> (const struct tm *<var>brokentime</var>, char *<var>buffer</var>)</i></dt>
1363
 
<dd><p>This function is similar to <code>asctime</code> but instead of placing the
1364
 
result in a static buffer it writes the string in the buffer pointed to
1365
 
by the parameter <var>buffer</var>.  This buffer should have room
1366
 
for at least 26 bytes, including the terminating null.
1367
 
</p>
1368
 
<p>If no error occurred the function returns a pointer to the string the
1369
 
result was written into, i.e., it returns <var>buffer</var>.  Otherwise
1370
 
return <code>NULL</code>.
1371
 
</p></dd></dl>
1372
 
 
1373
 
 
1374
 
<dl>
1375
 
<dt><a name="index-ctime"></a><u>Function:</u> char * <b>ctime</b><i> (const time_t *<var>time</var>)</i></dt>
1376
 
<dd><p>The <code>ctime</code> function is similar to <code>asctime</code>, except that you
1377
 
specify the calendar time argument as a <code>time_t</code> simple time value
1378
 
rather than in broken-down local time format.  It is equivalent to
1379
 
</p>
1380
 
<table><tr><td>&nbsp;</td><td><pre class="smallexample">asctime (localtime (<var>time</var>))
1381
 
</pre></td></tr></table>
1382
 
 
1383
 
<p><code>ctime</code> sets the variable <code>tzname</code>, because <code>localtime</code>
1384
 
does so.  See section <a href="#Time-Zone-Functions">Functions and Variables for Time Zones</a>.
1385
 
</p></dd></dl>
1386
 
 
1387
 
<dl>
1388
 
<dt><a name="index-ctime_005fr"></a><u>Function:</u> char * <b>ctime_r</b><i> (const time_t *<var>time</var>, char *<var>buffer</var>)</i></dt>
1389
 
<dd><p>This function is similar to <code>ctime</code>, but places the result in the
1390
 
string pointed to by <var>buffer</var>.  It is equivalent to (written using
1391
 
gcc extensions, see <a href="../gcc/Statement-Exprs.html#Statement-Exprs">(gcc)Statement Exprs</a> section &lsquo;Statement Exprs&rsquo; in <cite>Porting and Using gcc</cite>):
1392
 
</p>
1393
 
<table><tr><td>&nbsp;</td><td><pre class="smallexample">({ struct tm tm; asctime_r (localtime_r (time, &amp;tm), buf); })
1394
 
</pre></td></tr></table>
1395
 
 
1396
 
<p>If no error occurred the function returns a pointer to the string the
1397
 
result was written into, i.e., it returns <var>buffer</var>.  Otherwise
1398
 
return <code>NULL</code>.
1399
 
</p></dd></dl>
1400
 
 
1401
 
 
1402
 
<dl>
1403
 
<dt><a name="index-strftime"></a><u>Function:</u> size_t <b>strftime</b><i> (char *<var>s</var>, size_t <var>size</var>, const char *<var>template</var>, const struct tm *<var>brokentime</var>)</i></dt>
1404
 
<dd><p>This function is similar to the <code>sprintf</code> function (see section <a href="libc_12.html#Formatted-Input">Formatted Input</a>), but the conversion specifications that can appear in the format
1405
 
template <var>template</var> are specialized for printing components of the date
1406
 
and time <var>brokentime</var> according to the locale currently specified for
1407
 
time conversion (see section <a href="libc_7.html#Locales">Locales and Internationalization</a>).
1408
 
</p>
1409
 
<p>Ordinary characters appearing in the <var>template</var> are copied to the
1410
 
output string <var>s</var>; this can include multibyte character sequences.
1411
 
Conversion specifiers are introduced by a &lsquo;<samp>%</samp>&rsquo; character, followed
1412
 
by an optional flag which can be one of the following.  These flags
1413
 
are all GNU extensions. The first three affect only the output of
1414
 
numbers:
1415
 
</p>
1416
 
<dl compact="compact">
1417
 
<dt> <code>_</code></dt>
1418
 
<dd><p>The number is padded with spaces.
1419
 
</p>
1420
 
</dd>
1421
 
<dt> <code>-</code></dt>
1422
 
<dd><p>The number is not padded at all.
1423
 
</p>
1424
 
</dd>
1425
 
<dt> <code>0</code></dt>
1426
 
<dd><p>The number is padded with zeros even if the format specifies padding
1427
 
with spaces.
1428
 
</p>
1429
 
</dd>
1430
 
<dt> <code>^</code></dt>
1431
 
<dd><p>The output uses uppercase characters, but only if this is possible
1432
 
(see section <a href="libc_4.html#Case-Conversion">Case Conversion</a>).
1433
 
</p></dd>
1434
 
</dl>
1435
 
 
1436
 
<p>The default action is to pad the number with zeros to keep it a constant
1437
 
width.  Numbers that do not have a range indicated below are never
1438
 
padded, since there is no natural width for them.
1439
 
</p>
1440
 
<p>Following the flag an optional specification of the width is possible.
1441
 
This is specified in decimal notation.  If the natural size of the
1442
 
output is of the field has less than the specified number of characters,
1443
 
the result is written right adjusted and space padded to the given
1444
 
size.
1445
 
</p>
1446
 
<p>An optional modifier can follow the optional flag and width
1447
 
specification.  The modifiers, which were first standardized by
1448
 
POSIX.2-1992 and by ISO C99, are:
1449
 
</p>
1450
 
<dl compact="compact">
1451
 
<dt> <code>E</code></dt>
1452
 
<dd><p>Use the locale&rsquo;s alternate representation for date and time.  This
1453
 
modifier applies to the <code>%c</code>, <code>%C</code>, <code>%x</code>, <code>%X</code>,
1454
 
<code>%y</code> and <code>%Y</code> format specifiers.  In a Japanese locale, for
1455
 
example, <code>%Ex</code> might yield a date format based on the Japanese
1456
 
Emperors&rsquo; reigns.
1457
 
</p>
1458
 
</dd>
1459
 
<dt> <code>O</code></dt>
1460
 
<dd><p>Use the locale&rsquo;s alternate numeric symbols for numbers.  This modifier
1461
 
applies only to numeric format specifiers.
1462
 
</p></dd>
1463
 
</dl>
1464
 
 
1465
 
<p>If the format supports the modifier but no alternate representation
1466
 
is available, it is ignored.
1467
 
</p>
1468
 
<p>The conversion specifier ends with a format specifier taken from the
1469
 
following list.  The whole &lsquo;<samp>%</samp>&rsquo; sequence is replaced in the output
1470
 
string as follows:
1471
 
</p>
1472
 
<dl compact="compact">
1473
 
<dt> <code>%a</code></dt>
1474
 
<dd><p>The abbreviated weekday name according to the current locale.
1475
 
</p>
1476
 
</dd>
1477
 
<dt> <code>%A</code></dt>
1478
 
<dd><p>The full weekday name according to the current locale.
1479
 
</p>
1480
 
</dd>
1481
 
<dt> <code>%b</code></dt>
1482
 
<dd><p>The abbreviated month name according to the current locale.
1483
 
</p>
1484
 
</dd>
1485
 
<dt> <code>%B</code></dt>
1486
 
<dd><p>The full month name according to the current locale.
1487
 
</p>
1488
 
<p>Using <code>%B</code> together with <code>%d</code> produces grammatically
1489
 
incorrect results for some locales.
1490
 
</p>
1491
 
</dd>
1492
 
<dt> <code>%c</code></dt>
1493
 
<dd><p>The preferred calendar time representation for the current locale.
1494
 
</p>
1495
 
</dd>
1496
 
<dt> <code>%C</code></dt>
1497
 
<dd><p>The century of the year.  This is equivalent to the greatest integer not
1498
 
greater than the year divided by 100.
1499
 
</p>
1500
 
<p>This format was first standardized by POSIX.2-1992 and by ISO C99.
1501
 
</p>
1502
 
</dd>
1503
 
<dt> <code>%d</code></dt>
1504
 
<dd><p>The day of the month as a decimal number (range <code>01</code> through <code>31</code>).
1505
 
</p>
1506
 
</dd>
1507
 
<dt> <code>%D</code></dt>
1508
 
<dd><p>The date using the format <code>%m/%d/%y</code>.
1509
 
</p>
1510
 
<p>This format was first standardized by POSIX.2-1992 and by ISO C99.
1511
 
</p>
1512
 
</dd>
1513
 
<dt> <code>%e</code></dt>
1514
 
<dd><p>The day of the month like with <code>%d</code>, but padded with blank (range
1515
 
<code> 1</code> through <code>31</code>).
1516
 
</p>
1517
 
<p>This format was first standardized by POSIX.2-1992 and by ISO C99.
1518
 
</p>
1519
 
</dd>
1520
 
<dt> <code>%F</code></dt>
1521
 
<dd><p>The date using the format <code>%Y-%m-%d</code>.  This is the form specified
1522
 
in the ISO 8601 standard and is the preferred form for all uses.
1523
 
</p>
1524
 
<p>This format was first standardized by ISO C99 and by POSIX.1-2001.
1525
 
</p>
1526
 
</dd>
1527
 
<dt> <code>%g</code></dt>
1528
 
<dd><p>The year corresponding to the ISO week number, but without the century
1529
 
(range <code>00</code> through <code>99</code>).  This has the same format and value
1530
 
as <code>%y</code>, except that if the ISO week number (see <code>%V</code>) belongs
1531
 
to the previous or next year, that year is used instead.
1532
 
</p>
1533
 
<p>This format was first standardized by ISO C99 and by POSIX.1-2001.
1534
 
</p>
1535
 
</dd>
1536
 
<dt> <code>%G</code></dt>
1537
 
<dd><p>The year corresponding to the ISO week number.  This has the same format
1538
 
and value as <code>%Y</code>, except that if the ISO week number (see
1539
 
<code>%V</code>) belongs to the previous or next year, that year is used
1540
 
instead.
1541
 
</p>
1542
 
<p>This format was first standardized by ISO C99 and by POSIX.1-2001
1543
 
but was previously available as a GNU extension.
1544
 
</p>
1545
 
</dd>
1546
 
<dt> <code>%h</code></dt>
1547
 
<dd><p>The abbreviated month name according to the current locale.  The action
1548
 
is the same as for <code>%b</code>.
1549
 
</p>
1550
 
<p>This format was first standardized by POSIX.2-1992 and by ISO C99.
1551
 
</p>
1552
 
</dd>
1553
 
<dt> <code>%H</code></dt>
1554
 
<dd><p>The hour as a decimal number, using a 24-hour clock (range <code>00</code> through
1555
 
<code>23</code>).
1556
 
</p>
1557
 
</dd>
1558
 
<dt> <code>%I</code></dt>
1559
 
<dd><p>The hour as a decimal number, using a 12-hour clock (range <code>01</code> through
1560
 
<code>12</code>).
1561
 
</p>
1562
 
</dd>
1563
 
<dt> <code>%j</code></dt>
1564
 
<dd><p>The day of the year as a decimal number (range <code>001</code> through <code>366</code>).
1565
 
</p>
1566
 
</dd>
1567
 
<dt> <code>%k</code></dt>
1568
 
<dd><p>The hour as a decimal number, using a 24-hour clock like <code>%H</code>, but
1569
 
padded with blank (range <code> 0</code> through <code>23</code>).
1570
 
</p>
1571
 
<p>This format is a GNU extension.
1572
 
</p>
1573
 
</dd>
1574
 
<dt> <code>%l</code></dt>
1575
 
<dd><p>The hour as a decimal number, using a 12-hour clock like <code>%I</code>, but
1576
 
padded with blank (range <code> 1</code> through <code>12</code>).
1577
 
</p>
1578
 
<p>This format is a GNU extension.
1579
 
</p>
1580
 
</dd>
1581
 
<dt> <code>%m</code></dt>
1582
 
<dd><p>The month as a decimal number (range <code>01</code> through <code>12</code>).
1583
 
</p>
1584
 
</dd>
1585
 
<dt> <code>%M</code></dt>
1586
 
<dd><p>The minute as a decimal number (range <code>00</code> through <code>59</code>).
1587
 
</p>
1588
 
</dd>
1589
 
<dt> <code>%n</code></dt>
1590
 
<dd><p>A single &lsquo;<samp>\n</samp>&rsquo; (newline) character.
1591
 
</p>
1592
 
<p>This format was first standardized by POSIX.2-1992 and by ISO C99.
1593
 
</p>
1594
 
</dd>
1595
 
<dt> <code>%p</code></dt>
1596
 
<dd><p>Either &lsquo;<samp>AM</samp>&rsquo; or &lsquo;<samp>PM</samp>&rsquo;, according to the given time value; or the
1597
 
corresponding strings for the current locale.  Noon is treated as
1598
 
&lsquo;<samp>PM</samp>&rsquo; and midnight as &lsquo;<samp>AM</samp>&rsquo;.  In most locales
1599
 
&lsquo;<samp>AM</samp>&rsquo;/&lsquo;<samp>PM</samp>&rsquo; format is not supported, in such cases <code>&quot;%p&quot;</code>
1600
 
yields an empty string.
1601
 
</p>
1602
 
</dd>
1603
 
<dt> <code>%P</code></dt>
1604
 
<dd><p>Either &lsquo;<samp>am</samp>&rsquo; or &lsquo;<samp>pm</samp>&rsquo;, according to the given time value; or the
1605
 
corresponding strings for the current locale, printed in lowercase
1606
 
characters.  Noon is treated as &lsquo;<samp>pm</samp>&rsquo; and midnight as &lsquo;<samp>am</samp>&rsquo;.  In
1607
 
most locales &lsquo;<samp>AM</samp>&rsquo;/&lsquo;<samp>PM</samp>&rsquo; format is not supported, in such cases
1608
 
<code>&quot;%P&quot;</code> yields an empty string.
1609
 
</p>
1610
 
<p>This format is a GNU extension.
1611
 
</p>
1612
 
</dd>
1613
 
<dt> <code>%r</code></dt>
1614
 
<dd><p>The complete calendar time using the AM/PM format of the current locale.
1615
 
</p>
1616
 
<p>This format was first standardized by POSIX.2-1992 and by ISO C99.
1617
 
In the POSIX locale, this format is equivalent to <code>%I:%M:%S %p</code>.
1618
 
</p>
1619
 
</dd>
1620
 
<dt> <code>%R</code></dt>
1621
 
<dd><p>The hour and minute in decimal numbers using the format <code>%H:%M</code>.
1622
 
</p>
1623
 
<p>This format was first standardized by ISO C99 and by POSIX.1-2001
1624
 
but was previously available as a GNU extension.
1625
 
</p>
1626
 
</dd>
1627
 
<dt> <code>%s</code></dt>
1628
 
<dd><p>The number of seconds since the epoch, i.e., since 1970-01-01 00:00:00 UTC.
1629
 
Leap seconds are not counted unless leap second support is available.
1630
 
</p>
1631
 
<p>This format is a GNU extension.
1632
 
</p>
1633
 
</dd>
1634
 
<dt> <code>%S</code></dt>
1635
 
<dd><p>The seconds as a decimal number (range <code>00</code> through <code>60</code>).
1636
 
</p>
1637
 
</dd>
1638
 
<dt> <code>%t</code></dt>
1639
 
<dd><p>A single &lsquo;<samp>\t</samp>&rsquo; (tabulator) character.
1640
 
</p>
1641
 
<p>This format was first standardized by POSIX.2-1992 and by ISO C99.
1642
 
</p>
1643
 
</dd>
1644
 
<dt> <code>%T</code></dt>
1645
 
<dd><p>The time of day using decimal numbers using the format <code>%H:%M:%S</code>.
1646
 
</p>
1647
 
<p>This format was first standardized by POSIX.2-1992 and by ISO C99.
1648
 
</p>
1649
 
</dd>
1650
 
<dt> <code>%u</code></dt>
1651
 
<dd><p>The day of the week as a decimal number (range <code>1</code> through
1652
 
<code>7</code>), Monday being <code>1</code>.
1653
 
</p>
1654
 
<p>This format was first standardized by POSIX.2-1992 and by ISO C99.
1655
 
</p>
1656
 
</dd>
1657
 
<dt> <code>%U</code></dt>
1658
 
<dd><p>The week number of the current year as a decimal number (range <code>00</code>
1659
 
through <code>53</code>), starting with the first Sunday as the first day of
1660
 
the first week.  Days preceding the first Sunday in the year are
1661
 
considered to be in week <code>00</code>.
1662
 
</p>
1663
 
</dd>
1664
 
<dt> <code>%V</code></dt>
1665
 
<dd><p>The ISO 8601:1988 week number as a decimal number (range <code>01</code>
1666
 
through <code>53</code>).  ISO weeks start with Monday and end with Sunday.
1667
 
Week <code>01</code> of a year is the first week which has the majority of its
1668
 
days in that year; this is equivalent to the week containing the year&rsquo;s
1669
 
first Thursday, and it is also equivalent to the week containing January
1670
 
4.  Week <code>01</code> of a year can contain days from the previous year.
1671
 
The week before week <code>01</code> of a year is the last week (<code>52</code> or
1672
 
<code>53</code>) of the previous year even if it contains days from the new
1673
 
year.
1674
 
</p>
1675
 
<p>This format was first standardized by POSIX.2-1992 and by ISO C99.
1676
 
</p>
1677
 
</dd>
1678
 
<dt> <code>%w</code></dt>
1679
 
<dd><p>The day of the week as a decimal number (range <code>0</code> through
1680
 
<code>6</code>), Sunday being <code>0</code>.
1681
 
</p>
1682
 
</dd>
1683
 
<dt> <code>%W</code></dt>
1684
 
<dd><p>The week number of the current year as a decimal number (range <code>00</code>
1685
 
through <code>53</code>), starting with the first Monday as the first day of
1686
 
the first week.  All days preceding the first Monday in the year are
1687
 
considered to be in week <code>00</code>.
1688
 
</p>
1689
 
</dd>
1690
 
<dt> <code>%x</code></dt>
1691
 
<dd><p>The preferred date representation for the current locale.
1692
 
</p>
1693
 
</dd>
1694
 
<dt> <code>%X</code></dt>
1695
 
<dd><p>The preferred time of day representation for the current locale.
1696
 
</p>
1697
 
</dd>
1698
 
<dt> <code>%y</code></dt>
1699
 
<dd><p>The year without a century as a decimal number (range <code>00</code> through
1700
 
<code>99</code>).  This is equivalent to the year modulo 100.
1701
 
</p>
1702
 
</dd>
1703
 
<dt> <code>%Y</code></dt>
1704
 
<dd><p>The year as a decimal number, using the Gregorian calendar.  Years
1705
 
before the year <code>1</code> are numbered <code>0</code>, <code>-1</code>, and so on.
1706
 
</p>
1707
 
</dd>
1708
 
<dt> <code>%z</code></dt>
1709
 
<dd><p>RFC 822/ISO 8601:1988 style numeric time zone (e.g.,
1710
 
<code>-0600</code> or <code>+0100</code>), or nothing if no time zone is
1711
 
determinable.
1712
 
</p>
1713
 
<p>This format was first standardized by ISO C99 and by POSIX.1-2001
1714
 
but was previously available as a GNU extension.
1715
 
</p>
1716
 
<p>In the POSIX locale, a full RFC 822 timestamp is generated by the format
1717
 
&lsquo;<samp>&quot;%a, %d %b %Y %H:%M:%S %z&quot;</samp>&rsquo; (or the equivalent
1718
 
&lsquo;<samp>&quot;%a, %d %b %Y %T %z&quot;</samp>&rsquo;).
1719
 
</p>
1720
 
</dd>
1721
 
<dt> <code>%Z</code></dt>
1722
 
<dd><p>The time zone abbreviation (empty if the time zone can&rsquo;t be determined).
1723
 
</p>
1724
 
</dd>
1725
 
<dt> <code>%%</code></dt>
1726
 
<dd><p>A literal &lsquo;<samp>%</samp>&rsquo; character.
1727
 
</p></dd>
1728
 
</dl>
1729
 
 
1730
 
<p>The <var>size</var> parameter can be used to specify the maximum number of
1731
 
characters to be stored in the array <var>s</var>, including the terminating
1732
 
null character.  If the formatted time requires more than <var>size</var>
1733
 
characters, <code>strftime</code> returns zero and the contents of the array
1734
 
<var>s</var> are undefined.  Otherwise the return value indicates the
1735
 
number of characters placed in the array <var>s</var>, not including the
1736
 
terminating null character.
1737
 
</p>
1738
 
<p><em>Warning:</em> This convention for the return value which is prescribed
1739
 
in ISO C can lead to problems in some situations.  For certain
1740
 
format strings and certain locales the output really can be the empty
1741
 
string and this cannot be discovered by testing the return value only.
1742
 
E.g., in most locales the AM/PM time format is not supported (most of
1743
 
the world uses the 24 hour time representation).  In such locales
1744
 
<code>&quot;%p&quot;</code> will return the empty string, i.e., the return value is
1745
 
zero.  To detect situations like this something similar to the following
1746
 
code should be used:
1747
 
</p>
1748
 
<table><tr><td>&nbsp;</td><td><pre class="smallexample">buf[0] = '\1';
1749
 
len = strftime (buf, bufsize, format, tp);
1750
 
if (len == 0 &amp;&amp; buf[0] != '\0')
1751
 
  {
1752
 
    /* Something went wrong in the strftime call.  */
1753
 
    &hellip;
1754
 
  }
1755
 
</pre></td></tr></table>
1756
 
 
1757
 
<p>If <var>s</var> is a null pointer, <code>strftime</code> does not actually write
1758
 
anything, but instead returns the number of characters it would have written.
1759
 
</p>
1760
 
<p>According to POSIX.1 every call to <code>strftime</code> implies a call to
1761
 
<code>tzset</code>.  So the contents of the environment variable <code>TZ</code>
1762
 
is examined before any output is produced.
1763
 
</p>
1764
 
<p>For an example of <code>strftime</code>, see <a href="#Time-Functions-Example">Time Functions Example</a>.
1765
 
</p></dd></dl>
1766
 
 
1767
 
<dl>
1768
 
<dt><a name="index-wcsftime"></a><u>Function:</u> size_t <b>wcsftime</b><i> (wchar_t *<var>s</var>, size_t <var>size</var>, const wchar_t *<var>template</var>, const struct tm *<var>brokentime</var>)</i></dt>
1769
 
<dd><p>The <code>wcsftime</code> function is equivalent to the <code>strftime</code>
1770
 
function with the difference that it operates on wide character
1771
 
strings.  The buffer where the result is stored, pointed to by <var>s</var>,
1772
 
must be an array of wide characters.  The parameter <var>size</var> which
1773
 
specifies the size of the output buffer gives the number of wide
1774
 
character, not the number of bytes.
1775
 
</p>
1776
 
<p>Also the format string <var>template</var> is a wide character string.  Since
1777
 
all characters needed to specify the format string are in the basic
1778
 
character set it is portably possible to write format strings in the C
1779
 
source code using the <code>L&quot;&hellip;&quot;</code> notation.  The parameter
1780
 
<var>brokentime</var> has the same meaning as in the <code>strftime</code> call.
1781
 
</p>
1782
 
<p>The <code>wcsftime</code> function supports the same flags, modifiers, and
1783
 
format specifiers as the <code>strftime</code> function.
1784
 
</p>
1785
 
<p>The return value of <code>wcsftime</code> is the number of wide characters
1786
 
stored in <code>s</code>.  When more characters would have to be written than
1787
 
can be placed in the buffer <var>s</var> the return value is zero, with the
1788
 
same problems indicated in the <code>strftime</code> documentation.
1789
 
</p></dd></dl>
1790
 
 
1791
 
<hr size="6">
1792
 
<a name="Parsing-Date-and-Time"></a>
1793
 
<table cellpadding="1" cellspacing="1" border="0">
1794
 
<tr><td valign="middle" align="left">[<a href="#Formatting-Calendar-Time" title="Previous section in reading order"> &lt; </a>]</td>
1795
 
<td valign="middle" align="left">[<a href="#Low_002dLevel-Time-String-Parsing" title="Next section in reading order"> &gt; </a>]</td>
1796
 
<td valign="middle" align="left"> &nbsp; </td>
1797
 
<td valign="middle" align="left">[<a href="#Date-and-Time" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
1798
 
<td valign="middle" align="left">[<a href="#Calendar-Time" title="Up section"> Up </a>]</td>
1799
 
<td valign="middle" align="left">[<a href="libc_22.html#Resource-Usage-And-Limitation" title="Next chapter"> &gt;&gt; </a>]</td>
1800
 
<td valign="middle" align="left"> &nbsp; </td>
1801
 
<td valign="middle" align="left"> &nbsp; </td>
1802
 
<td valign="middle" align="left"> &nbsp; </td>
1803
 
<td valign="middle" align="left"> &nbsp; </td>
1804
 
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
1805
 
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
1806
 
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
1807
 
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
1808
 
</tr></table>
1809
 
<a name="Convert-textual-time-and-date-information-back"></a>
1810
 
<h3 class="subsection">21.4.6 Convert textual time and date information back</h3>
1811
 
 
1812
 
<p>The ISO C standard does not specify any functions which can convert
1813
 
the output of the <code>strftime</code> function back into a binary format.
1814
 
This led to a variety of more-or-less successful implementations with
1815
 
different interfaces over the years.  Then the Unix standard was
1816
 
extended by the addition of two functions: <code>strptime</code> and
1817
 
<code>getdate</code>.  Both have strange interfaces but at least they are
1818
 
widely available.
1819
 
</p>
1820
 
<table class="menu" border="0" cellspacing="0">
1821
 
<tr><td align="left" valign="top"><a href="#Low_002dLevel-Time-String-Parsing">21.4.6.1 Interpret string according to given format</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top"></td></tr>
1822
 
<tr><td align="left" valign="top"><a href="#General-Time-String-Parsing">21.4.6.2 A More User-friendly Way to Parse Times and Dates</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">    User-friendly function to parse data and
1823
 
                                    time strings.
1824
 
</td></tr>
1825
 
</table>
1826
 
 
1827
 
<hr size="6">
1828
 
<a name="Low_002dLevel-Time-String-Parsing"></a>
1829
 
<table cellpadding="1" cellspacing="1" border="0">
1830
 
<tr><td valign="middle" align="left">[<a href="#Parsing-Date-and-Time" title="Previous section in reading order"> &lt; </a>]</td>
1831
 
<td valign="middle" align="left">[<a href="#General-Time-String-Parsing" title="Next section in reading order"> &gt; </a>]</td>
1832
 
<td valign="middle" align="left"> &nbsp; </td>
1833
 
<td valign="middle" align="left">[<a href="#Date-and-Time" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
1834
 
<td valign="middle" align="left">[<a href="#Parsing-Date-and-Time" title="Up section"> Up </a>]</td>
1835
 
<td valign="middle" align="left">[<a href="libc_22.html#Resource-Usage-And-Limitation" title="Next chapter"> &gt;&gt; </a>]</td>
1836
 
<td valign="middle" align="left"> &nbsp; </td>
1837
 
<td valign="middle" align="left"> &nbsp; </td>
1838
 
<td valign="middle" align="left"> &nbsp; </td>
1839
 
<td valign="middle" align="left"> &nbsp; </td>
1840
 
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
1841
 
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
1842
 
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
1843
 
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
1844
 
</tr></table>
1845
 
<a name="Interpret-string-according-to-given-format"></a>
1846
 
<h4 class="subsubsection">21.4.6.1 Interpret string according to given format</h4>
1847
 
 
1848
 
<p>The first function is rather low-level.  It is nevertheless frequently
1849
 
used in software since it is better known.  Its interface and
1850
 
implementation are heavily influenced by the <code>getdate</code> function,
1851
 
which is defined and implemented in terms of calls to <code>strptime</code>.
1852
 
</p>
1853
 
<dl>
1854
 
<dt><a name="index-strptime"></a><u>Function:</u> char * <b>strptime</b><i> (const char *<var>s</var>, const char *<var>fmt</var>, struct tm *<var>tp</var>)</i></dt>
1855
 
<dd><p>The <code>strptime</code> function parses the input string <var>s</var> according
1856
 
to the format string <var>fmt</var> and stores its results in the
1857
 
structure <var>tp</var>.
1858
 
</p>
1859
 
<p>The input string could be generated by a <code>strftime</code> call or
1860
 
obtained any other way.  It does not need to be in a human-recognizable
1861
 
format; e.g. a date passed as <code>&quot;02:1999:9&quot;</code> is acceptable, even
1862
 
though it is ambiguous without context.  As long as the format string
1863
 
<var>fmt</var> matches the input string the function will succeed.
1864
 
</p>
1865
 
<p>The user has to make sure, though, that the input can be parsed in a
1866
 
unambiguous way.  The string <code>&quot;1999112&quot;</code> can be parsed using the
1867
 
format <code>&quot;%Y%m%d&quot;</code> as 1999-1-12, 1999-11-2, or even 19991-1-2.  It
1868
 
is necessary to add appropriate separators to reliably get results.
1869
 
</p>
1870
 
<p>The format string consists of the same components as the format string
1871
 
of the <code>strftime</code> function.  The only difference is that the flags
1872
 
<code>_</code>, <code>-</code>, <code>0</code>, and <code>^</code> are not allowed.
1873
 
Several of the distinct formats of <code>strftime</code> do the same work in
1874
 
<code>strptime</code> since differences like case of the input do not matter.
1875
 
For reasons of symmetry all formats are supported, though.
1876
 
</p>
1877
 
<p>The modifiers <code>E</code> and <code>O</code> are also allowed everywhere the
1878
 
<code>strftime</code> function allows them.
1879
 
</p>
1880
 
<p>The formats are:
1881
 
</p>
1882
 
<dl compact="compact">
1883
 
<dt> <code>%a</code></dt>
1884
 
<dt> <code>%A</code></dt>
1885
 
<dd><p>The weekday name according to the current locale, in abbreviated form or
1886
 
the full name.
1887
 
</p>
1888
 
</dd>
1889
 
<dt> <code>%b</code></dt>
1890
 
<dt> <code>%B</code></dt>
1891
 
<dt> <code>%h</code></dt>
1892
 
<dd><p>The month name according to the current locale, in abbreviated form or
1893
 
the full name.
1894
 
</p>
1895
 
</dd>
1896
 
<dt> <code>%c</code></dt>
1897
 
<dd><p>The date and time representation for the current locale.
1898
 
</p>
1899
 
</dd>
1900
 
<dt> <code>%Ec</code></dt>
1901
 
<dd><p>Like <code>%c</code> but the locale&rsquo;s alternative date and time format is used.
1902
 
</p>
1903
 
</dd>
1904
 
<dt> <code>%C</code></dt>
1905
 
<dd><p>The century of the year.
1906
 
</p>
1907
 
<p>It makes sense to use this format only if the format string also
1908
 
contains the <code>%y</code> format.
1909
 
</p>
1910
 
</dd>
1911
 
<dt> <code>%EC</code></dt>
1912
 
<dd><p>The locale&rsquo;s representation of the period.
1913
 
</p>
1914
 
<p>Unlike <code>%C</code> it sometimes makes sense to use this format since some
1915
 
cultures represent years relative to the beginning of eras instead of
1916
 
using the Gregorian years.
1917
 
</p>
1918
 
</dd>
1919
 
<dt> <code>%d</code></dt>
1920
 
<dt> <code>%e</code></dt>
1921
 
<dd><p>The day of the month as a decimal number (range <code>1</code> through <code>31</code>).
1922
 
Leading zeroes are permitted but not required.
1923
 
</p>
1924
 
</dd>
1925
 
<dt> <code>%Od</code></dt>
1926
 
<dt> <code>%Oe</code></dt>
1927
 
<dd><p>Same as <code>%d</code> but using the locale&rsquo;s alternative numeric symbols.
1928
 
</p>
1929
 
<p>Leading zeroes are permitted but not required.
1930
 
</p>
1931
 
</dd>
1932
 
<dt> <code>%D</code></dt>
1933
 
<dd><p>Equivalent to <code>%m/%d/%y</code>.
1934
 
</p>
1935
 
</dd>
1936
 
<dt> <code>%F</code></dt>
1937
 
<dd><p>Equivalent to <code>%Y-%m-%d</code>, which is the ISO 8601 date
1938
 
format.
1939
 
</p>
1940
 
<p>This is a GNU extension following an ISO C99 extension to
1941
 
<code>strftime</code>.
1942
 
</p>
1943
 
</dd>
1944
 
<dt> <code>%g</code></dt>
1945
 
<dd><p>The year corresponding to the ISO week number, but without the century
1946
 
(range <code>00</code> through <code>99</code>).
1947
 
</p>
1948
 
<p><em>Note:</em> Currently, this is not fully implemented.  The format is
1949
 
recognized, input is consumed but no field in <var>tm</var> is set.
1950
 
</p>
1951
 
<p>This format is a GNU extension following a GNU extension of <code>strftime</code>.
1952
 
</p>
1953
 
</dd>
1954
 
<dt> <code>%G</code></dt>
1955
 
<dd><p>The year corresponding to the ISO week number.
1956
 
</p>
1957
 
<p><em>Note:</em> Currently, this is not fully implemented.  The format is
1958
 
recognized, input is consumed but no field in <var>tm</var> is set.
1959
 
</p>
1960
 
<p>This format is a GNU extension following a GNU extension of <code>strftime</code>.
1961
 
</p>
1962
 
</dd>
1963
 
<dt> <code>%H</code></dt>
1964
 
<dt> <code>%k</code></dt>
1965
 
<dd><p>The hour as a decimal number, using a 24-hour clock (range <code>00</code> through
1966
 
<code>23</code>).
1967
 
</p>
1968
 
<p><code>%k</code> is a GNU extension following a GNU extension of <code>strftime</code>.
1969
 
</p>
1970
 
</dd>
1971
 
<dt> <code>%OH</code></dt>
1972
 
<dd><p>Same as <code>%H</code> but using the locale&rsquo;s alternative numeric symbols.
1973
 
</p>
1974
 
</dd>
1975
 
<dt> <code>%I</code></dt>
1976
 
<dt> <code>%l</code></dt>
1977
 
<dd><p>The hour as a decimal number, using a 12-hour clock (range <code>01</code> through
1978
 
<code>12</code>).
1979
 
</p>
1980
 
<p><code>%l</code> is a GNU extension following a GNU extension of <code>strftime</code>.
1981
 
</p>
1982
 
</dd>
1983
 
<dt> <code>%OI</code></dt>
1984
 
<dd><p>Same as <code>%I</code> but using the locale&rsquo;s alternative numeric symbols.
1985
 
</p>
1986
 
</dd>
1987
 
<dt> <code>%j</code></dt>
1988
 
<dd><p>The day of the year as a decimal number (range <code>1</code> through <code>366</code>).
1989
 
</p>
1990
 
<p>Leading zeroes are permitted but not required.
1991
 
</p>
1992
 
</dd>
1993
 
<dt> <code>%m</code></dt>
1994
 
<dd><p>The month as a decimal number (range <code>1</code> through <code>12</code>).
1995
 
</p>
1996
 
<p>Leading zeroes are permitted but not required.
1997
 
</p>
1998
 
</dd>
1999
 
<dt> <code>%Om</code></dt>
2000
 
<dd><p>Same as <code>%m</code> but using the locale&rsquo;s alternative numeric symbols.
2001
 
</p>
2002
 
</dd>
2003
 
<dt> <code>%M</code></dt>
2004
 
<dd><p>The minute as a decimal number (range <code>0</code> through <code>59</code>).
2005
 
</p>
2006
 
<p>Leading zeroes are permitted but not required.
2007
 
</p>
2008
 
</dd>
2009
 
<dt> <code>%OM</code></dt>
2010
 
<dd><p>Same as <code>%M</code> but using the locale&rsquo;s alternative numeric symbols.
2011
 
</p>
2012
 
</dd>
2013
 
<dt> <code>%n</code></dt>
2014
 
<dt> <code>%t</code></dt>
2015
 
<dd><p>Matches any white space.
2016
 
</p>
2017
 
</dd>
2018
 
<dt> <code>%p</code></dt>
2019
 
<dt> <code>%P</code></dt>
2020
 
<dd><p>The locale-dependent equivalent to &lsquo;<samp>AM</samp>&rsquo; or &lsquo;<samp>PM</samp>&rsquo;.
2021
 
</p>
2022
 
<p>This format is not useful unless <code>%I</code> or <code>%l</code> is also used.
2023
 
Another complication is that the locale might not define these values at
2024
 
all and therefore the conversion fails.
2025
 
</p>
2026
 
<p><code>%P</code> is a GNU extension following a GNU extension to <code>strftime</code>.
2027
 
</p>
2028
 
</dd>
2029
 
<dt> <code>%r</code></dt>
2030
 
<dd><p>The complete time using the AM/PM format of the current locale.
2031
 
</p>
2032
 
<p>A complication is that the locale might not define this format at all
2033
 
and therefore the conversion fails.
2034
 
</p>
2035
 
</dd>
2036
 
<dt> <code>%R</code></dt>
2037
 
<dd><p>The hour and minute in decimal numbers using the format <code>%H:%M</code>.
2038
 
</p>
2039
 
<p><code>%R</code> is a GNU extension following a GNU extension to <code>strftime</code>.
2040
 
</p>
2041
 
</dd>
2042
 
<dt> <code>%s</code></dt>
2043
 
<dd><p>The number of seconds since the epoch, i.e., since 1970-01-01 00:00:00 UTC.
2044
 
Leap seconds are not counted unless leap second support is available.
2045
 
</p>
2046
 
<p><code>%s</code> is a GNU extension following a GNU extension to <code>strftime</code>.
2047
 
</p>
2048
 
</dd>
2049
 
<dt> <code>%S</code></dt>
2050
 
<dd><p>The seconds as a decimal number (range <code>0</code> through <code>60</code>).
2051
 
</p>
2052
 
<p>Leading zeroes are permitted but not required.
2053
 
</p>
2054
 
<p><strong>NB:</strong> The Unix specification says the upper bound on this value
2055
 
is <code>61</code>, a result of a decision to allow double leap seconds.  You
2056
 
will not see the value <code>61</code> because no minute has more than one
2057
 
leap second, but the myth persists.
2058
 
</p>
2059
 
</dd>
2060
 
<dt> <code>%OS</code></dt>
2061
 
<dd><p>Same as <code>%S</code> but using the locale&rsquo;s alternative numeric symbols.
2062
 
</p>
2063
 
</dd>
2064
 
<dt> <code>%T</code></dt>
2065
 
<dd><p>Equivalent to the use of <code>%H:%M:%S</code> in this place.
2066
 
</p>
2067
 
</dd>
2068
 
<dt> <code>%u</code></dt>
2069
 
<dd><p>The day of the week as a decimal number (range <code>1</code> through
2070
 
<code>7</code>), Monday being <code>1</code>.
2071
 
</p>
2072
 
<p>Leading zeroes are permitted but not required.
2073
 
</p>
2074
 
<p><em>Note:</em> Currently, this is not fully implemented.  The format is
2075
 
recognized, input is consumed but no field in <var>tm</var> is set.
2076
 
</p>
2077
 
</dd>
2078
 
<dt> <code>%U</code></dt>
2079
 
<dd><p>The week number of the current year as a decimal number (range <code>0</code>
2080
 
through <code>53</code>).
2081
 
</p>
2082
 
<p>Leading zeroes are permitted but not required.
2083
 
</p>
2084
 
</dd>
2085
 
<dt> <code>%OU</code></dt>
2086
 
<dd><p>Same as <code>%U</code> but using the locale&rsquo;s alternative numeric symbols.
2087
 
</p>
2088
 
</dd>
2089
 
<dt> <code>%V</code></dt>
2090
 
<dd><p>The ISO 8601:1988 week number as a decimal number (range <code>1</code>
2091
 
through <code>53</code>).
2092
 
</p>
2093
 
<p>Leading zeroes are permitted but not required.
2094
 
</p>
2095
 
<p><em>Note:</em> Currently, this is not fully implemented.  The format is
2096
 
recognized, input is consumed but no field in <var>tm</var> is set.
2097
 
</p>
2098
 
</dd>
2099
 
<dt> <code>%w</code></dt>
2100
 
<dd><p>The day of the week as a decimal number (range <code>0</code> through
2101
 
<code>6</code>), Sunday being <code>0</code>.
2102
 
</p>
2103
 
<p>Leading zeroes are permitted but not required.
2104
 
</p>
2105
 
<p><em>Note:</em> Currently, this is not fully implemented.  The format is
2106
 
recognized, input is consumed but no field in <var>tm</var> is set.
2107
 
</p>
2108
 
</dd>
2109
 
<dt> <code>%Ow</code></dt>
2110
 
<dd><p>Same as <code>%w</code> but using the locale&rsquo;s alternative numeric symbols.
2111
 
</p>
2112
 
</dd>
2113
 
<dt> <code>%W</code></dt>
2114
 
<dd><p>The week number of the current year as a decimal number (range <code>0</code>
2115
 
through <code>53</code>).
2116
 
</p>
2117
 
<p>Leading zeroes are permitted but not required.
2118
 
</p>
2119
 
<p><em>Note:</em> Currently, this is not fully implemented.  The format is
2120
 
recognized, input is consumed but no field in <var>tm</var> is set.
2121
 
</p>
2122
 
</dd>
2123
 
<dt> <code>%OW</code></dt>
2124
 
<dd><p>Same as <code>%W</code> but using the locale&rsquo;s alternative numeric symbols.
2125
 
</p>
2126
 
</dd>
2127
 
<dt> <code>%x</code></dt>
2128
 
<dd><p>The date using the locale&rsquo;s date format.
2129
 
</p>
2130
 
</dd>
2131
 
<dt> <code>%Ex</code></dt>
2132
 
<dd><p>Like <code>%x</code> but the locale&rsquo;s alternative data representation is used.
2133
 
</p>
2134
 
</dd>
2135
 
<dt> <code>%X</code></dt>
2136
 
<dd><p>The time using the locale&rsquo;s time format.
2137
 
</p>
2138
 
</dd>
2139
 
<dt> <code>%EX</code></dt>
2140
 
<dd><p>Like <code>%X</code> but the locale&rsquo;s alternative time representation is used.
2141
 
</p>
2142
 
</dd>
2143
 
<dt> <code>%y</code></dt>
2144
 
<dd><p>The year without a century as a decimal number (range <code>0</code> through
2145
 
<code>99</code>).
2146
 
</p>
2147
 
<p>Leading zeroes are permitted but not required.
2148
 
</p>
2149
 
<p>Note that it is questionable to use this format without
2150
 
the <code>%C</code> format.  The <code>strptime</code> function does regard input
2151
 
values in the range <em>68</em> to <em>99</em> as the years <em>1969</em> to
2152
 
<em>1999</em> and the values <em>0</em> to <em>68</em> as the years
2153
 
<em>2000</em> to <em>2068</em>.  But maybe this heuristic fails for some
2154
 
input data.
2155
 
</p>
2156
 
<p>Therefore it is best to avoid <code>%y</code> completely and use <code>%Y</code>
2157
 
instead.
2158
 
</p>
2159
 
</dd>
2160
 
<dt> <code>%Ey</code></dt>
2161
 
<dd><p>The offset from <code>%EC</code> in the locale&rsquo;s alternative representation.
2162
 
</p>
2163
 
</dd>
2164
 
<dt> <code>%Oy</code></dt>
2165
 
<dd><p>The offset of the year (from <code>%C</code>) using the locale&rsquo;s alternative
2166
 
numeric symbols.
2167
 
</p>
2168
 
</dd>
2169
 
<dt> <code>%Y</code></dt>
2170
 
<dd><p>The year as a decimal number, using the Gregorian calendar.
2171
 
</p>
2172
 
</dd>
2173
 
<dt> <code>%EY</code></dt>
2174
 
<dd><p>The full alternative year representation.
2175
 
</p>
2176
 
</dd>
2177
 
<dt> <code>%z</code></dt>
2178
 
<dd><p>The offset from GMT in ISO 8601/RFC822 format.
2179
 
</p>
2180
 
</dd>
2181
 
<dt> <code>%Z</code></dt>
2182
 
<dd><p>The timezone name.
2183
 
</p>
2184
 
<p><em>Note:</em> Currently, this is not fully implemented.  The format is
2185
 
recognized, input is consumed but no field in <var>tm</var> is set.
2186
 
</p>
2187
 
</dd>
2188
 
<dt> <code>%%</code></dt>
2189
 
<dd><p>A literal &lsquo;<samp>%</samp>&rsquo; character.
2190
 
</p></dd>
2191
 
</dl>
2192
 
 
2193
 
<p>All other characters in the format string must have a matching character
2194
 
in the input string.  Exceptions are white spaces in the input string
2195
 
which can match zero or more whitespace characters in the format string.
2196
 
</p>
2197
 
<p><strong>Portability Note:</strong> The XPG standard advises applications to use
2198
 
at least one whitespace character (as specified by <code>isspace</code>) or
2199
 
other non-alphanumeric characters between any two conversion
2200
 
specifications.  The GNU C Library does not have this limitation but
2201
 
other libraries might have trouble parsing formats like
2202
 
<code>&quot;%d%m%Y%H%M%S&quot;</code>.
2203
 
</p>
2204
 
<p>The <code>strptime</code> function processes the input string from right to
2205
 
left.  Each of the three possible input elements (white space, literal,
2206
 
or format) are handled one after the other.  If the input cannot be
2207
 
matched to the format string the function stops.  The remainder of the
2208
 
format and input strings are not processed.
2209
 
</p>
2210
 
<p>The function returns a pointer to the first character it was unable to
2211
 
process.  If the input string contains more characters than required by
2212
 
the format string the return value points right after the last consumed
2213
 
input character.  If the whole input string is consumed the return value
2214
 
points to the <code>NULL</code> byte at the end of the string.  If an error
2215
 
occurs, i.e., <code>strptime</code> fails to match all of the format string,
2216
 
the function returns <code>NULL</code>.
2217
 
</p></dd></dl>
2218
 
 
2219
 
<p>The specification of the function in the XPG standard is rather vague,
2220
 
leaving out a few important pieces of information.  Most importantly, it
2221
 
does not specify what happens to those elements of <var>tm</var> which are
2222
 
not directly initialized by the different formats.  The
2223
 
implementations on different Unix systems vary here.
2224
 
</p>
2225
 
<p>The GNU libc implementation does not touch those fields which are not
2226
 
directly initialized.  Exceptions are the <code>tm_wday</code> and
2227
 
<code>tm_yday</code> elements, which are recomputed if any of the year, month,
2228
 
or date elements changed.  This has two implications:
2229
 
</p>
2230
 
<ul>
2231
 
<li>
2232
 
Before calling the <code>strptime</code> function for a new input string, you
2233
 
should prepare the <var>tm</var> structure you pass.  Normally this will mean
2234
 
initializing all values are to zero.  Alternatively, you can set all
2235
 
fields to values like <code>INT_MAX</code>, allowing you to determine which
2236
 
elements were set by the function call.  Zero does not work here since
2237
 
it is a valid value for many of the fields.
2238
 
 
2239
 
<p>Careful initialization is necessary if you want to find out whether a
2240
 
certain field in <var>tm</var> was initialized by the function call.
2241
 
</p>
2242
 
</li><li>
2243
 
You can construct a <code>struct tm</code> value with several consecutive
2244
 
<code>strptime</code> calls.  A useful application of this is e.g. the parsing
2245
 
of two separate strings, one containing date information and the other
2246
 
time information.  By parsing one after the other without clearing the
2247
 
structure in-between, you can construct a complete broken-down time.
2248
 
</li></ul>
2249
 
 
2250
 
<p>The following example shows a function which parses a string which is
2251
 
contains the date information in either US style or ISO 8601 form:
2252
 
</p>
2253
 
<table><tr><td>&nbsp;</td><td><pre class="smallexample">const char *
2254
 
parse_date (const char *input, struct tm *tm)
2255
 
{
2256
 
  const char *cp;
2257
 
 
2258
 
  /* <span class="roman">First clear the result structure.</span>  */
2259
 
  memset (tm, '\0', sizeof (*tm));
2260
 
 
2261
 
  /* <span class="roman">Try the ISO format first.</span>  */
2262
 
  cp = strptime (input, &quot;%F&quot;, tm);
2263
 
  if (cp == NULL)
2264
 
    {
2265
 
      /* <span class="roman">Does not match.  Try the US form.</span>  */
2266
 
      cp = strptime (input, &quot;%D&quot;, tm);
2267
 
    }
2268
 
 
2269
 
  return cp;
2270
 
}
2271
 
</pre></td></tr></table>
2272
 
 
2273
 
<hr size="6">
2274
 
<a name="General-Time-String-Parsing"></a>
2275
 
<table cellpadding="1" cellspacing="1" border="0">
2276
 
<tr><td valign="middle" align="left">[<a href="#Low_002dLevel-Time-String-Parsing" title="Previous section in reading order"> &lt; </a>]</td>
2277
 
<td valign="middle" align="left">[<a href="#TZ-Variable" title="Next section in reading order"> &gt; </a>]</td>
2278
 
<td valign="middle" align="left"> &nbsp; </td>
2279
 
<td valign="middle" align="left">[<a href="#Date-and-Time" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
2280
 
<td valign="middle" align="left">[<a href="#Parsing-Date-and-Time" title="Up section"> Up </a>]</td>
2281
 
<td valign="middle" align="left">[<a href="libc_22.html#Resource-Usage-And-Limitation" title="Next chapter"> &gt;&gt; </a>]</td>
2282
 
<td valign="middle" align="left"> &nbsp; </td>
2283
 
<td valign="middle" align="left"> &nbsp; </td>
2284
 
<td valign="middle" align="left"> &nbsp; </td>
2285
 
<td valign="middle" align="left"> &nbsp; </td>
2286
 
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
2287
 
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
2288
 
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
2289
 
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
2290
 
</tr></table>
2291
 
<a name="A-More-User_002dfriendly-Way-to-Parse-Times-and-Dates"></a>
2292
 
<h4 class="subsubsection">21.4.6.2 A More User-friendly Way to Parse Times and Dates</h4>
2293
 
 
2294
 
<p>The Unix standard defines another function for parsing date strings.
2295
 
The interface is weird, but if the function happens to suit your
2296
 
application it is just fine.  It is problematic to use this function
2297
 
in multi-threaded programs or libraries, since it returns a pointer to
2298
 
a static variable, and uses a global variable and global state (an
2299
 
environment variable).
2300
 
</p>
2301
 
<dl>
2302
 
<dt><a name="index-getdate_005ferr"></a><u>Variable:</u> <b>getdate_err</b></dt>
2303
 
<dd><p>This variable of type <code>int</code> contains the error code of the last
2304
 
unsuccessful call to <code>getdate</code>.  Defined values are:
2305
 
</p>
2306
 
<dl compact="compact">
2307
 
<dt> <em>1</em></dt>
2308
 
<dd><p>The environment variable <code>DATEMSK</code> is not defined or null.
2309
 
</p></dd>
2310
 
<dt> <em>2</em></dt>
2311
 
<dd><p>The template file denoted by the <code>DATEMSK</code> environment variable
2312
 
cannot be opened.
2313
 
</p></dd>
2314
 
<dt> <em>3</em></dt>
2315
 
<dd><p>Information about the template file cannot retrieved.
2316
 
</p></dd>
2317
 
<dt> <em>4</em></dt>
2318
 
<dd><p>The template file is not a regular file.
2319
 
</p></dd>
2320
 
<dt> <em>5</em></dt>
2321
 
<dd><p>An I/O error occurred while reading the template file.
2322
 
</p></dd>
2323
 
<dt> <em>6</em></dt>
2324
 
<dd><p>Not enough memory available to execute the function.
2325
 
</p></dd>
2326
 
<dt> <em>7</em></dt>
2327
 
<dd><p>The template file contains no matching template.
2328
 
</p></dd>
2329
 
<dt> <em>8</em></dt>
2330
 
<dd><p>The input date is invalid, but would match a template otherwise.  This
2331
 
includes dates like February 31st, and dates which cannot be represented
2332
 
in a <code>time_t</code> variable.
2333
 
</p></dd>
2334
 
</dl>
2335
 
</dd></dl>
2336
 
 
2337
 
<dl>
2338
 
<dt><a name="index-getdate"></a><u>Function:</u> struct tm * <b>getdate</b><i> (const char *<var>string</var>)</i></dt>
2339
 
<dd><p>The interface to <code>getdate</code> is the simplest possible for a function
2340
 
to parse a string and return the value.  <var>string</var> is the input
2341
 
string and the result is returned in a statically-allocated variable.
2342
 
</p>
2343
 
<p>The details about how the string is processed are hidden from the user.
2344
 
In fact, they can be outside the control of the program.  Which formats
2345
 
are recognized is controlled by the file named by the environment
2346
 
variable <code>DATEMSK</code>.  This file should contain
2347
 
lines of valid format strings which could be passed to <code>strptime</code>.
2348
 
</p>
2349
 
<p>The <code>getdate</code> function reads these format strings one after the
2350
 
other and tries to match the input string.  The first line which
2351
 
completely matches the input string is used.
2352
 
</p>
2353
 
<p>Elements not initialized through the format string retain the values
2354
 
present at the time of the <code>getdate</code> function call.
2355
 
</p>
2356
 
<p>The formats recognized by <code>getdate</code> are the same as for
2357
 
<code>strptime</code>.  See above for an explanation.  There are only a few
2358
 
extensions to the <code>strptime</code> behavior:
2359
 
</p>
2360
 
<ul>
2361
 
<li>
2362
 
If the <code>%Z</code> format is given the broken-down time is based on the
2363
 
current time of the timezone matched, not of the current timezone of the
2364
 
runtime environment.
2365
 
 
2366
 
<p><em>Note</em>: This is not implemented (currently).  The problem is that
2367
 
timezone names are not unique.  If a fixed timezone is assumed for a
2368
 
given string (say <code>EST</code> meaning US East Coast time), then uses for
2369
 
countries other than the USA will fail.  So far we have found no good
2370
 
solution to this.
2371
 
</p>
2372
 
</li><li>
2373
 
If only the weekday is specified the selected day depends on the current
2374
 
date.  If the current weekday is greater or equal to the <code>tm_wday</code>
2375
 
value the current week&rsquo;s day is chosen, otherwise the day next week is chosen.
2376
 
 
2377
 
</li><li>
2378
 
A similar heuristic is used when only the month is given and not the
2379
 
year.  If the month is greater than or equal to the current month, then
2380
 
the current year is used.  Otherwise it wraps to next year.  The first
2381
 
day of the month is assumed if one is not explicitly specified.
2382
 
 
2383
 
</li><li>
2384
 
The current hour, minute, and second are used if the appropriate value is
2385
 
not set through the format.
2386
 
 
2387
 
</li><li>
2388
 
If no date is given tomorrow&rsquo;s date is used if the time is
2389
 
smaller than the current time.  Otherwise today&rsquo;s date is taken.
2390
 
</li></ul>
2391
 
 
2392
 
<p>It should be noted that the format in the template file need not only
2393
 
contain format elements.  The following is a list of possible format
2394
 
strings (taken from the Unix standard):
2395
 
</p>
2396
 
<table><tr><td>&nbsp;</td><td><pre class="smallexample">%m
2397
 
%A %B %d, %Y %H:%M:%S
2398
 
%A
2399
 
%B
2400
 
%m/%d/%y %I %p
2401
 
%d,%m,%Y %H:%M
2402
 
at %A the %dst of %B in %Y
2403
 
run job at %I %p,%B %dnd
2404
 
%A den %d. %B %Y %H.%M Uhr
2405
 
</pre></td></tr></table>
2406
 
 
2407
 
<p>As you can see, the template list can contain very specific strings like
2408
 
<code>run job at %I %p,%B %dnd</code>.  Using the above list of templates and
2409
 
assuming the current time is Mon Sep 22 12:19:47 EDT 1986 we can obtain the
2410
 
following results for the given input.
2411
 
</p>
2412
 
<table>
2413
 
<tr><td>Input</td><td>Match</td><td>Result</td></tr>
2414
 
<tr><td>Mon</td><td>%a</td><td>Mon Sep 22 12:19:47 EDT 1986</td></tr>
2415
 
<tr><td>Sun</td><td>%a</td><td>Sun Sep 28 12:19:47 EDT 1986</td></tr>
2416
 
<tr><td>Fri</td><td>%a</td><td>Fri Sep 26 12:19:47 EDT 1986</td></tr>
2417
 
<tr><td>September</td><td>%B</td><td>Mon Sep 1 12:19:47 EDT 1986</td></tr>
2418
 
<tr><td>January</td><td>%B</td><td>Thu Jan 1 12:19:47 EST 1987</td></tr>
2419
 
<tr><td>December</td><td>%B</td><td>Mon Dec 1 12:19:47 EST 1986</td></tr>
2420
 
<tr><td>Sep Mon</td><td>%b %a</td><td>Mon Sep 1 12:19:47 EDT 1986</td></tr>
2421
 
<tr><td>Jan Fri</td><td>%b %a</td><td>Fri Jan 2 12:19:47 EST 1987</td></tr>
2422
 
<tr><td>Dec Mon</td><td>%b %a</td><td>Mon Dec 1 12:19:47 EST 1986</td></tr>
2423
 
<tr><td>Jan Wed 1989</td><td>%b %a %Y</td><td>Wed Jan 4 12:19:47 EST 1989</td></tr>
2424
 
<tr><td>Fri 9</td><td>%a %H</td><td>Fri Sep 26 09:00:00 EDT 1986</td></tr>
2425
 
<tr><td>Feb 10:30</td><td>%b %H:%S</td><td>Sun Feb 1 10:00:30 EST 1987</td></tr>
2426
 
<tr><td>10:30</td><td>%H:%M</td><td>Tue Sep 23 10:30:00 EDT 1986</td></tr>
2427
 
<tr><td>13:30</td><td>%H:%M</td><td>Mon Sep 22 13:30:00 EDT 1986</td></tr>
2428
 
</table>
2429
 
 
2430
 
<p>The return value of the function is a pointer to a static variable of
2431
 
type <code>struct tm</code>, or a null pointer if an error occurred.  The
2432
 
result is only valid until the next <code>getdate</code> call, making this
2433
 
function unusable in multi-threaded applications.
2434
 
</p>
2435
 
<p>The <code>errno</code> variable is <em>not</em> changed.  Error conditions are
2436
 
stored in the global variable <code>getdate_err</code>.  See the
2437
 
description above for a list of the possible error values.
2438
 
</p>
2439
 
<p><em>Warning:</em> The <code>getdate</code> function should <em>never</em> be
2440
 
used in SUID-programs.  The reason is obvious: using the
2441
 
<code>DATEMSK</code> environment variable you can get the function to open
2442
 
any arbitrary file and chances are high that with some bogus input
2443
 
(such as a binary file) the program will crash.
2444
 
</p></dd></dl>
2445
 
 
2446
 
<dl>
2447
 
<dt><a name="index-getdate_005fr"></a><u>Function:</u> int <b>getdate_r</b><i> (const char *<var>string</var>, struct tm *<var>tp</var>)</i></dt>
2448
 
<dd><p>The <code>getdate_r</code> function is the reentrant counterpart of
2449
 
<code>getdate</code>.  It does not use the global variable <code>getdate_err</code>
2450
 
to signal an error, but instead returns an error code.  The same error
2451
 
codes as described in the <code>getdate_err</code> documentation above are
2452
 
used, with 0 meaning success.
2453
 
</p>
2454
 
<p>Moreover, <code>getdate_r</code> stores the broken-down time in the variable
2455
 
of type <code>struct tm</code> pointed to by the second argument, rather than
2456
 
in a static variable.
2457
 
</p>
2458
 
<p>This function is not defined in the Unix standard.  Nevertheless it is
2459
 
available on some other Unix systems as well.
2460
 
</p>
2461
 
<p>The warning against using <code>getdate</code> in SUID-programs applies to
2462
 
<code>getdate_r</code> as well.
2463
 
</p></dd></dl>
2464
 
 
2465
 
<hr size="6">
2466
 
<a name="TZ-Variable"></a>
2467
 
<table cellpadding="1" cellspacing="1" border="0">
2468
 
<tr><td valign="middle" align="left">[<a href="#General-Time-String-Parsing" title="Previous section in reading order"> &lt; </a>]</td>
2469
 
<td valign="middle" align="left">[<a href="#Time-Zone-Functions" title="Next section in reading order"> &gt; </a>]</td>
2470
 
<td valign="middle" align="left"> &nbsp; </td>
2471
 
<td valign="middle" align="left">[<a href="#Date-and-Time" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
2472
 
<td valign="middle" align="left">[<a href="#Calendar-Time" title="Up section"> Up </a>]</td>
2473
 
<td valign="middle" align="left">[<a href="libc_22.html#Resource-Usage-And-Limitation" title="Next chapter"> &gt;&gt; </a>]</td>
2474
 
<td valign="middle" align="left"> &nbsp; </td>
2475
 
<td valign="middle" align="left"> &nbsp; </td>
2476
 
<td valign="middle" align="left"> &nbsp; </td>
2477
 
<td valign="middle" align="left"> &nbsp; </td>
2478
 
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
2479
 
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
2480
 
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
2481
 
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
2482
 
</tr></table>
2483
 
<a name="Specifying-the-Time-Zone-with-TZ"></a>
2484
 
<h3 class="subsection">21.4.7 Specifying the Time Zone with <code>TZ</code></h3>
2485
 
 
2486
 
<p>In POSIX systems, a user can specify the time zone by means of the
2487
 
<code>TZ</code> environment variable.  For information about how to set
2488
 
environment variables, see <a href="libc_25.html#Environment-Variables">Environment Variables</a>.  The functions
2489
 
for accessing the time zone are declared in &lsquo;<tt>time.h</tt>&rsquo;.
2490
 
<a name="index-time_002eh-4"></a>
2491
 
<a name="index-time-zone"></a>
2492
 
</p>
2493
 
<p>You should not normally need to set <code>TZ</code>.  If the system is
2494
 
configured properly, the default time zone will be correct.  You might
2495
 
set <code>TZ</code> if you are using a computer over a network from a
2496
 
different time zone, and would like times reported to you in the time
2497
 
zone local to you, rather than what is local to the computer.
2498
 
</p>
2499
 
<p>In POSIX.1 systems the value of the <code>TZ</code> variable can be in one of
2500
 
three formats.  With the GNU C library, the most common format is the
2501
 
last one, which can specify a selection from a large database of time
2502
 
zone information for many regions of the world.  The first two formats
2503
 
are used to describe the time zone information directly, which is both
2504
 
more cumbersome and less precise.  But the POSIX.1 standard only
2505
 
specifies the details of the first two formats, so it is good to be
2506
 
familiar with them in case you come across a POSIX.1 system that doesn&rsquo;t
2507
 
support a time zone information database.
2508
 
</p>
2509
 
<p>The first format is used when there is no Daylight Saving Time (or
2510
 
summer time) in the local time zone:
2511
 
</p>
2512
 
<table><tr><td>&nbsp;</td><td><pre class="smallexample"><span class="roman"><var>std</var> <var>offset</var></span>
2513
 
</pre></td></tr></table>
2514
 
 
2515
 
<p>The <var>std</var> string specifies the name of the time zone.  It must be
2516
 
three or more characters long and must not contain a leading colon,
2517
 
embedded digits, commas, nor plus and minus signs.  There is no space
2518
 
character separating the time zone name from the <var>offset</var>, so these
2519
 
restrictions are necessary to parse the specification correctly.
2520
 
</p>
2521
 
<p>The <var>offset</var> specifies the time value you must add to the local time
2522
 
to get a Coordinated Universal Time value.  It has syntax like
2523
 
[<code>+</code>|<code>-</code>]<var>hh</var>[<code>:</code><var>mm</var>[<code>:</code><var>ss</var>]].  This
2524
 
is positive if the local time zone is west of the Prime Meridian and
2525
 
negative if it is east.  The hour must be between <code>0</code> and
2526
 
<code>23</code>, and the minute and seconds between <code>0</code> and <code>59</code>.
2527
 
</p>
2528
 
<p>For example, here is how we would specify Eastern Standard Time, but
2529
 
without any Daylight Saving Time alternative:
2530
 
</p>
2531
 
<table><tr><td>&nbsp;</td><td><pre class="smallexample">EST+5
2532
 
</pre></td></tr></table>
2533
 
 
2534
 
<p>The second format is used when there is Daylight Saving Time:
2535
 
</p>
2536
 
<table><tr><td>&nbsp;</td><td><pre class="smallexample"><span class="roman"><var>std</var> <var>offset</var> <var>dst</var> [<var>offset</var>]<code>,</code><var>start</var>[<code>/</code><var>time</var>]<code>,</code><var>end</var>[<code>/</code><var>time</var>]</span>
2537
 
</pre></td></tr></table>
2538
 
 
2539
 
<p>The initial <var>std</var> and <var>offset</var> specify the standard time zone, as
2540
 
described above.  The <var>dst</var> string and <var>offset</var> specify the name
2541
 
and offset for the corresponding Daylight Saving Time zone; if the
2542
 
<var>offset</var> is omitted, it defaults to one hour ahead of standard time.
2543
 
</p>
2544
 
<p>The remainder of the specification describes when Daylight Saving Time is
2545
 
in effect.  The <var>start</var> field is when Daylight Saving Time goes into
2546
 
effect and the <var>end</var> field is when the change is made back to standard
2547
 
time.  The following formats are recognized for these fields:
2548
 
</p>
2549
 
<dl compact="compact">
2550
 
<dt> <code>J<var>n</var></code></dt>
2551
 
<dd><p>This specifies the Julian day, with <var>n</var> between <code>1</code> and <code>365</code>.
2552
 
February 29 is never counted, even in leap years.
2553
 
</p>
2554
 
</dd>
2555
 
<dt> <code><var>n</var></code></dt>
2556
 
<dd><p>This specifies the Julian day, with <var>n</var> between <code>0</code> and <code>365</code>.
2557
 
February 29 is counted in leap years.
2558
 
</p>
2559
 
</dd>
2560
 
<dt> <code>M<var>m</var>.<var>w</var>.<var>d</var></code></dt>
2561
 
<dd><p>This specifies day <var>d</var> of week <var>w</var> of month <var>m</var>.  The day
2562
 
<var>d</var> must be between <code>0</code> (Sunday) and <code>6</code>.  The week
2563
 
<var>w</var> must be between <code>1</code> and <code>5</code>; week <code>1</code> is the
2564
 
first week in which day <var>d</var> occurs, and week <code>5</code> specifies the
2565
 
<em>last</em> <var>d</var> day in the month.  The month <var>m</var> should be
2566
 
between <code>1</code> and <code>12</code>.
2567
 
</p></dd>
2568
 
</dl>
2569
 
 
2570
 
<p>The <var>time</var> fields specify when, in the local time currently in
2571
 
effect, the change to the other time occurs.  If omitted, the default is
2572
 
<code>02:00:00</code>.
2573
 
</p>
2574
 
<p>For example, here is how you would specify the Eastern time zone in the
2575
 
United States, including the appropriate Daylight Saving Time and its dates
2576
 
of applicability.  The normal offset from UTC is 5 hours; since this is
2577
 
west of the prime meridian, the sign is positive.  Summer time begins on
2578
 
the first Sunday in April at 2:00am, and ends on the last Sunday in October
2579
 
at 2:00am.
2580
 
</p>
2581
 
<table><tr><td>&nbsp;</td><td><pre class="smallexample">EST+5EDT,M4.1.0/2,M10.5.0/2
2582
 
</pre></td></tr></table>
2583
 
 
2584
 
<p>The schedule of Daylight Saving Time in any particular jurisdiction has
2585
 
changed over the years.  To be strictly correct, the conversion of dates
2586
 
and times in the past should be based on the schedule that was in effect
2587
 
then.  However, this format has no facilities to let you specify how the
2588
 
schedule has changed from year to year.  The most you can do is specify
2589
 
one particular schedule&mdash;usually the present day schedule&mdash;and this is
2590
 
used to convert any date, no matter when.  For precise time zone
2591
 
specifications, it is best to use the time zone information database
2592
 
(see below).
2593
 
</p>
2594
 
<p>The third format looks like this:
2595
 
</p>
2596
 
<table><tr><td>&nbsp;</td><td><pre class="smallexample">:<var>characters</var>
2597
 
</pre></td></tr></table>
2598
 
 
2599
 
<p>Each operating system interprets this format differently; in the GNU C
2600
 
library, <var>characters</var> is the name of a file which describes the time
2601
 
zone.
2602
 
</p>
2603
 
<a name="index-_002fetc_002flocaltime"></a>
2604
 
<a name="index-localtime-1"></a>
2605
 
<p>If the <code>TZ</code> environment variable does not have a value, the
2606
 
operation chooses a time zone by default.  In the GNU C library, the
2607
 
default time zone is like the specification &lsquo;<samp>TZ=:/etc/localtime</samp>&rsquo;
2608
 
(or &lsquo;<samp>TZ=:/usr/local/etc/localtime</samp>&rsquo;, depending on how GNU C library
2609
 
was configured; see section <a href="libc_36.html#Installation">Installing the GNU C Library</a>).  Other C libraries use their own
2610
 
rule for choosing the default time zone, so there is little we can say
2611
 
about them.
2612
 
</p>
2613
 
<a name="index-time-zone-database"></a>
2614
 
<a name="index-_002fshare_002flib_002fzoneinfo"></a>
2615
 
<a name="index-zoneinfo"></a>
2616
 
<p>If <var>characters</var> begins with a slash, it is an absolute file name;
2617
 
otherwise the library looks for the file
2618
 
&lsquo;<tt>/share/lib/zoneinfo/<var>characters</var></tt>&rsquo;.  The &lsquo;<tt>zoneinfo</tt>&rsquo;
2619
 
directory contains data files describing local time zones in many
2620
 
different parts of the world.  The names represent major cities, with
2621
 
subdirectories for geographical areas; for example,
2622
 
&lsquo;<tt>America/New_York</tt>&rsquo;, &lsquo;<tt>Europe/London</tt>&rsquo;, &lsquo;<tt>Asia/Hong_Kong</tt>&rsquo;.
2623
 
These data files are installed by the system administrator, who also
2624
 
sets &lsquo;<tt>/etc/localtime</tt>&rsquo; to point to the data file for the local time
2625
 
zone.  The GNU C library comes with a large database of time zone
2626
 
information for most regions of the world, which is maintained by a
2627
 
community of volunteers and put in the public domain.
2628
 
</p>
2629
 
<hr size="6">
2630
 
<a name="Time-Zone-Functions"></a>
2631
 
<table cellpadding="1" cellspacing="1" border="0">
2632
 
<tr><td valign="middle" align="left">[<a href="#TZ-Variable" title="Previous section in reading order"> &lt; </a>]</td>
2633
 
<td valign="middle" align="left">[<a href="#Time-Functions-Example" title="Next section in reading order"> &gt; </a>]</td>
2634
 
<td valign="middle" align="left"> &nbsp; </td>
2635
 
<td valign="middle" align="left">[<a href="#Date-and-Time" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
2636
 
<td valign="middle" align="left">[<a href="#Calendar-Time" title="Up section"> Up </a>]</td>
2637
 
<td valign="middle" align="left">[<a href="libc_22.html#Resource-Usage-And-Limitation" title="Next chapter"> &gt;&gt; </a>]</td>
2638
 
<td valign="middle" align="left"> &nbsp; </td>
2639
 
<td valign="middle" align="left"> &nbsp; </td>
2640
 
<td valign="middle" align="left"> &nbsp; </td>
2641
 
<td valign="middle" align="left"> &nbsp; </td>
2642
 
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
2643
 
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
2644
 
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
2645
 
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
2646
 
</tr></table>
2647
 
<a name="Functions-and-Variables-for-Time-Zones"></a>
2648
 
<h3 class="subsection">21.4.8 Functions and Variables for Time Zones</h3>
2649
 
 
2650
 
<dl>
2651
 
<dt><a name="index-tzname"></a><u>Variable:</u> char * <b>tzname</b><i> [2]</i></dt>
2652
 
<dd><p>The array <code>tzname</code> contains two strings, which are the standard
2653
 
names of the pair of time zones (standard and Daylight
2654
 
Saving) that the user has selected.  <code>tzname[0]</code> is the name of
2655
 
the standard time zone (for example, <code>&quot;EST&quot;</code>), and <code>tzname[1]</code>
2656
 
is the name for the time zone when Daylight Saving Time is in use (for
2657
 
example, <code>&quot;EDT&quot;</code>).  These correspond to the <var>std</var> and <var>dst</var>
2658
 
strings (respectively) from the <code>TZ</code> environment variable.  If
2659
 
Daylight Saving Time is never used, <code>tzname[1]</code> is the empty string.
2660
 
</p>
2661
 
<p>The <code>tzname</code> array is initialized from the <code>TZ</code> environment
2662
 
variable whenever <code>tzset</code>, <code>ctime</code>, <code>strftime</code>,
2663
 
<code>mktime</code>, or <code>localtime</code> is called.  If multiple abbreviations
2664
 
have been used (e.g. <code>&quot;EWT&quot;</code> and <code>&quot;EDT&quot;</code> for U.S. Eastern War
2665
 
Time and Eastern Daylight Time), the array contains the most recent
2666
 
abbreviation.
2667
 
</p>
2668
 
<p>The <code>tzname</code> array is required for POSIX.1 compatibility, but in
2669
 
GNU programs it is better to use the <code>tm_zone</code> member of the
2670
 
broken-down time structure, since <code>tm_zone</code> reports the correct
2671
 
abbreviation even when it is not the latest one.
2672
 
</p>
2673
 
<p>Though the strings are declared as <code>char *</code> the user must refrain
2674
 
from modifying these strings.  Modifying the strings will almost certainly
2675
 
lead to trouble.
2676
 
</p>
2677
 
</dd></dl>
2678
 
 
2679
 
<dl>
2680
 
<dt><a name="index-tzset"></a><u>Function:</u> void <b>tzset</b><i> (void)</i></dt>
2681
 
<dd><p>The <code>tzset</code> function initializes the <code>tzname</code> variable from
2682
 
the value of the <code>TZ</code> environment variable.  It is not usually
2683
 
necessary for your program to call this function, because it is called
2684
 
automatically when you use the other time conversion functions that
2685
 
depend on the time zone.
2686
 
</p></dd></dl>
2687
 
 
2688
 
<p>The following variables are defined for compatibility with System V
2689
 
Unix.  Like <code>tzname</code>, these variables are set by calling
2690
 
<code>tzset</code> or the other time conversion functions.
2691
 
</p>
2692
 
<dl>
2693
 
<dt><a name="index-timezone"></a><u>Variable:</u> long int <b>timezone</b></dt>
2694
 
<dd><p>This contains the difference between UTC and the latest local standard
2695
 
time, in seconds west of UTC.  For example, in the U.S. Eastern time
2696
 
zone, the value is <code>5*60*60</code>.  Unlike the <code>tm_gmtoff</code> member
2697
 
of the broken-down time structure, this value is not adjusted for
2698
 
daylight saving, and its sign is reversed.  In GNU programs it is better
2699
 
to use <code>tm_gmtoff</code>, since it contains the correct offset even when
2700
 
it is not the latest one.
2701
 
</p></dd></dl>
2702
 
 
2703
 
<dl>
2704
 
<dt><a name="index-daylight"></a><u>Variable:</u> int <b>daylight</b></dt>
2705
 
<dd><p>This variable has a nonzero value if Daylight Saving Time rules apply.
2706
 
A nonzero value does not necessarily mean that Daylight Saving Time is
2707
 
now in effect; it means only that Daylight Saving Time is sometimes in
2708
 
effect.
2709
 
</p></dd></dl>
2710
 
 
2711
 
<hr size="6">
2712
 
<a name="Time-Functions-Example"></a>
2713
 
<table cellpadding="1" cellspacing="1" border="0">
2714
 
<tr><td valign="middle" align="left">[<a href="#Time-Zone-Functions" title="Previous section in reading order"> &lt; </a>]</td>
2715
 
<td valign="middle" align="left">[<a href="#Setting-an-Alarm" title="Next section in reading order"> &gt; </a>]</td>
2716
 
<td valign="middle" align="left"> &nbsp; </td>
2717
 
<td valign="middle" align="left">[<a href="#Date-and-Time" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
2718
 
<td valign="middle" align="left">[<a href="#Calendar-Time" title="Up section"> Up </a>]</td>
2719
 
<td valign="middle" align="left">[<a href="libc_22.html#Resource-Usage-And-Limitation" title="Next chapter"> &gt;&gt; </a>]</td>
2720
 
<td valign="middle" align="left"> &nbsp; </td>
2721
 
<td valign="middle" align="left"> &nbsp; </td>
2722
 
<td valign="middle" align="left"> &nbsp; </td>
2723
 
<td valign="middle" align="left"> &nbsp; </td>
2724
 
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
2725
 
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
2726
 
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
2727
 
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
2728
 
</tr></table>
2729
 
<a name="Time-Functions-Example-1"></a>
2730
 
<h3 class="subsection">21.4.9 Time Functions Example</h3>
2731
 
 
2732
 
<p>Here is an example program showing the use of some of the calendar time
2733
 
functions.
2734
 
</p>
2735
 
<table><tr><td>&nbsp;</td><td><pre class="smallexample">#include &lt;time.h&gt;
2736
 
#include &lt;stdio.h&gt;
2737
 
 
2738
 
#define SIZE 256
2739
 
 
2740
 
int
2741
 
main (void)
2742
 
{
2743
 
  char buffer[SIZE];
2744
 
  time_t curtime;
2745
 
  struct tm *loctime;
2746
 
 
2747
 
  /* <span class="roman">Get the current time.</span> */
2748
 
  curtime = time (NULL);
2749
 
 
2750
 
  /* <span class="roman">Convert it to local time representation.</span> */
2751
 
  loctime = localtime (&amp;curtime);
2752
 
 
2753
 
  /* <span class="roman">Print out the date and time in the standard format.</span> */
2754
 
  fputs (asctime (loctime), stdout);
2755
 
 
2756
 
</pre><pre class="smallexample">  /* <span class="roman">Print it out in a nice format.</span> */
2757
 
  strftime (buffer, SIZE, &quot;Today is %A, %B %d.\n&quot;, loctime);
2758
 
  fputs (buffer, stdout);
2759
 
  strftime (buffer, SIZE, &quot;The time is %I:%M %p.\n&quot;, loctime);
2760
 
  fputs (buffer, stdout);
2761
 
 
2762
 
  return 0;
2763
 
}
2764
 
</pre></td></tr></table>
2765
 
 
2766
 
<p>It produces output like this:
2767
 
</p>
2768
 
<table><tr><td>&nbsp;</td><td><pre class="smallexample">Wed Jul 31 13:02:36 1991
2769
 
Today is Wednesday, July 31.
2770
 
The time is 01:02 PM.
2771
 
</pre></td></tr></table>
2772
 
 
2773
 
 
2774
 
<hr size="6">
2775
 
<a name="Setting-an-Alarm"></a>
2776
 
<table cellpadding="1" cellspacing="1" border="0">
2777
 
<tr><td valign="middle" align="left">[<a href="#Time-Functions-Example" title="Previous section in reading order"> &lt; </a>]</td>
2778
 
<td valign="middle" align="left">[<a href="#Sleeping" title="Next section in reading order"> &gt; </a>]</td>
2779
 
<td valign="middle" align="left"> &nbsp; </td>
2780
 
<td valign="middle" align="left">[<a href="#Date-and-Time" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
2781
 
<td valign="middle" align="left">[<a href="#Date-and-Time" title="Up section"> Up </a>]</td>
2782
 
<td valign="middle" align="left">[<a href="libc_22.html#Resource-Usage-And-Limitation" title="Next chapter"> &gt;&gt; </a>]</td>
2783
 
<td valign="middle" align="left"> &nbsp; </td>
2784
 
<td valign="middle" align="left"> &nbsp; </td>
2785
 
<td valign="middle" align="left"> &nbsp; </td>
2786
 
<td valign="middle" align="left"> &nbsp; </td>
2787
 
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
2788
 
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
2789
 
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
2790
 
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
2791
 
</tr></table>
2792
 
<a name="Setting-an-Alarm-1"></a>
2793
 
<h2 class="section">21.5 Setting an Alarm</h2>
2794
 
 
2795
 
<p>The <code>alarm</code> and <code>setitimer</code> functions provide a mechanism for a
2796
 
process to interrupt itself in the future.  They do this by setting a
2797
 
timer; when the timer expires, the process receives a signal.
2798
 
</p>
2799
 
<a name="index-setting-an-alarm"></a>
2800
 
<a name="index-interval-timer_002c-setting"></a>
2801
 
<a name="index-alarms_002c-setting"></a>
2802
 
<a name="index-timers_002c-setting"></a>
2803
 
<p>Each process has three independent interval timers available:
2804
 
</p>
2805
 
<ul>
2806
 
<li>
2807
 
A real-time timer that counts elapsed time.  This timer sends a
2808
 
<code>SIGALRM</code> signal to the process when it expires.
2809
 
<a name="index-real_002dtime-timer"></a>
2810
 
<a name="index-timer_002c-real_002dtime"></a>
2811
 
 
2812
 
</li><li>
2813
 
A virtual timer that counts processor time used by the process.  This timer
2814
 
sends a <code>SIGVTALRM</code> signal to the process when it expires.
2815
 
<a name="index-virtual-timer"></a>
2816
 
<a name="index-timer_002c-virtual"></a>
2817
 
 
2818
 
</li><li>
2819
 
A profiling timer that counts both processor time used by the process,
2820
 
and processor time spent in system calls on behalf of the process.  This
2821
 
timer sends a <code>SIGPROF</code> signal to the process when it expires.
2822
 
<a name="index-profiling-timer"></a>
2823
 
<a name="index-timer_002c-profiling"></a>
2824
 
 
2825
 
<p>This timer is useful for profiling in interpreters.  The interval timer
2826
 
mechanism does not have the fine granularity necessary for profiling
2827
 
native code.
2828
 
</p></li></ul>
2829
 
 
2830
 
<p>You can only have one timer of each kind set at any given time.  If you
2831
 
set a timer that has not yet expired, that timer is simply reset to the
2832
 
new value.
2833
 
</p>
2834
 
<p>You should establish a handler for the appropriate alarm signal using
2835
 
<code>signal</code> or <code>sigaction</code> before issuing a call to
2836
 
<code>setitimer</code> or <code>alarm</code>.  Otherwise, an unusual chain of events
2837
 
could cause the timer to expire before your program establishes the
2838
 
handler.  In this case it would be terminated, since termination is the
2839
 
default action for the alarm signals.  See section <a href="libc_24.html#Signal-Handling">Signal Handling</a>.
2840
 
</p>
2841
 
<p>To be able to use the alarm function to interrupt a system call which
2842
 
might block otherwise indefinitely it is important to <em>not</em> set the
2843
 
<code>SA_RESTART</code> flag when registering the signal handler using
2844
 
<code>sigaction</code>.  When not using <code>sigaction</code> things get even
2845
 
uglier: the <code>signal</code> function has to fixed semantics with respect
2846
 
to restarts.  The BSD semantics for this function is to set the flag.
2847
 
Therefore, if <code>sigaction</code> for whatever reason cannot be used, it is
2848
 
necessary to use <code>sysv_signal</code> and not <code>signal</code>.
2849
 
</p>
2850
 
<p>The <code>setitimer</code> function is the primary means for setting an alarm.
2851
 
This facility is declared in the header file &lsquo;<tt>sys/time.h</tt>&rsquo;.  The
2852
 
<code>alarm</code> function, declared in &lsquo;<tt>unistd.h</tt>&rsquo;, provides a somewhat
2853
 
simpler interface for setting the real-time timer.
2854
 
<a name="index-unistd_002eh-14"></a>
2855
 
<a name="index-sys_002ftime_002eh-2"></a>
2856
 
</p>
2857
 
<dl>
2858
 
<dt><a name="index-struct-itimerval"></a><u>Data Type:</u> <b>struct itimerval</b></dt>
2859
 
<dd><p>This structure is used to specify when a timer should expire.  It contains
2860
 
the following members:
2861
 
</p><dl compact="compact">
2862
 
<dt> <code>struct timeval it_interval</code></dt>
2863
 
<dd><p>This is the period between successive timer interrupts.  If zero, the
2864
 
alarm will only be sent once.
2865
 
</p>
2866
 
</dd>
2867
 
<dt> <code>struct timeval it_value</code></dt>
2868
 
<dd><p>This is the period between now and the first timer interrupt.  If zero,
2869
 
the alarm is disabled.
2870
 
</p></dd>
2871
 
</dl>
2872
 
 
2873
 
<p>The <code>struct timeval</code> data type is described in <a href="#Elapsed-Time">Elapsed Time</a>.
2874
 
</p></dd></dl>
2875
 
 
2876
 
<dl>
2877
 
<dt><a name="index-setitimer"></a><u>Function:</u> int <b>setitimer</b><i> (int <var>which</var>, struct itimerval *<var>new</var>, struct itimerval *<var>old</var>)</i></dt>
2878
 
<dd><p>The <code>setitimer</code> function sets the timer specified by <var>which</var>
2879
 
according to <var>new</var>.  The <var>which</var> argument can have a value of
2880
 
<code>ITIMER_REAL</code>, <code>ITIMER_VIRTUAL</code>, or <code>ITIMER_PROF</code>.
2881
 
</p>
2882
 
<p>If <var>old</var> is not a null pointer, <code>setitimer</code> returns information
2883
 
about any previous unexpired timer of the same kind in the structure it
2884
 
points to.
2885
 
</p>
2886
 
<p>The return value is <code>0</code> on success and <code>-1</code> on failure.  The
2887
 
following <code>errno</code> error conditions are defined for this function:
2888
 
</p>
2889
 
<dl compact="compact">
2890
 
<dt> <code>EINVAL</code></dt>
2891
 
<dd><p>The timer period is too large.
2892
 
</p></dd>
2893
 
</dl>
2894
 
</dd></dl>
2895
 
 
2896
 
<dl>
2897
 
<dt><a name="index-getitimer"></a><u>Function:</u> int <b>getitimer</b><i> (int <var>which</var>, struct itimerval *<var>old</var>)</i></dt>
2898
 
<dd><p>The <code>getitimer</code> function stores information about the timer specified
2899
 
by <var>which</var> in the structure pointed at by <var>old</var>.
2900
 
</p>
2901
 
<p>The return value and error conditions are the same as for <code>setitimer</code>.
2902
 
</p></dd></dl>
2903
 
 
2904
 
<dl compact="compact">
2905
 
<dt> <code>ITIMER_REAL</code>
2906
 
<a name="index-ITIMER_005fREAL"></a>
2907
 
</dt>
2908
 
<dd><p>This constant can be used as the <var>which</var> argument to the
2909
 
<code>setitimer</code> and <code>getitimer</code> functions to specify the real-time
2910
 
timer.
2911
 
</p>
2912
 
</dd>
2913
 
<dt> <code>ITIMER_VIRTUAL</code>
2914
 
<a name="index-ITIMER_005fVIRTUAL"></a>
2915
 
</dt>
2916
 
<dd><p>This constant can be used as the <var>which</var> argument to the
2917
 
<code>setitimer</code> and <code>getitimer</code> functions to specify the virtual
2918
 
timer.
2919
 
</p>
2920
 
</dd>
2921
 
<dt> <code>ITIMER_PROF</code>
2922
 
<a name="index-ITIMER_005fPROF"></a>
2923
 
</dt>
2924
 
<dd><p>This constant can be used as the <var>which</var> argument to the
2925
 
<code>setitimer</code> and <code>getitimer</code> functions to specify the profiling
2926
 
timer.
2927
 
</p></dd>
2928
 
</dl>
2929
 
 
2930
 
<dl>
2931
 
<dt><a name="index-alarm"></a><u>Function:</u> unsigned int <b>alarm</b><i> (unsigned int <var>seconds</var>)</i></dt>
2932
 
<dd><p>The <code>alarm</code> function sets the real-time timer to expire in
2933
 
<var>seconds</var> seconds.  If you want to cancel any existing alarm, you
2934
 
can do this by calling <code>alarm</code> with a <var>seconds</var> argument of
2935
 
zero.
2936
 
</p>
2937
 
<p>The return value indicates how many seconds remain before the previous
2938
 
alarm would have been sent.  If there is no previous alarm, <code>alarm</code>
2939
 
returns zero.
2940
 
</p></dd></dl>
2941
 
 
2942
 
<p>The <code>alarm</code> function could be defined in terms of <code>setitimer</code>
2943
 
like this:
2944
 
</p>
2945
 
<table><tr><td>&nbsp;</td><td><pre class="smallexample">unsigned int
2946
 
alarm (unsigned int seconds)
2947
 
{
2948
 
  struct itimerval old, new;
2949
 
  new.it_interval.tv_usec = 0;
2950
 
  new.it_interval.tv_sec = 0;
2951
 
  new.it_value.tv_usec = 0;
2952
 
  new.it_value.tv_sec = (long int) seconds;
2953
 
  if (setitimer (ITIMER_REAL, &amp;new, &amp;old) &lt; 0)
2954
 
    return 0;
2955
 
  else
2956
 
    return old.it_value.tv_sec;
2957
 
}
2958
 
</pre></td></tr></table>
2959
 
 
2960
 
<p>There is an example showing the use of the <code>alarm</code> function in
2961
 
<a href="libc_24.html#Handler-Returns">Signal Handlers that Return</a>.
2962
 
</p>
2963
 
<p>If you simply want your process to wait for a given number of seconds,
2964
 
you should use the <code>sleep</code> function.  See section <a href="#Sleeping">Sleeping</a>.
2965
 
</p>
2966
 
<p>You shouldn&rsquo;t count on the signal arriving precisely when the timer
2967
 
expires.  In a multiprocessing environment there is typically some
2968
 
amount of delay involved.
2969
 
</p>
2970
 
<p><strong>Portability Note:</strong> The <code>setitimer</code> and <code>getitimer</code>
2971
 
functions are derived from BSD Unix, while the <code>alarm</code> function is
2972
 
specified by the POSIX.1 standard.  <code>setitimer</code> is more powerful than
2973
 
<code>alarm</code>, but <code>alarm</code> is more widely used.
2974
 
</p>
2975
 
<hr size="6">
2976
 
<a name="Sleeping"></a>
2977
 
<table cellpadding="1" cellspacing="1" border="0">
2978
 
<tr><td valign="middle" align="left">[<a href="#Setting-an-Alarm" title="Previous section in reading order"> &lt; </a>]</td>
2979
 
<td valign="middle" align="left">[<a href="libc_22.html#Resource-Usage-And-Limitation" title="Next section in reading order"> &gt; </a>]</td>
2980
 
<td valign="middle" align="left"> &nbsp; </td>
2981
 
<td valign="middle" align="left">[<a href="#Date-and-Time" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
2982
 
<td valign="middle" align="left">[<a href="#Date-and-Time" title="Up section"> Up </a>]</td>
2983
 
<td valign="middle" align="left">[<a href="libc_22.html#Resource-Usage-And-Limitation" title="Next chapter"> &gt;&gt; </a>]</td>
2984
 
<td valign="middle" align="left"> &nbsp; </td>
2985
 
<td valign="middle" align="left"> &nbsp; </td>
2986
 
<td valign="middle" align="left"> &nbsp; </td>
2987
 
<td valign="middle" align="left"> &nbsp; </td>
2988
 
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
2989
 
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
2990
 
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
2991
 
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
2992
 
</tr></table>
2993
 
<a name="Sleeping-1"></a>
2994
 
<h2 class="section">21.6 Sleeping</h2>
2995
 
 
2996
 
<p>The function <code>sleep</code> gives a simple way to make the program wait
2997
 
for a short interval.  If your program doesn&rsquo;t use signals (except to
2998
 
terminate), then you can expect <code>sleep</code> to wait reliably throughout
2999
 
the specified interval.  Otherwise, <code>sleep</code> can return sooner if a
3000
 
signal arrives; if you want to wait for a given interval regardless of
3001
 
signals, use <code>select</code> (see section <a href="libc_13.html#Waiting-for-I_002fO">Waiting for Input or Output</a>) and don&rsquo;t specify
3002
 
any descriptors to wait for.
3003
 
</p>
3004
 
<dl>
3005
 
<dt><a name="index-sleep"></a><u>Function:</u> unsigned int <b>sleep</b><i> (unsigned int <var>seconds</var>)</i></dt>
3006
 
<dd><p>The <code>sleep</code> function waits for <var>seconds</var> or until a signal
3007
 
is delivered, whichever happens first.
3008
 
</p>
3009
 
<p>If <code>sleep</code> function returns because the requested interval is over,
3010
 
it returns a value of zero.  If it returns because of delivery of a
3011
 
signal, its return value is the remaining time in the sleep interval.
3012
 
</p>
3013
 
<p>The <code>sleep</code> function is declared in &lsquo;<tt>unistd.h</tt>&rsquo;.
3014
 
</p></dd></dl>
3015
 
 
3016
 
<p>Resist the temptation to implement a sleep for a fixed amount of time by
3017
 
using the return value of <code>sleep</code>, when nonzero, to call
3018
 
<code>sleep</code> again.  This will work with a certain amount of accuracy as
3019
 
long as signals arrive infrequently.  But each signal can cause the
3020
 
eventual wakeup time to be off by an additional second or so.  Suppose a
3021
 
few signals happen to arrive in rapid succession by bad luck&mdash;there is
3022
 
no limit on how much this could shorten or lengthen the wait.
3023
 
</p>
3024
 
<p>Instead, compute the calendar time at which the program should stop
3025
 
waiting, and keep trying to wait until that calendar time.  This won&rsquo;t
3026
 
be off by more than a second.  With just a little more work, you can use
3027
 
<code>select</code> and make the waiting period quite accurate.  (Of course,
3028
 
heavy system load can cause additional unavoidable delays&mdash;unless the
3029
 
machine is dedicated to one application, there is no way you can avoid
3030
 
this.)
3031
 
</p>
3032
 
<p>On some systems, <code>sleep</code> can do strange things if your program uses
3033
 
<code>SIGALRM</code> explicitly.  Even if <code>SIGALRM</code> signals are being
3034
 
ignored or blocked when <code>sleep</code> is called, <code>sleep</code> might
3035
 
return prematurely on delivery of a <code>SIGALRM</code> signal.  If you have
3036
 
established a handler for <code>SIGALRM</code> signals and a <code>SIGALRM</code>
3037
 
signal is delivered while the process is sleeping, the action taken
3038
 
might be just to cause <code>sleep</code> to return instead of invoking your
3039
 
handler.  And, if <code>sleep</code> is interrupted by delivery of a signal
3040
 
whose handler requests an alarm or alters the handling of <code>SIGALRM</code>,
3041
 
this handler and <code>sleep</code> will interfere.
3042
 
</p>
3043
 
<p>On the GNU system, it is safe to use <code>sleep</code> and <code>SIGALRM</code> in
3044
 
the same program, because <code>sleep</code> does not work by means of
3045
 
<code>SIGALRM</code>.
3046
 
</p>
3047
 
<dl>
3048
 
<dt><a name="index-nanosleep"></a><u>Function:</u> int <b>nanosleep</b><i> (const struct timespec *<var>requested_time</var>, struct timespec *<var>remaining</var>)</i></dt>
3049
 
<dd><p>If resolution to seconds is not enough the <code>nanosleep</code> function can
3050
 
be used.  As the name suggests the sleep interval can be specified in
3051
 
nanoseconds.  The actual elapsed time of the sleep interval might be
3052
 
longer since the system rounds the elapsed time you request up to the
3053
 
next integer multiple of the actual resolution the system can deliver.
3054
 
</p>
3055
 
<p>*<code>requested_time</code> is the elapsed time of the interval you want to
3056
 
sleep.
3057
 
</p>
3058
 
<p>The function returns as *<code>remaining</code> the elapsed time left in the
3059
 
interval for which you requested to sleep.  If the interval completed
3060
 
without getting interrupted by a signal, this is zero.
3061
 
</p>
3062
 
<p><code>struct timespec</code> is described in See section <a href="#Elapsed-Time">Elapsed Time</a>.
3063
 
</p>
3064
 
<p>If the function returns because the interval is over the return value is
3065
 
zero.  If the function returns <em>-1</em> the global variable <var>errno</var>
3066
 
is set to the following values:
3067
 
</p>
3068
 
<dl compact="compact">
3069
 
<dt> <code>EINTR</code></dt>
3070
 
<dd><p>The call was interrupted because a signal was delivered to the thread.
3071
 
If the <var>remaining</var> parameter is not the null pointer the structure
3072
 
pointed to by <var>remaining</var> is updated to contain the remaining
3073
 
elapsed time.
3074
 
</p>
3075
 
</dd>
3076
 
<dt> <code>EINVAL</code></dt>
3077
 
<dd><p>The nanosecond value in the <var>requested_time</var> parameter contains an
3078
 
illegal value.  Either the value is negative or greater than or equal to
3079
 
1000 million.
3080
 
</p></dd>
3081
 
</dl>
3082
 
 
3083
 
<p>This function is a cancellation point in multi-threaded programs.  This
3084
 
is a problem if the thread allocates some resources (like memory, file
3085
 
descriptors, semaphores or whatever) at the time <code>nanosleep</code> is
3086
 
called.  If the thread gets canceled these resources stay allocated
3087
 
until the program ends.  To avoid this calls to <code>nanosleep</code> should
3088
 
be protected using cancellation handlers.
3089
 
</p>
3090
 
<p>The <code>nanosleep</code> function is declared in &lsquo;<tt>time.h</tt>&rsquo;.
3091
 
</p></dd></dl>
3092
 
<hr size="6">
3093
 
<table cellpadding="1" cellspacing="1" border="0">
3094
 
<tr><td valign="middle" align="left">[<a href="#Date-and-Time" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
3095
 
<td valign="middle" align="left">[<a href="libc_22.html#Resource-Usage-And-Limitation" title="Next chapter"> &gt;&gt; </a>]</td>
3096
 
<td valign="middle" align="left"> &nbsp; </td>
3097
 
<td valign="middle" align="left"> &nbsp; </td>
3098
 
<td valign="middle" align="left"> &nbsp; </td>
3099
 
<td valign="middle" align="left"> &nbsp; </td>
3100
 
<td valign="middle" align="left"> &nbsp; </td>
3101
 
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
3102
 
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
3103
 
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
3104
 
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
3105
 
</tr></table>
3106
 
<p>
3107
 
 <font size="-1">
3108
 
  This document was generated by <em>root</em> on <em>May 20, 2010</em> using <a href="http://www.nongnu.org/texi2html/"><em>texi2html 1.82</em></a>.
3109
 
 </font>
3110
 
 <br>
3111
 
 
3112
 
</p>
3113
 
</body>
3114
 
</html>