xref: /netbsd-src/lib/libc/time/ctime.3 (revision c2f76ff004a2cb67efe5b12d97bd3ef7fe89e18d)
1.\" $NetBSD: ctime.3,v 1.38 2011/01/24 19:37:35 njoly Exp $
2.Dd December 14, 2010
3.Dt CTIME 3
4.Os
5.Sh NAME
6.Nm asctime ,
7.Nm asctime_r ,
8.Nm ctime ,
9.Nm ctime_r ,
10.Nm ctime_rz ,
11.Nm difftime ,
12.Nm gmtime ,
13.Nm gmtime_r ,
14.Nm localtime ,
15.Nm localtime_r ,
16.Nm localtime_rz ,
17.Nm mktime ,
18.Nm mktime_z ,
19.Nm tzalloc ,
20.Nm tzgetname ,
21.Nm tzfree ,
22.Nd convert date and time to ASCII
23.Sh LIBRARY
24.Lb libc
25.Sh SYNOPSIS
26.In time.h
27.Vt extern char *tzname[2];
28.Ft char *
29.Fn ctime "const time_t *clock"
30.Ft char *
31.Fn ctime_r "const time_t *clock"  "char *buf"
32.Ft char *
33.Fn ctime_rz "const timezone_t tz" "const time_t *clock"  "char *buf"
34.Ft double
35.Fn difftime "time_t time1" "time_t time0"
36.Ft char *
37.Fn asctime "const struct tm *tm"
38.Ft char *
39.Fn asctime_r "const struct tm restrict tm" "char * restrict buf"
40.Ft struct tm *
41.Fn localtime "const time_t *clock"
42.Ft struct tm *
43.Fn localtime_r "const time_t * restrict clock" "struct tm * restrict result"
44.Ft struct tm *
45.Fn localtime_rz "const timezone_t tz" "const time_t * restrict clock" "struct tm * restrict result"
46.Ft struct tm *
47.Fn gmtime "const time_t *clock"
48.Ft struct tm *
49.Fn gmtime_r "const time_t * restrict clock" "struct tm * restrict result"
50.Ft time_t
51.Fn mktime "struct tm *tm"
52.Ft time_t
53.Fn mktime_z "const timezone_t tz" "struct tm *tm"
54.Ft timezone_t
55.Fn tzalloc "const char *zone"
56.Ft const char *
57.Fn tzgetname "const timezone_t tz" "int isdst"
58.Ft void
59.Fn tzfree "const timezone_t tz"
60.Sh DESCRIPTION
61.Fn ctime
62converts a
63.Vt time_t ,
64pointed to by
65.Fa clock ,
66representing the time in seconds since
6700:00:00 UTC, 1970-01-01,
68and returns a pointer to a
69string of the form
70.D1 Thu Nov 24 18:22:48 1986\en\e0
71Years requiring fewer than four characters are padded with leading zeroes.
72For years longer than four characters, the string is of the form
73.D1 Thu Nov 24 18:22:48     81986\en\e0
74with five spaces before the year.
75These unusual formats are designed to make it less likely that older
76software that expects exactly 26 bytes of output will mistakenly output
77misleading values for out-of-range years.
78.Pp
79.Fn ctime_r
80is similar to
81.Fn ctime ,
82except it places the result of the conversion on the
83.Fa buf
84argument which should be 26 or more bytes long, instead of using a global
85static buffer.
86.Pp
87.Fn ctime_rz
88is similar to
89.Fn ctime_r ,
90but it also takes a
91.Ft "const timezone_t"
92argument, returned by a previous call to
93.Fn tzalloc .
94.Pp
95.Fn localtime
96and
97.Fn gmtime
98return pointers to
99.Va tm
100structures, described below.
101.Fn localtime
102corrects for the time zone and any time zone adjustments
103(such as Daylight Saving Time in the U.S.A.).
104After filling in the
105.Va tm
106structure,
107.Fn localtime
108sets the
109.Fa tm_isdst Ns 'th
110element of
111.Fa tzname
112to a pointer to an
113ASCII string that's the time zone abbreviation to be used with
114.Fn localtime Ns 's
115return value.
116.Pp
117.Fn gmtime
118converts to Coordinated Universal Time.
119.Pp
120The
121.Fn gmtime_r
122and
123.Fn localtime_r
124functions provide the same functionality as
125.Fn gmtime
126and
127.Fn localtime
128differing in that the caller must supply a buffer area
129.Fa result
130in which the result is stored; also,
131.Fn localtime_r
132does not imply initialization of the local time conversion information;
133the application may need to do so by calling
134.Xr tzset 3 .
135.Pp
136.Fn localtime_rz
137is similar to
138.Fn localtime_r ,
139but it also takes a
140.Ft "const timezone_t"
141argument, returned by a previous call to
142.Fn tzalloc .
143.Pp
144.Fn asctime
145converts a time value contained in a
146.Dq tm
147structure to a string,
148as shown in the above example,
149and returns a pointer to the string.
150.Pp
151.Fn mktime
152converts the broken-down time,
153expressed as local time,
154in the structure pointed to by
155.Fa tm
156into a calendar time value with the same encoding as that of the values
157returned by the
158.Xr time 3
159function.
160The original values of the
161.Fa tm_wday
162and
163.Fa tm_yday
164components of the structure are ignored,
165and the original values of the other components are not restricted
166to their normal ranges.
167(A positive or zero value for
168.Fa tm_isdst
169causes
170.Fn mktime
171to presume initially that summer time (for example, Daylight Saving Time
172in the U.S.A.) respectively,
173is or is not in effect for the specified time.
174A negative value for
175.Fa tm_isdst
176causes the
177.Fn mktime
178function to attempt to divine whether summer time is in effect
179for the specified time; in this case it does not use a consistent
180rule and may give a different answer when later
181presented with the same argument.)
182On successful completion, the values of the
183.Fa tm_wday
184and
185.Fa tm_yday
186components of the structure are set appropriately,
187and the other components are set to represent the specified calendar time,
188but with their values forced to their normal ranges; the final value of
189.Fa tm_mday
190is not set until
191.Fa tm_mon
192and
193.Fa tm_year
194are determined.
195.Fn mktime
196returns the specified calendar time; if the calendar time cannot be
197represented, it returns
198.Va "(time_t)-1" .
199.Pp
200.Fn mktime_z
201is similar to
202.Fn mktime
203but it also takes a
204.Ft "const timezone_t"
205argument, returned by a previous call to
206.Fn tzalloc .
207.Pp
208.Fn difftime
209returns the difference between two calendar times,
210.Fa ( time1 No - Fa time0 ) ,
211expressed in seconds.
212.Pp
213.Fn tzalloc
214takes as an argument a timezone name and returns a
215.Ft timezone_t
216object suitable to be used in
217.Fn ctime_rz ,
218.Fn localtime_rz ,
219and
220.Fn mktime_z .
221Instead of setting the environment variable
222.Va TZ ,
223and globally changing the behavior of the calling program, one can use
224multiple timezones at the same time by using separate
225.Ft timezone_t
226objects allocated by
227.Fn tzalloc
228and calling the
229.Dq z
230variants of the functions.
231.Pp
232.Fn tzgetname
233returns the name for the given
234.Fa tz .
235If
236.Fa isdst
237is
238.Va 0 ,
239the call is equivalent to
240.Va tzname[0] .
241If
242.Fa isdst
243is set to
244.Va 1
245the call is equivalent to
246.Va tzname[1] .
247.Pp
248.Fn tzfree
249frees the
250.Fa tz
251argument previously returned by
252.Fa tzalloc .
253.Pp
254The structure (of type)
255.Va "struct tm"
256includes the following fields:
257.Bd -literal -offset indent
258int tm_sec;	/* seconds after the minute [0,61] */
259int tm_min;	/* minutes after the hour [0,59] */
260int tm_hour;	/* hours since midnight [0,23] */
261int tm_mday;	/* day of the month [1,31] */
262int tm_mon;	/* months since January [0,11] */
263int tm_year;	/* years since 1900 */
264int tm_wday;	/* day of week [0,6] (Sunday = 0) */
265int tm_yday;	/* day of year [0,365] (Jan 1 = 0) */
266int tm_isdst;	/* daylight savings flag */
267long tm_gmtoff;	/* offset from UTC in seconds */
268char *tm_zone;	/* abbreviation of timezone name */
269.Ed
270.Pp
271The
272.Fa tm_zone
273and
274.Fa tm_gmtoff
275fields exist, and are filled in, only if arrangements to do
276so were made when the library containing these functions was
277created.
278There is no guarantee that these fields will continue to exist
279in this form in future releases of this code.
280The
281.Fa tm_zone
282field will become invalid and point to freed storage if the corresponding
283.Va "struct tm"
284was returned by
285.Fn localtime_rz
286and the
287.Ft "const timezone_t"
288.Fa tz
289argument has been freed by
290.Fn tzfree .
291.Pp
292.Fa tm_isdst
293is non-zero if summer time is in effect.
294.Pp
295.Fa tm_gmtoff
296is the offset (in seconds) of the time represented
297from UTC, with positive values indicating east
298of the Prime Meridian.
299.Sh RETURN VALUES
300On success the
301.Fn asctime
302and
303.Fn ctime
304functions return a pointer to a static character buffer, and the
305.Fn asctime_r ,
306.Fn ctime_r ,
307and
308.Fn ctime_rz
309function return a pointer to the user-supplied buffer.
310On failure they all return
311.Dv NULL
312and no errors are defined for them.
313On success the
314.Fn gmtime ,
315and
316.Fn localtime
317functions return a pointer to a statically allocated
318.Va "struct tm"
319whereas the
320.Fn gmtime_r ,
321.Fn localtime_r ,
322and
323.Fn localtime_rz ,
324functions return a pointer to the user-supplied
325.Va "struct tm" .
326On failure they all return
327.Dv NULL
328and the global variable
329.Va errno
330is set to indicate the error.
331The
332.Fn mktime
333and
334.Fn mktime_z
335function returns the specified time since the Epoch as a
336.Vt time_t
337type value.
338If the time cannot be represented, then
339.Fn mktime
340and
341.Fn mktime_z
342return
343.Va "(time_t)-1"
344setting the global variable
345.Va errno
346to indicate the error.
347The
348.Fn tzalloc
349function returns a pointer to a
350.Ft timezone_t
351object or
352.Dv NULL
353on failure, setting
354.Va errno
355to indicate the error.
356.Fn tzgetzone
357function returns string containing the name of the timezone given in
358.Fa tz .
359.Sh FILES
360.Bl -tag -width /usr/share/zoneinfo/posixrules -compact
361.It Pa /etc/localtime
362local time zone file
363.It Pa /usr/share/zoneinfo
364time zone information directory
365.It Pa /usr/share/zoneinfo/posixrules
366used with POSIX-style TZ's
367.It Pa /usr/share/zoneinfo/GMT
368for UTC leap seconds
369.El
370.Pp
371If
372.Pa /usr/share/zoneinfo/GMT
373is absent, UTC leap seconds are loaded from
374.Pa /usr/share/zoneinfo/posixrules .
375.Sh ERRORS
376The
377.Fn gmtime_r ,
378.Fn localtime_r ,
379.Fn localtime_rz ,
380.Fn gmtime ,
381.Fn localtime ,
382and
383.Fn mktime ,
384and
385.Fn mktime_z
386will fail when:
387.Bl -tag -width Er
388.It Bq Er EINVAL
389The result cannot be represented.
390.It Bq Er EOVERFLOW
391The result cannot be represented.
392.El
393.Pp
394All functions that return values except their
395.Dq z
396variants, can also return the same errors as
397.Xr open 2
398and
399.Xr malloc 3 .
400.Sh SEE ALSO
401.Xr getenv 3 ,
402.Xr strftime 3 ,
403.Xr time 3 ,
404.Xr tzset 3 ,
405.Xr tzfile 5
406.Sh STANDARDS
407The
408.Fn ctime ,
409.Fn difftime ,
410.Fn asctime ,
411.Fn localtime ,
412.Fn gmtime
413and
414.Fn mktime
415functions conform to
416.St -ansiC
417The
418.Fn ctime_r ,
419.Fn asctime_r ,
420.Fn localtime_r
421and
422.Fn gmtime_r
423functions conform to
424.St -p1003.1c-95 .
425.Sh NOTES
426The return values point to static data; the data is overwritten by
427each call.
428The
429.Fa tm_zone
430field of a returned
431.Va "struct tm"
432points to a static array of characters, which
433will also be overwritten at the next call
434(and by calls to
435.Xr tzset 3 ) .
436.Pp
437.Fn asctime
438and
439.Fn ctime
440behave strangely for years before 1000 or after 9999.
441The 1989 and 1999 editions of the C Standard say
442that years from \-99 through 999 are converted without
443extra spaces, but this conflicts with longstanding
444tradition and with this implementation.
445Traditional implementations of these two functions are
446restricted to years in the range 1900 through 2099.
447To avoid this portability mess, new programs should use
448.Fn strftime
449instead.
450.Pp
451Avoid using out-of-range values with
452.Fn mktime
453when setting up lunch with promptness sticklers in Riyadh.
454.\" @(#)newctime.3	8.3
455.\" This file is in the public domain, so clarified as of
456.\" 2009-05-17 by Arthur David Olson.
457