changes from Arthur Olson -- if TZ wrong, just use UTC
[unix-history] / usr / src / lib / libc / gen / ctime.3
CommitLineData
637a994b
KB
1.\" Copyright (c) 1989 The Regents of the University of California.
2.\" All rights reserved.
463d6749 3.\"
637a994b
KB
4.\" This code is derived from software contributed to Berkeley by
5.\" Arthur Olson.
463d6749 6.\"
637a994b
KB
7.\" Redistribution and use in source and binary forms are permitted
8.\" provided that the above copyright notice and this paragraph are
9.\" duplicated in all such forms and that any documentation,
10.\" advertising materials, and other materials related to such
11.\" distribution and use acknowledge that the software was developed
12.\" by the University of California, Berkeley. The name of the
13.\" University may not be used to endorse or promote products derived
14.\" from this software without specific prior written permission.
15.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16.\" IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18.\"
de716a99 19.\" @(#)ctime.3 6.11 (Berkeley) %G%
637a994b
KB
20.\"
21.TH CTIME 3 ""
463d6749 22.SH NAME
637a994b 23asctime, ctime, difftime, gmtime, localtime, mktime, tzset, tzsetwall \- convert date and time to ASCII
463d6749
KM
24.SH SYNOPSIS
25.nf
637a994b
KB
26.B extern char *tzname[2];
27.PP
94c2eda1
KB
28.B void tzset()
29.PP
637a994b
KB
30.B void tzsetwall()
31.PP
32.B #include <sys/types.h>
33.PP
463d6749 34.B char *ctime(clock)
94c2eda1 35.B time_t *clock;
463d6749 36.PP
637a994b
KB
37.B double difftime(time1, time0)
38.B time_t time1;
39.B time_t time0;
40.PP
2ad48aae 41.B #include <time.h>
463d6749 42.PP
94c2eda1
KB
43.B char *asctime(tm)
44.B struct tm *tm;
45.PP
463d6749 46.B struct tm *localtime(clock)
637a994b 47.B long *clock;
463d6749
KM
48.PP
49.B struct tm *gmtime(clock)
637a994b 50.B long *clock;
463d6749 51.PP
637a994b
KB
52.B time_t mktime(tm)
53.B struct tm *tm;
94c2eda1 54.fi
463d6749 55.SH DESCRIPTION
637a994b
KB
56.I Tzset
57uses the value of the environment variable
58.B TZ
59to set time conversion information used by
60.IR localtime .
61If
62.B TZ
63does not appear in the environment,
64the best available approximation to local wall clock time, as specified
65by the
66.IR tzfile (5)-format
67file
68.B localtime
69in the system time conversion information directory, is used by
70.IR localtime .
71If
72.B TZ
73appears in the environment but its value is a null string,
74Coordinated Universal Time (UTC) is used (without leap second
75correction). If
76.B TZ
77appears in the environment and its value is not a null string:
78.IP
79if the value begins with a colon, it is used as a pathname of a file
80from which to read the time conversion information;
81.IP
82if the value does not begin with a colon, it is first used as the
83pathname of a file from which to read the time conversion information,
84and, if that file cannot be read, is used directly as a specification of
85the time conversion information.
86.PP
87When
88.B TZ
89is used as a pathname, if it begins with a slash,
90it is used as an absolute pathname; otherwise,
91it is used as a pathname relative to a system time conversion information
92directory.
93The file must be in the format specified in
94.IR tzfile (5).
95.PP
96When
97.B TZ
98is used directly as a specification of the time conversion information,
99it must have the following syntax (spaces inserted for clarity):
100.IP
101\fIstd\|offset\fR[\fIdst\fR[\fIoffset\fR][\fB,\fIrule\fR]]
102.PP
103Where:
104.RS
105.TP 15
106.IR std " and " dst
107Three or more bytes that are the designation for the standard
108.RI ( std )
109or summer
110.RI ( dst )
111time zone. Only
112.I std
113is required; if
114.I dst
115is missing, then summer time does not apply in this locale.
116Upper- and lowercase letters are explicitly allowed. Any characters
117except a leading colon
118.RB ( : ),
119digits, comma
120.RB ( , ),
121minus
122.RB ( \(mi ),
123plus
124.RB ( \(pl ),
125and ASCII NUL are allowed.
126.TP
127.I offset
128Indicates the value one must add to the local time to arrive at
129Coordinated Universal Time. The
130.I offset
131has the form:
132.RS
133.IP
134\fIhh\fR[\fB:\fImm\fR[\fB:\fIss\fR]]
135.RE
136.IP
137The minutes
138.RI ( mm )
139and seconds
140.RI ( ss )
141are optional. The hour
142.RI ( hh )
143is required and may be a single digit. The
144.I offset
145following
146.I std
147is required. If no
148.I offset
149follows
150.IR dst ,
151summer time is assumed to be one hour ahead of standard time. One or
152more digits may be used; the value is always interpreted as a decimal
153number. The hour must be between zero and 24, and the minutes (and
154seconds) \(em if present \(em between zero and 59. If preceded by a
155.RB `` \(mi '',
156the time zone shall be east of the Prime Meridian; otherwise it shall be
157west (which may be indicated by an optional preceding
158.RB `` \(pl '').
159.TP
160.I rule
161Indicates when to change to and back from summer time. The
162.I rule
163has the form:
164.RS
165.IP
166\fIdate\fB/\fItime\fB,\fIdate\fB/\fItime\fR
167.RE
168.IP
169where the first
170.I date
171describes when the change from standard to summer time occurs and the
172second
173.I date
174describes when the change back happens. Each
175.I time
176field describes when, in current local time, the change to the other
177time is made.
178.IP
179The format of
180.I date
181is one of the following:
182.RS
183.TP 10
184.BI J n
185The Julian day
186.I n
187.RI "(1\ \(<=" "\ n\ " "\(<=\ 365).
188Leap days are not counted; that is, in all years \(em including leap
189years \(em February 28 is day 59 and March 1 is day 60. It is
190impossible to explicitly refer to the occasional February 29.
191.TP
192.I n
193The zero-based Julian day
194.RI "(0\ \(<=" "\ n\ " "\(<=\ 365).
195Leap days are counted, and it is possible to refer to February 29.
196.TP
197.BI M m . n . d
198The
199.IR d' th
200day
201.RI "(0\ \(<=" "\ d\ " "\(<=\ 6)
202of week
203.I n
204of month
205.I m
206of the year
207.RI "(1\ \(<=" "\ n\ " "\(<=\ 5,
208.RI "1\ \(<=" "\ m\ " "\(<=\ 12,
209where week 5 means ``the last
210.I d
211day in month
212.IR m ''
213which may occur in either the fourth or the fifth week). Week 1 is the
214first week in which the
215.IR d' th
216day occurs. Day zero is Sunday.
217.RE
218.IP "" 15
219The
220.I time
221has the same format as
222.I offset
223except that no leading sign
224.RB (`` \(mi ''
225or
226.RB `` \(pl '')
227is allowed. The default, if
228.I time
229is not given, is
230.BR 02:00:00 .
231.RE
232.LP
233If no
234.I rule
235is present in
236.BR TZ ,
237the rules specified
238by the
239.IR tzfile (5)-format
240file
241.B posixrules
242in the system time conversion information directory are used, with the
243standard and summer time offsets from UTC replaced by those specified by
244the
245.I offset
246values in
247.BR TZ .
248.PP
249For compatibility with System V Release 3.1, a semicolon
250.RB ( ; )
251may be used to separate the
252.I rule
253from the rest of the specification.
254.PP
255If the
256.B TZ
257environment variable does not specify a
258.IR tzfile (5)-format
259and cannot be interpreted as a direct specification,
de716a99 260UTC is used.
637a994b
KB
261.PP
262.I Tzsetwall
263sets things up so that
264.I localtime
265returns the best available approximation of local wall clock time.
266.PP
267.I Ctime\^
268converts a long integer, pointed to by
269.IR clock ,
270representing the time in seconds since
27100:00:00 UTC, January 1, 1970,
272and returns a pointer to a
27326-character string
274of the form
275.br
276.ce
277.eo
278Thu Nov 24 18:22:48 1986\n\0
279.ec
280.br
281All the fields have constant width.
282.PP
283.IR Localtime\^
463d6749 284and
637a994b
KB
285.I gmtime\^
286return pointers to ``tm'' structures, described below.
287.I Localtime\^
288corrects for the time zone and any time zone adjustments
289(such as Daylight Saving Time in the U.S.A.).
290Before doing so,
291.I localtime\^
292calls
293.I tzset\^
294(if
295.I tzset\^
296has not been called in the current process).
297After filling in the ``tm'' structure,
298.I localtime
299sets the
300.BR tm_isdst 'th
301element of
302.B tzname
303to a pointer to an
304ASCII string that's the time zone abbreviation to be used with
305.IR localtime 's
306return value.
307.PP
308.I Gmtime\^
309converts to Coordinated Universal Time.
310.PP
311.I Asctime\^
312converts a time value contained in a
313``tm'' structure to a 26-character string,
314as shown in the above example,
315and returns a pointer
316to the string.
463d6749 317.PP
637a994b
KB
318.I Mktime\^
319converts the broken-down time,
320expressed as local time,
321in the structure pointed to by
322.I tm
323into a calendar time value with the same encoding as that of the values
324returned by the
325.I time
326function.
327The original values of the
328.B tm_wday
329and
330.B tm_yday
331components of the structure are ignored,
332and the original values of the other components are not restricted
333to their normal ranges.
334(A positive or zero value for
335.B tm_isdst
336causes
337.I mktime
338to presume initially that summer time (for example, Daylight Saving Time
339in the U.S.A.)
340respectively,
341is or is not in effect for the specified time.
342A negative value for
343.B tm_isdst
344causes the
345.I mktime
346function to attempt to divine whether summer time is in effect
347for the specified time.)
348On successful completion, the values of the
349.B tm_wday
350and
351.B tm_yday
352components of the structure are set appropriately,
353and the other components are set to represent the specified calendar time,
354but with their values forced to their normal ranges; the final value of
355.B tm_mday
356is not set until
357.B tm_mon
358and
359.B tm_year
360are determined.
361.I Mktime\^
362returns the specified calendar time;
363If the calendar time cannot be represented,
364it returns
365.BR -1 .
366.PP
367.I Difftime\^
368returns the difference between two calendar times,
369.I time1
370-
371.IR time0,
372expressed in seconds.
373.PP
374Declarations of all the functions and externals, and the ``tm'' structure,
375are in the
376.B <time.h>\^
377header file.
378The structure (of type)
379.B struct tm
380includes the following fields:
8eafd288 381.RS
637a994b 382.PP
463d6749 383.nf
637a994b
KB
384.ta .5i +\w'long tm_gmtoff;\0\0'u
385 int tm_sec; /\(** seconds (0 - 60) \(**/
386 int tm_min; /\(** minutes (0 - 59) \(**/
387 int tm_hour; /\(** hours (0 - 23) \(**/
388 int tm_mday; /\(** day of month (1 - 31) \(**/
389 int tm_mon; /\(** month of year (0 - 11) \(**/
390 int tm_year; /\(** year \- 1900 \(**/
391 int tm_wday; /\(** day of week (Sunday = 0) \(**/
392 int tm_yday; /\(** day of year (0 - 365) \(**/
393 int tm_isdst; /\(** is summer time in effect? \(**/
394 char \(**tm_zone; /\(** abbreviation of timezone name \(**/
395 long tm_gmtoff; /\(** offset from UTC in seconds \(**/
463d6749
KM
396.fi
397.RE
398.PP
637a994b
KB
399The
400.I tm_zone
401and
402.I tm_gmtoff
403fields exist, and are filled in, only if arrangements to do
404so were made when the library containing these functions was
405created.
406There is no guarantee that these fields will continue to exist
407in this form in future releases of this code.
408.PP
409.I Tm_isdst\^
410is non-zero if summer time is in effect.
411.PP
412.I Tm_gmtoff
413is the offset (in seconds) of the time represented
414from UTC, with positive values indicating east
415of the Prime Meridian.
94c2eda1 416.SH FILES
637a994b 417.ta \w'/etc/zoneinfo/posixrules\0\0'u
94c2eda1
KB
418/etc/zoneinfo time zone information directory
419.br
420/etc/zoneinfo/localtime local time zone file
637a994b
KB
421.br
422/etc/zoneinfo/posixrules used in converting POSIX-style TZ's
423.br
424/etc/zoneinfo/GMT for UTC leap seconds
425.sp
426If
427.I /etc/zoneinfo/GMT
428is absent,
429UTC leap seconds are loaded from
430.IR /etc/zoneinfo/posixrules .
94c2eda1 431.SH SEE ALSO
637a994b
KB
432time(2), getenv(3), tzfile(5)
433.SH NOTES
434The return values point to static data
435whose content is overwritten by each call.
436The
437.B tm_zone
438field of a returned
439.B "struct tm"
440points to a static array of characters, which
441will also be overwritten at the next call
442(and by calls to
443.I tzset
444or
445.IR tzsetwall ).
446.PP
447Avoid using out-of-range values with
448.I mktime
449when setting up lunch with promptness sticklers in Riyadh.