]> git.saurik.com Git - apple/libc.git/blob - stdtime/ctime.3
Libc-583.tar.gz
[apple/libc.git] / stdtime / ctime.3
1 .\" Copyright (c) 1989, 1991, 1993
2 .\" The Regents of the University of California. All rights reserved.
3 .\"
4 .\" This code is derived from software contributed to Berkeley by
5 .\" Arthur Olson.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
8 .\" are met:
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. All advertising materials mentioning features or use of this software
15 .\" must display the following acknowledgement:
16 .\" This product includes software developed by the University of
17 .\" California, Berkeley and its contributors.
18 .\" 4. Neither the name of the University nor the names of its contributors
19 .\" may be used to endorse or promote products derived from this software
20 .\" without specific prior written permission.
21 .\"
22 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .\" SUCH DAMAGE.
33 .\"
34 .\" From: @(#)ctime.3 8.1 (Berkeley) 6/4/93
35 .\" $FreeBSD: src/lib/libc/stdtime/ctime.3,v 1.22 2004/07/02 23:52:12 ru Exp $
36 .\"
37 .Dd January 2, 1999
38 .Dt CTIME 3
39 .Os
40 .Sh NAME
41 .Nm asctime ,
42 .Nm asctime_r ,
43 .Nm ctime ,
44 .Nm ctime_r ,
45 .Nm difftime ,
46 .Nm gmtime ,
47 .Nm gmtime_r ,
48 .Nm localtime ,
49 .Nm localtime_r ,
50 .Nm mktime ,
51 .Nm timegm
52 .Nd transform binary date and time values
53 .Sh LIBRARY
54 .Lb libc
55 .Sh SYNOPSIS
56 .In time.h
57 .Vt extern char *tzname[2] ;
58 .Ft char *
59 .Fn asctime "const struct tm *timeptr"
60 .Ft char *
61 .Fn asctime_r "const struct tm *restrict timeptr" "char *restrict buf"
62 .Ft char *
63 .Fn ctime "const time_t *clock"
64 .Ft char *
65 .Fn ctime_r "const time_t *clock" "char *buf"
66 .Ft double
67 .Fn difftime "time_t time1" "time_t time0"
68 .Ft struct tm *
69 .Fn gmtime "const time_t *clock"
70 .Ft struct tm *
71 .Fn gmtime_r "const time_t *clock" "struct tm *result"
72 .Ft struct tm *
73 .Fn localtime "const time_t *clock"
74 .Ft struct tm *
75 .Fn localtime_r "const time_t *clock" "struct tm *result"
76 .Ft time_t
77 .Fn mktime "struct tm *timeptr"
78 .Ft time_t
79 .Fn timegm "struct tm *timeptr"
80 .Sh DESCRIPTION
81 The functions
82 .Fn ctime ,
83 .Fn gmtime ,
84 and
85 .Fn localtime
86 all take as an argument a time value representing the time in seconds since
87 the Epoch (00:00:00
88 .Tn UTC ,
89 January 1, 1970; see
90 .Xr time 3 ) .
91 .Pp
92 The function
93 .Fn localtime
94 converts the time value pointed at by
95 .Fa clock .
96 It returns a pointer to a
97 .Dq Fa struct tm
98 (described below), which contains
99 the broken-out time information for the value after adjusting for the current
100 time zone (and any other factors such as Daylight Saving Time).
101 Time zone adjustments are performed as specified by the
102 .Ev TZ
103 environment variable (see
104 .Xr tzset 3 ) .
105 The function
106 .Fn localtime
107 uses
108 .Xr tzset 3
109 to initialize time conversion information, if
110 .Xr tzset 3
111 has not already been called by the process.
112 .Pp
113 After filling in the tm structure,
114 .Fn localtime
115 sets the
116 .Fa tm_isdst Ns 'th
117 element of
118 .Fa tzname
119 to a pointer to an
120 .Tn ASCII
121 string containing the time zone abbreviation to be
122 used with
123 .Fn localtime Ns 's
124 return value.
125 .Pp
126 The function
127 .Fn gmtime
128 also converts the time value, but makes no time zone adjustment.
129 It returns a pointer to a tm structure (described below).
130 .Pp
131 The
132 .Fn ctime
133 function
134 adjusts the time value for the current time zone, in the same manner as
135 .Fn localtime .
136 It returns a pointer to a 26-character string of the form:
137 .Bd -literal -offset indent
138 Thu Nov 24 18:22:48 1986\en\e0
139 .Ed
140 .Pp
141 All of the fields have constant width.
142 .Pp
143 The
144 .Fn ctime_r
145 function
146 provides the same functionality as
147 .Fn ctime ,
148 except that the caller must provide the output buffer
149 .Fa buf
150 (which must be at least 26 characters long) to store the result.
151 The
152 .Fn localtime_r
153 and
154 .Fn gmtime_r
155 functions
156 provide the same functionality as
157 .Fn localtime
158 and
159 .Fn gmtime ,
160 respectively, except the caller must provide the output buffer
161 .Fa result .
162 .Pp
163 The
164 .Fn asctime
165 function
166 converts the broken-out time in the structure
167 .Fa tm
168 (pointed at by
169 .Fa *timeptr )
170 to the form
171 shown in the example above.
172 .Pp
173 The
174 .Fn asctime_r
175 function
176 provides the same functionality as
177 .Fn asctime ,
178 except that the caller provides the output buffer
179 .Fa buf
180 (which must be at least 26 characters long) to store the result.
181 .Pp
182 The functions
183 .Fn mktime
184 and
185 .Fn timegm
186 convert the broken-out time
187 (in the structure pointed to by
188 .Fa *timeptr )
189 into a time value with the same encoding as that of the
190 values returned by the
191 .Xr time 3
192 function (that is, seconds from the Epoch,
193 .Tn UTC ) .
194 The
195 .Fn mktime
196 function
197 interprets the input structure according to the current timezone setting
198 (see
199 .Xr tzset 3 ) .
200 The
201 .Fn timegm
202 function interprets the input structure
203 as representing Universal Coordinated Time
204 .Pq Tn UTC .
205 .Pp
206 The original values of the
207 .Fa tm_wday
208 and
209 .Fa tm_yday
210 components of the structure are ignored. The original values of the
211 other components are not restricted to their normal ranges and will be
212 normalized, if need be.
213 For example,
214 October 40 is changed into November 9,
215 a
216 .Fa tm_hour
217 of \-1 means 1 hour before midnight,
218 .Fa tm_mday
219 of 0 means the day preceding the current month, and
220 .Fa tm_mon
221 of \-2 means 2 months before January of
222 .Fa tm_year .
223 (A positive or zero value for
224 .Fa tm_isdst
225 causes
226 .Fn mktime
227 to presume initially that summer time (for example, Daylight Saving Time)
228 is or is not (respectively) in effect for the specified time.
229 A negative value for
230 .Fa tm_isdst
231 causes the
232 .Fn mktime
233 function to attempt to divine whether summer time is in effect for the
234 specified time.
235 The
236 .Fa tm_isdst
237 and
238 .Fa tm_gmtoff
239 members are forced to zero by
240 .Fn timegm . )
241 .Pp
242 On successful completion, the values of the
243 .Fa tm_wday
244 and
245 .Fa tm_yday
246 components of the structure are set appropriately, and the other components
247 are set to represent the specified calendar time, but with their values
248 forced to their normal ranges; the final value of
249 .Fa tm_mday
250 is not set until
251 .Fa tm_mon
252 and
253 .Fa tm_year
254 are determined.
255 The
256 .Fn mktime
257 function
258 returns the specified calendar time; if the calendar time cannot be
259 represented, it returns \-1;
260 .Pp
261 The
262 .Fn difftime
263 function
264 returns the difference between two calendar times,
265 .Pf ( Fa time1
266 -
267 .Fa time0 ) ,
268 expressed in seconds.
269 .Pp
270 External declarations, as well as the tm structure definition,
271 are contained in the
272 .In time.h
273 include file.
274 The tm structure includes at least the following fields:
275 .Bd -literal -offset indent
276 int tm_sec; /\(** seconds (0 - 60) \(**/
277 int tm_min; /\(** minutes (0 - 59) \(**/
278 int tm_hour; /\(** hours (0 - 23) \(**/
279 int tm_mday; /\(** day of month (1 - 31) \(**/
280 int tm_mon; /\(** month of year (0 - 11) \(**/
281 int tm_year; /\(** year \- 1900 \(**/
282 int tm_wday; /\(** day of week (Sunday = 0) \(**/
283 int tm_yday; /\(** day of year (0 - 365) \(**/
284 int tm_isdst; /\(** is summer time in effect? \(**/
285 char \(**tm_zone; /\(** abbreviation of timezone name \(**/
286 long tm_gmtoff; /\(** offset from UTC in seconds \(**/
287 .Ed
288 .Pp
289 The
290 field
291 .Fa tm_isdst
292 is non-zero if summer (i.e., Daylight Saving) time is in effect.
293 .Pp
294 The field
295 .Fa tm_gmtoff
296 is the offset (in seconds) of the time represented from
297 .Tn UTC ,
298 with positive
299 values indicating locations east of the Prime Meridian.
300 .Sh SEE ALSO
301 .Xr date 1 ,
302 .Xr gettimeofday 2 ,
303 .Xr getenv 3 ,
304 .Xr time 3 ,
305 .Xr tzset 3 ,
306 .Xr tzfile 5
307 .Sh STANDARDS
308 The
309 .Fn asctime ,
310 .Fn ctime ,
311 .Fn difftime ,
312 .Fn gmtime ,
313 .Fn localtime ,
314 and
315 .Fn mktime
316 functions conform to
317 .St -isoC ,
318 and conform to
319 .St -p1003.1-96
320 provided the selected local timezone does not contain a leap-second table
321 (see
322 .Xr zic 8 ) .
323 .Pp
324 The
325 .Fn asctime_r ,
326 .Fn ctime_r ,
327 .Fn gmtime_r ,
328 and
329 .Fn localtime_r
330 functions are expected to conform to
331 .St -p1003.1-96
332 (again provided the selected local timezone does not contain a leap-second
333 table).
334 .Pp
335 The
336 .Fn timegm
337 function is not specified by any standard; its function cannot be
338 completely emulated using the standard functions described above.
339 .Sh HISTORY
340 This manual page is derived from
341 the time package contributed to Berkeley by
342 .An Arthur Olson
343 and which appeared in
344 .Bx 4.3 .
345 .Sh BUGS
346 Except for
347 .Fn difftime ,
348 .Fn mktime ,
349 and the
350 .Fn \&_r
351 variants of the other functions,
352 these functions leaves their result in an internal static object and return
353 a pointer to that object.
354 Subsequent calls to these
355 function will modify the same object.
356 .Pp
357 The C Standard provides no mechanism for a program to modify its current
358 local timezone setting, and the
359 .Tn POSIX Ns No \&-standard
360 method is not reentrant.
361 (However, thread-safe implementations are provided
362 in the
363 .Tn POSIX
364 threaded environment.)
365 .Pp
366 The
367 .Va tm_zone
368 field of a returned
369 .Vt tm
370 structure points to a static array of characters,
371 which will also be overwritten by any subsequent calls (as well as by
372 subsequent calls to
373 .Xr tzset 3
374 and
375 .Xr tzsetwall 3 ) .
376 .Pp
377 Use of the external variable
378 .Fa tzname
379 is discouraged; the
380 .Fa tm_zone
381 entry in the tm structure is preferred.