1 .\" $NetBSD: ctime.3,v 1.48 2013/09/20 19:06:54 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 and returns a pointer to a string with the format described above.
100 Years requiring fewer than four characters are padded with leading zeroes.
101 For years longer than four characters, the string is of the form
102 .Bd -literal -offset indent
103 .D1 "Thu Nov 24 18:22:48 81986\en\e0"
106 with five spaces before the year.
107 These unusual formats are designed to make it less likely that older
108 software that expects exactly 26 bytes of output will mistakenly output
109 misleading values for out-of-range years.
113 time stamp represents the time in seconds since 1970-01-01 00:00:00
114 Coordinated Universal Time (UTC).
115 The POSIX standard says that time stamps must be nonnegative
116 and must ignore leap seconds.
117 Many implementations extend POSIX by allowing negative time stamps,
118 and can therefore represent time stamps that predate the
119 introduction of UTC and are some other flavor of Universal Time (UT).
120 Some implementations support leap seconds, in contradiction to POSIX.
121 .It Fn ctime_r "clock" "buf"
126 except it places the result of the conversion on the
128 argument, which should be 26 or more bytes long,
129 instead of using a global static buffer.
130 .It Fn ctime_rz "tz" "clock" "buf"
133 function is similar to
136 .Ft "const timezone_t"
137 argument, as returned by a previous call to
139 or a null pointer denoting
140 Coordinated Universal Time
142 .It Fn difftime "time1" "time2"
145 function returns the difference between two calendar times,
146 .Fa ( time1 No - Fa time0 ) ,
147 expressed in seconds.
148 .It Fn gmtime "clock"
151 function converts to Coordinated Universal Time
153 and returns a pointer to the
155 structure described in
157 .It Fn gmtime_r "clock" "result"
160 provides the same functionality as
162 differing in that the caller must supply a buffer area
164 to which the result is stored.
165 .It Fn localtime "clock"
172 corrects for the time zone and any time zone adjustments
173 (such as Daylight Saving Time in the U.S.A.).
176 structure, the function sets the
181 ASCII string that is the time zone abbreviation to be used with
184 .It Fn localtime_r "clock" "result"
189 takes an additional buffer
191 as a parameter and stores the result to it.
194 does not imply initialization of the local time conversion information;
195 the application may need to do so by calling
197 .It Fn localtime_rz "tz" "clock" "result"
200 function is similar to
203 .Ft "const timezone_t"
204 argument, returned by a previous call to
206 or a null pointer denoting
207 Coordinated Universal Time
212 function converts the broken-down time,
213 expressed as local time in the
215 structure, into a calendar time value with
216 the same encoding as that of the values returned by the
219 The following remarks should be taken into account.
222 The original values of the
226 components of the structure are ignored,
227 and the original values of the other components are not restricted
228 to their normal ranges.
229 (A positive or zero value for
233 to presume initially that summer time (for example, Daylight Saving Time
234 in the U.S.A.) respectively,
235 is or is not in effect for the specified time.
241 function to attempt to divine whether summer time is in effect
242 for the specified time; in this case it does not use a consistent
243 rule and may give a different answer when later
244 presented with the same argument.
246 On successful completion, the values of the
250 components of the structure are set appropriately,
251 and the other components are set to represent the specified calendar time,
252 but with their values forced to their normal ranges; the final value of
261 The function returns the specified calendar time;
262 if the calendar time cannot be represented, it returns
264 This can happen either because the resulting conversion would not fit
267 variable, or because the time specified happens to be in the daylight
274 implementations do not return an error in the second case and return
275 the appropriate time offset after the daylight savings gap.
276 There is code to mimick this behavior, but it is not enabled by default.
277 .It Fn mktime_z "tz" "tm"
280 function is similar to
283 .Ft "const timezone_t"
284 argument, returned by a previous call to
286 or a null pointer denoting
287 Coordinated Universal Time
289 .It Fn tzalloc "zone"
292 function takes as an argument a timezone name and returns a
294 object suitable to be used in the
301 A null pointer may be passed to
303 instead of a timezone name, to refer to
304 Coordinated Universal Time
307 Note that instead of setting the environment variable
309 and globally changing the behavior of the calling program, one can use
310 multiple timezones at the same time by using separate
316 variants of the functions.
322 which was previously allocated by
327 returns the name for the given
333 the call is equivalent to
339 the call is equivalent to
343 Declarations of all the functions and externals, and the
345 structure, are in the
348 The structure (of type)
350 includes the following fields:
352 int tm_sec; /* seconds (0 - 60) */
353 int tm_min; /* minutes (0 - 59) */
354 int tm_hour; /* hours (0 - 23) */
355 int tm_mday; /* day of month (1 - 31) */
356 int tm_mon; /* month of year (0 - 11) */
357 int tm_year; /* year - 1900 */
358 int tm_wday; /* day of week (Sunday = 0) */
359 int tm_yday; /* day of year (0 - 365) */
360 int tm_isdst; /* is summer time in effect? */
361 char *tm_zone; /* abbreviation of timezone name */
362 long tm_gmtoff; /* offset from UT in seconds */
369 fields exist, and are filled in, only if
370 arrangements to do so were made when the library containing these functions
372 There is no guarantee that these fields will continue to exist in this form
373 in future releases of this code.
377 is non-zero if summer time is in effect.
380 is the offset (in seconds) of the time represented from UT,
381 with positive values indicating east of the Prime Meridian.
382 The field's name is derived from Greenwich Mean Time, a precursor of UT.
391 functions return a pointer to a static character buffer, and the
396 function return a pointer to the user-supplied buffer.
397 On failure they all return
399 and no errors are defined for them.
405 functions return a pointer to a statically allocated
412 functions return a pointer to the user-supplied
414 On failure they all return
416 and the global variable
418 is set to indicate the error.
424 function returns the specified time since the Epoch as a
427 If the time cannot be represented, then
433 setting the global variable
435 to indicate the error.
439 function returns a pointer to a
445 to indicate the error.
452 and this is not an error, but a way of referring to
453 Coordinated Universal Time
457 function returns string containing the name of the timezone given in
461 .Bl -tag -width /usr/share/zoneinfo/posixrules -compact
462 .It Pa /etc/localtime
464 .It Pa /usr/share/zoneinfo
465 time zone information directory
466 .It Pa /usr/share/zoneinfo/posixrules
467 used with POSIX-style TZ's
468 .It Pa /usr/share/zoneinfo/GMT
473 .Pa /usr/share/zoneinfo/GMT
474 is absent, UTC leap seconds are loaded from
475 .Pa /usr/share/zoneinfo/posixrules .
477 The described functions may fail with
480 The result cannot be represented because a parameter is incorrect, or
481 the conversion failed because no such time exists (for example a time
484 The result cannot be represented because the time requested is out of bounds
485 and the time calculation resulted in overflow.
488 All functions that return values, except their
490 variants, can also return the same errors as
512 Rest of the functions conform to
515 The functions that do not take an explicit
517 argument return values point to static data; the data is overwritten by
519 For the above functions the
523 points to a static array of characters, which
524 will also be overwritten at the next call
527 The functions that do take an explicit
529 argument and set the fields of a supplied
537 points to data allocated by
544 functions behave strangely for years before 1000 or after 9999.
545 The 1989 and 1999 editions of the C Standard say
546 that years from \-99 through 999 are converted without
547 extra spaces, but this conflicts with longstanding
548 tradition and with this implementation.
549 Traditional implementations of these two functions are
550 restricted to years in the range 1900 through 2099.
551 To avoid this portability mess, new programs should use
555 Avoid using out-of-range values with
557 when setting up lunch with promptness sticklers in Riyadh.
558 .\" @(#)newctime.3 8.3
559 .\" This file is in the public domain, so clarified as of
560 .\" 2009-05-17 by Arthur David Olson.