1 .\" $NetBSD: ctime.3,v 1.46 2011/11/02 23:06:08 christos Exp $
3 .\" XXX: License missing?
25 .Nd convert date and time to ASCII
30 .Vt extern char *tzname[2];
32 .Fn asctime "const struct tm *tm"
34 .Fn asctime_r "const struct tm restrict tm" "char * restrict buf"
36 .Fn ctime "const time_t *clock"
38 .Fn ctime_r "const time_t *clock" "char *buf"
40 .Fn ctime_rz "const timezone_t tz" "const time_t *clock" "char *buf"
42 .Fn difftime "time_t time1" "time_t time0"
44 .Fn gmtime "const time_t *clock"
46 .Fn gmtime_r "const time_t * restrict clock" "struct tm * restrict result"
48 .Fn localtime "const time_t *clock"
50 .Fn localtime_r "const time_t * restrict clock" "struct tm * restrict result"
52 .Fn localtime_rz "const timezone_t tz" "const time_t * restrict clock" "struct tm * restrict result"
54 .Fn mktime "struct tm *tm"
56 .Fn mktime_z "const timezone_t tz" "struct tm *tm"
58 .Fn tzalloc "const char *zone"
60 .Fn tzfree "const timezone_t tz"
62 .Fn tzgetname "const timezone_t tz" "int isdst"
66 family of functions provide various standard library routines
67 to operate with time and conversions related to time.
73 function converts a time value contained in the
75 structure to a string with the following general format:
76 .Bd -literal -offset indent
77 .D1 Thu Nov 24 18:22:48 1986\en\e0
82 structure is described in
84 .It Fn asctime_r "tm" "buf"
87 has the same behavior as
89 but the result is stored to
91 which should have a size of at least 26 bytes.
99 representing the time in seconds since 00:00:00 UTC, 1970-01-01,
100 and returns a pointer to a string with the format described above.
101 Years requiring fewer than four characters are padded with leading zeroes.
102 For years longer than four characters, the string is of the form
103 .Bd -literal -offset indent
104 .D1 "Thu Nov 24 18:22:48 81986\en\e0"
107 with five spaces before the year.
108 These unusual formats are designed to make it less likely that older
109 software that expects exactly 26 bytes of output will mistakenly output
110 misleading values for out-of-range years.
111 .It Fn ctime_r "clock" "buf"
116 except it places the result of the conversion on the
118 argument, which should be 26 or more bytes long,
119 instead of using a global static buffer.
120 .It Fn ctime_rz "tz" "clock" "buf"
123 function is similar to
126 .Ft "const timezone_t"
127 argument, as returned by a previous call to
129 .It Fn difftime "time1" "time2"
132 function returns the difference between two calendar times,
133 .Fa ( time1 No - Fa time0 ) ,
134 expressed in seconds.
135 .It Fn gmtime "clock"
138 function converts to Coordinated Universal Time
140 and returns a pointer to the
142 structure described in
144 .It Fn gmtime_r "clock" "result"
147 provides the same functionality as
149 differing in that the caller must supply a buffer area
151 to which the result is stored.
152 .It Fn localtime "clock"
159 corrects for the time zone and any time zone adjustments
160 (such as Daylight Saving Time in the U.S.A.).
163 structure, the function sets the
168 ASCII string that is the time zone abbreviation to be used with
171 .It Fn localtime_r "clock" "result"
176 takes an additional buffer
178 as a parameter and stores the result to it.
181 does not imply initialization of the local time conversion information;
182 the application may need to do so by calling
184 .It Fn localtime_rz "tz" "clock" "result"
187 function is similar to
190 .Ft "const timezone_t"
191 argument, returned by a previous call to
196 function converts the broken-down time,
197 expressed as local time in the
199 structure, into a calendar time value with
200 the same encoding as that of the values returned by the
203 The following remarks should be taken into account.
206 The original values of the
210 components of the structure are ignored,
211 and the original values of the other components are not restricted
212 to their normal ranges.
213 (A positive or zero value for
217 to presume initially that summer time (for example, Daylight Saving Time
218 in the U.S.A.) respectively,
219 is or is not in effect for the specified time.
225 function to attempt to divine whether summer time is in effect
226 for the specified time; in this case it does not use a consistent
227 rule and may give a different answer when later
228 presented with the same argument.
230 On successful completion, the values of the
234 components of the structure are set appropriately,
235 and the other components are set to represent the specified calendar time,
236 but with their values forced to their normal ranges; the final value of
245 The function returns the specified calendar time;
246 if the calendar time cannot be represented, it returns
248 This can happen either because the resulting conversion would not fit
251 variable, or because the time specified happens to be in the daylight
258 implementations do not return an error in the second case and return
259 the appropriate time offset after the daylight savings gap.
260 There is code to mimick this behavior, but it is not enabled by default.
261 .It Fn mktime_z "tz" "tm"
264 function is similar to
267 .Ft "const timezone_t"
268 argument, returned by a previous call to
270 .It Fn tzalloc "zone"
273 function takes as an argument a timezone name and returns a
275 object suitable to be used in the
282 Note that instead of setting the environment variable
284 and globally changing the behavior of the calling program, one can use
285 multiple timezones at the same time by using separate
291 variants of the functions.
297 which was previously allocated by
302 returns the name for the given
308 the call is equivalent to
314 the call is equivalent to
324 functions return a pointer to a static character buffer, and the
329 function return a pointer to the user-supplied buffer.
330 On failure they all return
332 and no errors are defined for them.
338 functions return a pointer to a statically allocated
345 functions return a pointer to the user-supplied
347 On failure they all return
349 and the global variable
351 is set to indicate the error.
357 function returns the specified time since the Epoch as a
360 If the time cannot be represented, then
366 setting the global variable
368 to indicate the error.
372 function returns a pointer to a
378 to indicate the error.
381 function returns string containing the name of the timezone given in
385 .Bl -tag -width /usr/share/zoneinfo/posixrules -compact
386 .It Pa /etc/localtime
388 .It Pa /usr/share/zoneinfo
389 time zone information directory
390 .It Pa /usr/share/zoneinfo/posixrules
391 used with POSIX-style TZ's
392 .It Pa /usr/share/zoneinfo/GMT
397 .Pa /usr/share/zoneinfo/GMT
398 is absent, UTC leap seconds are loaded from
399 .Pa /usr/share/zoneinfo/posixrules .
401 The described functions may fail with
404 The result cannot be represented because a parameter is incorrect, or
405 the conversion failed because no such time exists (for example a time
408 The result cannot be represented because the time requested is out of bounds
409 and the time calculation resulted in overflow.
412 All functions that return values, except their
414 variants, can also return the same errors as
436 Rest of the functions conform to
439 The functions that do not take an explicit
441 argument return values point to static data; the data is overwritten by
443 For the above functions the
447 points to a static array of characters, which
448 will also be overwritten at the next call
451 The functions that do take an explicit
453 argument and set the fields of a supplied
461 points to data allocated by
468 functions behave strangely for years before 1000 or after 9999.
469 The 1989 and 1999 editions of the C Standard say
470 that years from \-99 through 999 are converted without
471 extra spaces, but this conflicts with longstanding
472 tradition and with this implementation.
473 Traditional implementations of these two functions are
474 restricted to years in the range 1900 through 2099.
475 To avoid this portability mess, new programs should use
479 Avoid using out-of-range values with
481 when setting up lunch with promptness sticklers in Riyadh.
482 .\" @(#)newctime.3 8.3
483 .\" This file is in the public domain, so clarified as of
484 .\" 2009-05-17 by Arthur David Olson.