xref: /original-bsd/lib/libc/gen/ctime.3 (revision c3e32dec)
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.\" %sccs.include.redist.man%
7.\"
8.\"     @(#)ctime.3	8.1 (Berkeley) 06/04/93
9.\"
10.Dd
11.Dt CTIME 3
12.Os BSD 4.3
13.Sh NAME
14.Nm asctime ,
15.Nm ctime ,
16.Nm difftime ,
17.Nm gmtime ,
18.Nm localtime ,
19.Nm mktime
20.Nd transform binary date and time value to
21.Tn ASCII
22.Sh SYNOPSIS
23.Fd #include <sys/types.h>
24.Fd #include <time.h>
25.Vt extern char *tzname[2];
26.Ft char *
27.Fn ctime "const time_t *clock"
28.Ft double
29.Fn difftime "time_t time1" "time_t time0"
30.Ft char *
31.Fn asctime "const struct tm *tm"
32.Ft struct tm *
33.Fn localtime "const time_t *clock"
34.Ft struct tm *
35.Fn gmtime "const time_t *clock"
36.Ft time_t
37.Fn mktime "struct tm *tm"
38.Sh DESCRIPTION
39The functions
40.Fn ctime ,
41.Fn gmtime
42and
43.Fn localtime
44all take as an argument a time value representing the time in seconds since
45the Epoch (00:00:00
46.Tn UTC ,
47January 1, 1970; see
48.Xr time 3 ) .
49.Pp
50The function
51.Fn localtime
52converts the time value pointed at by
53.Fa clock ,
54and returns a pointer to a
55.Dq Fa struct tm
56(described below) which contains
57the broken-out time information for the value after adjusting for the current
58time zone (and any other factors such as Daylight Saving Time).
59Time zone adjustments are performed as specified by the
60.Ev TZ
61environmental variable (see
62.Xr tzset 3 ) .
63The function
64.Fn localtime
65uses
66.Xr tzset
67to initialize time conversion information if
68.Xr tzset
69has not already been called by the process.
70.Pp
71After filling in the tm structure,
72.Fn localtime
73sets the
74.Fa tm_isdst Ns 'th
75element of
76.Fa tzname
77to a pointer to an
78.Tn ASCII
79string that's the time zone abbreviation to be
80used with
81.Fn localtime Ns 's
82return value.
83.Pp
84The function
85.Fn gmtime
86similarly converts the time value, but without any time zone adjustment,
87and returns a pointer to a tm structure (described below).
88.Pp
89The
90.Fn ctime
91function
92adjusts the time value for the current time zone in the same manner as
93.Fn localtime ,
94and returns a pointer to a 26-character string of the form:
95.Bd -literal -offset indent
96Thu Nov 24 18:22:48 1986\en\e0
97.Ed
98.Pp
99All the fields have constant width.
100.Pp
101The
102.Fn asctime
103function
104converts the broken down time in the structure
105.Fa tm
106pointed at by
107.Fa *tm
108to the form
109shown in the example above.
110.Pp
111The function
112.Fn mktime
113converts the broken-down time, expressed as local time, in the structure
114pointed to by tm into a time value with the same encoding as that of the
115values returned by the
116.Xr time 3
117function, that is, seconds from the Epoch,
118.Tn UTC .
119.Pp
120The original values of the
121.Fa tm_wday
122and
123.Fa tm_yday
124components of the structure are ignored, and the original values of the
125other components are not restricted to their normal ranges.
126(A positive or zero value for
127.Fa tm_isdst
128causes
129.Fn mktime
130to presume initially that summer time (for example, Daylight Saving Time)
131is or is not in effect for the specified time, respectively.
132A negative value for
133.Fa tm_isdst
134causes the
135.Fn mktime
136function to attempt to divine whether summer time is in effect for the
137specified time.)
138.Pp
139On successful completion, the values of the
140.Fa tm_wday
141and
142.Fa tm_yday
143components of the structure are set appropriately, and the other components
144are set to represent the specified calendar time, but with their values
145forced to their normal ranges; the final value of
146.Fa tm_mday
147is not set until
148.Fa tm_mon
149and
150.Fa tm_year
151are determined.
152.Fn Mktime
153returns the specified calendar time; if the calendar time cannot be
154represented, it returns \-1;
155.Pp
156The
157.Fn difftime
158function
159returns the difference between two calendar times,
160.Pf ( Fa time1
161-
162.Fa time0 ) ,
163expressed in seconds.
164.Pp
165External declarations as well as the tm structure definition are in the
166.Aq Pa time.h
167include file.
168The tm structure includes at least the following fields:
169.Bd -literal -offset indent
170int tm_sec;	/\(** seconds (0 - 60) \(**/
171int tm_min;	/\(** minutes (0 - 59) \(**/
172int tm_hour;	/\(** hours (0 - 23) \(**/
173int tm_mday;	/\(** day of month (1 - 31) \(**/
174int tm_mon;	/\(** month of year (0 - 11) \(**/
175int tm_year;	/\(** year \- 1900 \(**/
176int tm_wday;	/\(** day of week (Sunday = 0) \(**/
177int tm_yday;	/\(** day of year (0 - 365) \(**/
178int tm_isdst;	/\(** is summer time in effect? \(**/
179char \(**tm_zone;	/\(** abbreviation of timezone name \(**/
180long tm_gmtoff;	/\(** offset from UTC in seconds \(**/
181.Ed
182.Pp
183The
184field
185.Fa tm_isdst
186is non-zero if summer time is in effect.
187.Pp
188The field
189.Fa tm_gmtoff
190is the offset (in seconds) of the time represented from
191.Tn UTC ,
192with positive
193values indicating east of the Prime Meridian.
194.Sh SEE ALSO
195.Xr date 1 ,
196.Xr gettimeofday 2 ,
197.Xr getenv 3 ,
198.Xr time 3 ,
199.Xr tzset 3 ,
200.Xr tzfile 5
201.Sh HISTORY
202This manual page is derived from
203the time package contributed to Berkeley by
204Arthur Olsen and which appeared in
205.Bx 4.3 .
206.Sh BUGS
207Except for
208.Fn difftime
209and
210.Fn mktime ,
211these functions leaves their result in an internal static object and return
212a pointer to that object. Subsequent calls to these
213function will modify the same object.
214.Pp
215The
216.Fa tm_zone
217field of a returned tm structure points to a static array of characters,
218which will also be overwritten by any subsequent calls (as well as by
219subsequent calls to
220.Xr tzset 3
221and
222.Xr tzsetwall 3 ) .
223.Pp
224Use of the external variable
225.Fa tzname
226is discouraged; the
227.Fa tm_zone
228entry in the tm structure is preferred.
229.Pp
230Avoid using out-of-range values with
231.Fn mktime
232when setting up lunch with promptness sticklers in Riyadh.
233