8498 ficl: variable 'count' might be clobbered by 'longjmp' or 'vfork'
[unleashed.git] / share / man / man3c / ctime.3c
blobaa61217296bc05bb763951fb313dfdd935ea6973
1 '\" te
2 .\" Copyright (c) 2005, Sun Microsystems, Inc.  All Rights Reserved.
3 .\" Copyright 1989 AT&T
4 .\" Portions Copyright (c) 2001, the Institute of Electrical and Electronics Engineers, Inc. and The Open Group. All Rights Reserved.
5 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at
6 .\" http://www.opengroup.org/bookstore/.
7 .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
8 .\"  This notice shall appear on any product containing this material.
9 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
10 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
11 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
12 .TH CTIME 3C "May 27, 2005"
13 .SH NAME
14 ctime, ctime_r, localtime, localtime_r, gmtime, gmtime_r, asctime, asctime_r,
15 tzset \- convert date and time to string
16 .SH SYNOPSIS
17 .LP
18 .nf
19 #include <time.h>
21 \fBchar *\fR\fBctime\fR(\fBconst time_t *\fR\fIclock\fR);
22 .fi
24 .LP
25 .nf
26 \fBstruct tm *\fR\fBlocaltime\fR(\fBconst time_t *\fR\fIclock\fR);
27 .fi
29 .LP
30 .nf
31 \fBstruct tm *\fR\fBgmtime\fR(\fBconst time_t *\fR\fIclock\fR);
32 .fi
34 .LP
35 .nf
36 \fBchar *\fR\fBasctime\fR(\fBconst struct tm *\fR\fItm\fR);
37 .fi
39 .LP
40 .nf
41 extern time_t timezone, altzone;
42 extern int daylight;
43 extern char *tzname[2];
45 \fBvoid\fR \fBtzset\fR(\fBvoid\fR);
46 .fi
48 .LP
49 .nf
50 \fBchar *\fR\fBctime_r\fR(\fBconst time_t *\fR\fIclock\fR, \fBchar *\fR\fIbuf\fR);
51 .fi
54 .LP
55 .nf
56 \fBstruct tm *\fR\fBlocaltime_r\fR(\fBconst time_t *restrict\fR \fIclock\fR,
57      \fBstruct tm *restrict\fR \fIres\fR);
58 .fi
60 .LP
61 .nf
62 \fBstruct tm *\fR\fBgmtime_r\fR(\fBconst time_t *restrict\fR \fIclock\fR,
63      \fBstruct tm *restrict\fR \fIres\fR);
64 .fi
66 .LP
67 .nf
68 \fBchar *\fR\fBasctime_r\fR(\fBconst struct tm *\fR\fItm\fR, \fBchar *\fR\fIbuf\fR);
69 .fi
71 .SH DESCRIPTION
72 .sp
73 .LP
74 The \fBctime()\fR function converts the time pointed to by \fIclock\fR,
75 representing the time in seconds since the Epoch (00:00:00 UTC, January 1,
76 1970), to local time in the form of a 26-character string, as shown below. Time
77 zone and daylight savings corrections are made before string generation. The
78 fields are in constant width:
79 .sp
80 .LP
81 Fri Sep 13 00:00:00 1986\en\e0
82 .sp
83 .LP
84 The \fBctime()\fR function is equivalent to:
85 .sp
86 .LP
87 \fBasctime(localtime(\fR\fIclock\fR\fB\fR\fB))\fR
88 .sp
89 .LP
90 The \fBctime()\fR, \fBasctime()\fR, \fBgmtime()\fR, and \fBlocaltime()\fR
91 functions return values in one of two thread-specific data objects: a
92 broken-down time structure and an array of \fBchar\fR. Execution of any of the
93 functions can overwrite the information returned in either of these objects by
94 any of the other functions executed by the same thread.
95 .sp
96 .LP
97 The \fBctime_r()\fR function has the same functionality as \fBctime()\fR except
98 that the caller must supply a buffer \fIbuf\fR to store the result;
99 \fIbuf\fR must be at least 26 bytes.
102 The \fBlocaltime()\fR and \fBgmtime()\fR functions return pointers to \fBtm\fR
103 structures (see below). The \fBlocaltime()\fR function corrects for the main
104 time zone and possible alternate ("daylight savings") time zone; the
105 \fBgmtime()\fR function converts directly to Coordinated Universal Time (UTC),
106 which is what the UNIX system uses internally.
109 The \fBlocaltime_r()\fR and \fBgmtime_r()\fR functions have the same
110 functionality as \fBlocaltime()\fR and \fBgmtime()\fR respectively, except that
111 the caller must supply a buffer \fIres\fR to store the result.
114 The \fBasctime()\fR function converts a \fBtm\fR structure to a 26-character
115 string, as shown in the previous example, and returns a pointer to the string.
118 The \fBasctime_r()\fR function has the same functionality as \fBasctime()\fR
119 except that the caller must supply a buffer \fIbuf\fR for the result to be
120 stored.  The \fIbuf\fR argument must be at least 26 bytes.  The
121 \fBasctime_r()\fR function returns a pointer to \fIbuf\fR upon success.  In
122 case of failure, \fINULL\fR is returned and \fBerrno\fR is set.
125 Declarations of all the functions and externals, and the \fBtm\fR structure,
126 are in the <\fBtime.h\fR> header. The members of the \fBtm\fR structure are:
128 .in +2
130 int   tm_sec;    /* seconds after the minute \(em [0, 60] */
131                  /* for leap seconds */
132 int   tm_min;    /* minutes after the hour \(em [0, 59] */
133 int   tm_hour;   /* hour since midnight \(em [0, 23] */
134 int   tm_mday;   /* day of the month \(em [1, 31] */
135 int   tm_mon;    /* months since January \(em [0, 11] */
136 int   tm_year;   /* years since 1900 */
137 int   tm_wday;   /* days since Sunday \(em [0, 6] */
138 int   tm_yday;   /* days since January 1 \(em [0, 365] */
139 int   tm_isdst;  /* flag for alternate daylight savings time */
141 .in -2
145 The value of \fBtm_isdst\fR is positive if daylight savings time is in effect,
146 zero if daylight savings time is not in effect, and negative if the information
147 is not available. Previously, the value of \fBtm_isdst\fR was defined as
148 non-zero if daylight savings was in effect.
151 The external \fBtime_t\fR variable \fBaltzone\fR contains the difference, in
152 seconds, between Coordinated Universal Time and the alternate time zone. The
153 external variable \fBtimezone\fR contains the difference, in seconds, between
154 UTC and local standard time. The external variable \fBdaylight\fR indicates
155 whether time should reflect daylight savings time. Both \fBtimezone\fR and
156 \fBaltzone\fR default to 0 (UTC). The external variable \fBdaylight\fR is
157 non-zero if an alternate time zone exists. The time zone names are contained in
158 the external variable \fBtzname\fR, which by default is set to:
161 char *tzname[2] = { "GMT", "\|" };
164 These functions know about the peculiarities of this conversion for various
165 time periods for the \fBU.S.\fR (specifically, the years 1974, 1975, and 1987).
166 They start handling the new daylight savings time starting with the first
167 Sunday in April, 1987.
170 The \fBtzset()\fR function uses the contents of the environment variable
171 \fBTZ\fR to override the value of the different external variables. It is
172 called by \fBasctime()\fR and can also be called by the user. If \fBTZ\fR is
173 not specified or has an invalid setting, \fBtzset()\fR uses \fBGMT0\fR. See
174 \fBenviron\fR(5) for a description of the \fBTZ\fR environment variable.
177 Starting and ending times are relative to the current local time zone. If the
178 alternate time zone start and end dates and the time are not provided, the days
179 for the United States that year will be used and the time will be 2 AM. If the
180 start and end dates are provided but the time is not provided, the time will be
181 2 AM. The effects of \fBtzset()\fR change the values of the external variables
182 \fBtimezone\fR, \fBaltzone\fR, \fBdaylight\fR, and \fBtzname\fR.
185 Note that in most installations, \fBTZ\fR is set to the correct value by
186 default when the user logs on, using the local \fB/etc/default/init\fR file
187 (see \fBTIMEZONE\fR(4)).
188 .SH RETURN VALUES
191 Upon successful completion, the \fBgmtime()\fR and \fBlocaltime()\fR functions
192 return a pointer to a \fBstruct tm\fR. If an error is detected, \fBgmtime()\fR
193 and \fBlocaltime()\fR return a null pointer.
196 Upon successful completion, the \fBgmtime_r()\fR and \fBlocaltime_r()\fR
197 functions return the address of the structure pointed to by the \fIres\fR
198 argument. If an error is detected, \fBgmtime_r()\fR and \fBlocaltime_r()\fR
199 return a null pointer and set \fBerrno\fR to indicate the error.
200 .SH ERRORS
204 The \fBgmtime()\fR, \fBgmtime_r()\fR, \fBlocaltime()\fR, and
205 \fBlocaltime_r()\fR functions will fail if:
207 .ne 2
209 \fB\fBEOVERFLOW\fR\fR
211 .RS 13n
212 The result cannot be represented.
215 .SH USAGE
218 These functions do not support localized date and time formats. The
219 \fBstrftime\fR(3C) function can be used when localization is required.
222 The \fBlocaltime()\fR, \fBlocaltime_r()\fR, \fBgmtime()\fR, \fBgmtime_r()\fR,
223 \fBctime()\fR, and \fBctime_r()\fR functions assume Gregorian dates. Times
224 before the adoption of the Gregorian calendar will not match historial records.
225 .SH EXAMPLES
227 \fBExample 1 \fRExamples of the \fBtzset()\fR function.
230 The \fBtzset()\fR function scans the contents of the environment variable and
231 assigns the different fields to the respective variable. For example, the most
232 complete setting for New Jersey in 1986 could be:
235 .in +2
237 EST5EDT4,116/2:00:00,298/2:00:00
239 .in -2
243 or simply
246 .in +2
248 EST5EDT
250 .in -2
254 An example of a southern hemisphere setting such as the Cook Islands could be
257 .in +2
259 KDT9:30KST10:00,63/5:00,302/20:00
261 .in -2
265 In the longer version of the New Jersey example of \fBTZ,\fR
266 \fBtzname\fR[\fB0\fR] is EST, \fBtimezone\fR is set to 5*60*60,
267 \fBtzname\fR[\fB1\fR] is EDT, \fBaltzone\fR is set to 4*60*60, the starting
268 date of the alternate time zone is the 117th day at 2 AM, the ending date of
269 the alternate time zone is the 299th day at 2 AM (using zero-based Julian
270 days), and \fBdaylight\fR is set positive. Starting and ending times are
271 relative to the current local time zone. If the alternate time zone start and
272 end dates and the time are not provided, the days for the United States that
273 year will be used and the time will be 2 AM. If the start and end dates are
274 provided but the time is not provided, the time will be 2 AM. The effects of
275 \fBtzset()\fR are thus to change the values of the external variables
276 \fBtimezone\fR, \fBaltzone\fR, \fBdaylight\fR, and \fBtzname\fR. The
277 \fBctime()\fR, \fBlocaltime()\fR, \fBmktime()\fR, and \fBstrftime()\fR
278 functions also update these external variables as if they had called
279 \fBtzset()\fR at the time specified by the \fBtime_t\fR or \fBstruct tm\fR
280 value that they are converting.
282 .SH BUGS
285 The \fBzoneinfo\fR timezone data files do not transition past Tue Jan 19
286 03:14:07 2038 UTC.  Therefore for 64-bit applications using \fBzoneinfo\fR
287 timezones, calculations beyond this date might not use the correct offset from
288 standard time, and could return incorrect values. This affects the 64-bit
289 version of \fBlocaltime()\fR, \fBlocaltime_r()\fR, \fBctime()\fR, and
290 \fBctime_r()\fR.
291 .SH ATTRIBUTES
294 See \fBattributes\fR(5) for descriptions of the following attributes:
299 box;
300 c | c
301 l | l .
302 ATTRIBUTE TYPE  ATTRIBUTE VALUE
304 CSI     Enabled
306 Interface Stability     Standard
308 MT-Level        MT-Safe with exceptions
313 The \fBasctime()\fR, \fBctime()\fR, \fBgmtime()\fR, and \fBlocaltime()\fR
314 functions are safe to use in multithread applications because they employ
315 thread-specific data. However, their use is discouraged because standards do
316 not require them to be thread-safe. The \fBasctime_r()\fR and \fBgmtime_r()\fR
317 functions are MT-Safe. The \fBctime_r()\fR, \fBlocaltime_r()\fR, and
318 \fBtzset()\fR functions are MT-Safe in multithread applications, as long as no
319 user-defined function directly modifies one of the following variables:
320 \fBtimezone\fR, \fBaltzone\fR, \fBdaylight\fR, and \fBtzname\fR. These four
321 variables are not MT-Safe to access. They are modified by the \fBtzset()\fR
322 function in an MT-Safe manner. The \fBmktime()\fR, \fBlocaltime_r()\fR, and
323 \fBctime_r()\fR functions call \fBtzset()\fR.
324 .SH SEE ALSO
327 \fBtime\fR(2), \fBIntro\fR(3), \fBgetenv\fR(3C), \fBmktime\fR(3C),
328 \fBprintf\fR(3C), \fBputenv\fR(3C), \fBsetlocale\fR(3C), \fBstrftime\fR(3C),
329 \fBTIMEZONE\fR(4), \fBattributes\fR(5), \fBenviron\fR(5), \fBstandards\fR(5)
330 .SH NOTES
333 When compiling multithreaded programs, see \fBIntro\fR(3).
336 The return values for \fBasctime()\fR, \fBctime()\fR, \fBgmtime()\fR, and
337 \fBlocaltime()\fR point to thread-specific data whose content is overwritten by
338 each call by the same thread.
341 Setting the time during the interval of change from \fBtimezone\fR to
342 \fBaltzone\fR or vice versa can produce unpredictable results. The system
343 administrator must change the Julian start and end days annually.
346 If \fBtzset()\fR has previously evaluated the timezone identified by the value
347 of the \fBTZ\fR  environment variable, \fBtzset()\fR can reuse the previous
348 settings of the external variables \fBaltzone\fR, \fBdaylight\fR,
349 \fBtimezone\fR, and \fBtzname\fR[] associated with that timezone.
352 In Solaris 10, \fBgmtime()\fR, \fBgmtime_r()\fR, \fBlocaltime()\fR, and
353 \fBlocaltime_r()\fR were updated to return a null pointer if an error is
354 detected. This change was based on the SUSv3 specification. See
355 \fBstandards\fR(5).