]> git.saurik.com Git - apple/libc.git/blame - stdtime/ctime.3
Libc-498.1.1.tar.gz
[apple/libc.git] / stdtime / ctime.3
CommitLineData
224c7076
A
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
81The functions
82.Fn ctime ,
83.Fn gmtime ,
84and
85.Fn localtime
86all take as an argument a time value representing the time in seconds since
87the Epoch (00:00:00
88.Tn UTC ,
89January 1, 1970; see
90.Xr time 3 ) .
91.Pp
92The function
93.Fn localtime
94converts the time value pointed at by
95.Fa clock .
96It returns a pointer to a
97.Dq Fa struct tm
98(described below), which contains
99the broken-out time information for the value after adjusting for the current
100time zone (and any other factors such as Daylight Saving Time).
101Time zone adjustments are performed as specified by the
102.Ev TZ
103environment variable (see
104.Xr tzset 3 ) .
105The function
106.Fn localtime
107uses
108.Xr tzset 3
109to initialize time conversion information, if
110.Xr tzset 3
111has not already been called by the process.
112.Pp
113After filling in the tm structure,
114.Fn localtime
115sets the
116.Fa tm_isdst Ns 'th
117element of
118.Fa tzname
119to a pointer to an
120.Tn ASCII
121string containing the time zone abbreviation to be
122used with
123.Fn localtime Ns 's
124return value.
125.Pp
126The function
127.Fn gmtime
128also converts the time value, but makes no time zone adjustment.
129It returns a pointer to a tm structure (described below).
130.Pp
131The
132.Fn ctime
133function
134adjusts the time value for the current time zone, in the same manner as
135.Fn localtime .
136It returns a pointer to a 26-character string of the form:
137.Bd -literal -offset indent
138Thu Nov 24 18:22:48 1986\en\e0
139.Ed
140.Pp
141All of the fields have constant width.
142.Pp
143The
144.Fn ctime_r
145function
146provides the same functionality as
147.Fn ctime ,
148except that the caller must provide the output buffer
149.Fa buf
150(which must be at least 26 characters long) to store the result.
151The
152.Fn localtime_r
153and
154.Fn gmtime_r
155functions
156provide the same functionality as
157.Fn localtime
158and
159.Fn gmtime ,
160respectively, except the caller must provide the output buffer
161.Fa result .
162.Pp
163The
164.Fn asctime
165function
166converts the broken-out time in the structure
167.Fa tm
168(pointed at by
169.Fa *timeptr )
170to the form
171shown in the example above.
172.Pp
173The
174.Fn asctime_r
175function
176provides the same functionality as
177.Fn asctime ,
178except 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
182The functions
183.Fn mktime
184and
185.Fn timegm
186convert the broken-out time
187(in the structure pointed to by
188.Fa *timeptr )
189into a time value with the same encoding as that of the
190values returned by the
191.Xr time 3
192function (that is, seconds from the Epoch,
193.Tn UTC ) .
194The
195.Fn mktime
196function
197interprets the input structure according to the current timezone setting
198(see
199.Xr tzset 3 ) .
200The
201.Fn timegm
202function interprets the input structure
203as representing Universal Coordinated Time
204.Pq Tn UTC .
205.Pp
206The original values of the
207.Fa tm_wday
208and
209.Fa tm_yday
210components of the structure are ignored. The original values of the
211other components are not restricted to their normal ranges and will be
212normalized, if need be.
213For example,
214October 40 is changed into November 9,
215a
216.Fa tm_hour
217of \-1 means 1 hour before midnight,
218.Fa tm_mday
219of 0 means the day preceding the current month, and
220.Fa tm_mon
221of \-2 means 2 months before January of
222.Fa tm_year .
223(A positive or zero value for
224.Fa tm_isdst
225causes
226.Fn mktime
227to presume initially that summer time (for example, Daylight Saving Time)
228is or is not (respectively) in effect for the specified time.
229A negative value for
230.Fa tm_isdst
231causes the
232.Fn mktime
233function to attempt to divine whether summer time is in effect for the
234specified time.
235The
236.Fa tm_isdst
237and
238.Fa tm_gmtoff
239members are forced to zero by
240.Fn timegm . )
241.Pp
242On successful completion, the values of the
243.Fa tm_wday
244and
245.Fa tm_yday
246components of the structure are set appropriately, and the other components
247are set to represent the specified calendar time, but with their values
248forced to their normal ranges; the final value of
249.Fa tm_mday
250is not set until
251.Fa tm_mon
252and
253.Fa tm_year
254are determined.
255The
256.Fn mktime
257function
258returns the specified calendar time; if the calendar time cannot be
259represented, it returns \-1;
260.Pp
261The
262.Fn difftime
263function
264returns the difference between two calendar times,
265.Pf ( Fa time1
266-
267.Fa time0 ) ,
268expressed in seconds.
269.Pp
270External declarations, as well as the tm structure definition,
271are contained in the
272.In time.h
273include file.
274The tm structure includes at least the following fields:
275.Bd -literal -offset indent
276int tm_sec; /\(** seconds (0 - 60) \(**/
277int tm_min; /\(** minutes (0 - 59) \(**/
278int tm_hour; /\(** hours (0 - 23) \(**/
279int tm_mday; /\(** day of month (1 - 31) \(**/
280int tm_mon; /\(** month of year (0 - 11) \(**/
281int tm_year; /\(** year \- 1900 \(**/
282int tm_wday; /\(** day of week (Sunday = 0) \(**/
283int tm_yday; /\(** day of year (0 - 365) \(**/
284int tm_isdst; /\(** is summer time in effect? \(**/
285char \(**tm_zone; /\(** abbreviation of timezone name \(**/
286long tm_gmtoff; /\(** offset from UTC in seconds \(**/
287.Ed
288.Pp
289The
290field
291.Fa tm_isdst
292is non-zero if summer (i.e., Daylight Saving) time is in effect.
293.Pp
294The field
295.Fa tm_gmtoff
296is the offset (in seconds) of the time represented from
297.Tn UTC ,
298with positive
299values 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
308The
309.Fn asctime ,
310.Fn ctime ,
311.Fn difftime ,
312.Fn gmtime ,
313.Fn localtime ,
314and
315.Fn mktime
316functions conform to
317.St -isoC ,
318and conform to
319.St -p1003.1-96
320provided the selected local timezone does not contain a leap-second table
321(see
322.Xr zic 8 ) .
323.Pp
324The
325.Fn asctime_r ,
326.Fn ctime_r ,
327.Fn gmtime_r ,
328and
329.Fn localtime_r
330functions are expected to conform to
331.St -p1003.1-96
332(again provided the selected local timezone does not contain a leap-second
333table).
334.Pp
335The
336.Fn timegm
337function is not specified by any standard; its function cannot be
338completely emulated using the standard functions described above.
339.Sh HISTORY
340This manual page is derived from
341the time package contributed to Berkeley by
342.An Arthur Olson
343and which appeared in
344.Bx 4.3 .
345.Sh BUGS
346Except for
347.Fn difftime ,
348.Fn mktime ,
349and the
350.Fn \&_r
351variants of the other functions,
352these functions leaves their result in an internal static object and return
353a pointer to that object.
354Subsequent calls to these
355function will modify the same object.
356.Pp
357The C Standard provides no mechanism for a program to modify its current
358local timezone setting, and the
359.Tn POSIX Ns No \&-standard
360method is not reentrant.
361(However, thread-safe implementations are provided
362in the
363.Tn POSIX
364threaded environment.)
365.Pp
366The
367.Va tm_zone
368field of a returned
369.Vt tm
370structure points to a static array of characters,
371which will also be overwritten by any subsequent calls (as well as by
372subsequent calls to
373.Xr tzset 3
374and
375.Xr tzsetwall 3 ) .
376.Pp
377Use of the external variable
378.Fa tzname
379is discouraged; the
380.Fa tm_zone
381entry in the tm structure is preferred.