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