1.\" $NetBSD: ctime.3,v 1.49 2013/12/26 18:34:28 christos Exp $ 2.\" 3.\" XXX: License missing? 4.\" 5.Dd December 26, 2013 6.Dt CTIME 3 7.Os 8.Sh NAME 9.Nm asctime , 10.Nm asctime_r , 11.Nm ctime , 12.Nm ctime_r , 13.Nm ctime_rz , 14.Nm difftime , 15.Nm gmtime , 16.Nm gmtime_r , 17.Nm localtime , 18.Nm localtime_r , 19.Nm localtime_rz , 20.Nm mktime , 21.Nm mktime_z , 22.Nm tzalloc , 23.Nm tzgetname , 24.Nm tzfree , 25.Nd convert date and time to ASCII 26.Sh LIBRARY 27.Lb libc 28.Sh SYNOPSIS 29.In time.h 30.Vt extern char *tzname[2]; 31.Ft char * 32.Fn asctime "const struct tm *tm" 33.Ft char * 34.Fn asctime_r "const struct tm restrict tm" "char * restrict buf" 35.Ft char * 36.Fn ctime "const time_t *clock" 37.Ft char * 38.Fn ctime_r "const time_t *clock" "char *buf" 39.Ft char * 40.Fn ctime_rz "const timezone_t tz" "const time_t *clock" "char *buf" 41.Ft double 42.Fn difftime "time_t time1" "time_t time0" 43.Ft struct tm * 44.Fn gmtime "const time_t *clock" 45.Ft struct tm * 46.Fn gmtime_r "const time_t * restrict clock" "struct tm * restrict result" 47.Ft struct tm * 48.Fn localtime "const time_t *clock" 49.Ft struct tm * 50.Fn localtime_r "const time_t * restrict clock" "struct tm * restrict result" 51.Ft struct tm * 52.Fn localtime_rz "const timezone_t tz" "const time_t * restrict clock" "struct tm * restrict result" 53.Ft time_t 54.Fn mktime "struct tm *tm" 55.Ft time_t 56.Fn mktime_z "const timezone_t tz" "struct tm *tm" 57.Ft timezone_t 58.Fn tzalloc "const char *zone" 59.Ft void 60.Fn tzfree "const timezone_t tz" 61.Ft const char * 62.Fn tzgetname "const timezone_t tz" "int isdst" 63.Sh DESCRIPTION 64The 65.Nm 66family of functions provide various standard library routines 67to operate with time and conversions related to time. 68.Sh FUNCTIONS 69.Bl -tag -width abcd 70.It Fn asctime "tm" 71The 72.Fn asctime 73function converts a time value contained in the 74.Fa tm 75structure to a string with the following general format: 76.Bd -literal -offset indent 77.D1 Thu Nov 24 18:22:48 1986\en\e0 78.Ed 79.Pp 80The 81.Fa tm 82structure is described in 83.Xr tm 3 . 84.It Fn asctime_r "tm" "buf" 85The 86.Fn asctime_r 87has the same behavior as 88.Fn asctime , 89but the result is stored to 90.Fa buf , 91which should have a size of at least 26 bytes. 92.It Fn ctime "clock" 93The 94.Fn ctime 95function converts a 96.Vt time_t , 97pointed to by 98.Fa clock , 99and returns a pointer to a string with the format described above. 100Years requiring fewer than four characters are padded with leading zeroes. 101For 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" 104.Ed 105.Pp 106with five spaces before the year. 107These unusual formats are designed to make it less likely that older 108software that expects exactly 26 bytes of output will mistakenly output 109misleading values for out-of-range years. 110.Pp 111The 112.Fa clock 113time stamp represents the time in seconds since 1970-01-01 00:00:00 114Coordinated Universal Time (UTC). 115The POSIX standard says that time stamps must be nonnegative 116and must ignore leap seconds. 117Many implementations extend POSIX by allowing negative time stamps, 118and can therefore represent time stamps that predate the 119introduction of UTC and are some other flavor of Universal Time (UT). 120Some implementations support leap seconds, in contradiction to POSIX. 121.It Fn ctime_r "clock" "buf" 122The 123.Fn ctime_r 124is similar to 125.Fn ctime , 126except it places the result of the conversion on the 127.Fa buf 128argument, which should be 26 or more bytes long, 129instead of using a global static buffer. 130.It Fn ctime_rz "tz" "clock" "buf" 131The 132.Fn ctime_rz 133function is similar to 134.Fn ctime_r , 135but it also takes a 136.Ft "const timezone_t" 137argument, as returned by a previous call to 138.Fn tzalloc , 139or a null pointer denoting 140Coordinated Universal Time 141.Pq Tn UTC . 142.It Fn difftime "time1" "time2" 143The 144.Fn difftime 145function returns the difference between two calendar times, 146.Fa ( time1 No - Fa time0 ) , 147expressed in seconds. 148.It Fn gmtime "clock" 149The 150.Fn gmtime 151function converts to Coordinated Universal Time 152.Pq Tn UTC 153and returns a pointer to the 154.Va tm 155structure described in 156.Xr tm 3 . 157.It Fn gmtime_r "clock" "result" 158The 159.Fn gmtime_r 160provides the same functionality as 161.Fn gmtime , 162differing in that the caller must supply a buffer area 163.Fa result 164to which the result is stored. 165.It Fn localtime "clock" 166Also 167.Fn localtime 168is comparable to 169.Fn gmtime . 170However, 171.Fn localtime 172corrects for the time zone and any time zone adjustments 173(such as Daylight Saving Time in the U.S.A.). 174After filling in the 175.Va tm 176structure, the function sets the 177.Fa tm_isdst Ns 'th 178element of 179.Fa tzname 180to a pointer to an 181ASCII string that is the time zone abbreviation to be used with 182.Fn localtime Ns 's 183return value. 184.It Fn localtime_r "clock" "result" 185As 186.Fn gmtime_r , 187the 188.Fn localtime_r 189takes an additional buffer 190.Fa result 191as a parameter and stores the result to it. 192Note however that 193.Fn localtime_r 194does not imply initialization of the local time conversion information; 195the application may need to do so by calling 196.Xr tzset 3 . 197.It Fn localtime_rz "tz" "clock" "result" 198The 199.Fn localtime_rz 200function is similar to 201.Fn localtime_r , 202but it also takes a 203.Ft "const timezone_t" 204argument, returned by a previous call to 205.Fn tzalloc , 206or a null pointer denoting 207Coordinated Universal Time 208.Pq Tn UTC . 209.It Fn mktime "tm" 210The 211.Fn mktime 212function converts the broken-down time, 213expressed as local time in the 214.Xr tm 3 215structure, into a calendar time value with 216the same encoding as that of the values returned by the 217.Xr time 3 218function. 219The following remarks should be taken into account. 220.Bl -bullet 221.It 222The original values of the 223.Fa tm_wday 224and 225.Fa tm_yday 226components of the structure are ignored, 227and the original values of the other components are not restricted 228to their normal ranges. 229(A positive or zero value for 230.Fa tm_isdst 231causes 232.Fn mktime 233to presume initially that summer time (for example, Daylight Saving Time 234in the U.S.A.) respectively, 235is or is not in effect for the specified time. 236.It 237A negative value for 238.Fa tm_isdst 239causes the 240.Fn mktime 241function to attempt to divine whether summer time is in effect 242for the specified time; in this case it does not use a consistent 243rule and may give a different answer when later 244presented with the same argument. 245.It 246On successful completion, the values of the 247.Fa tm_wday 248and 249.Fa tm_yday 250components of the structure are set appropriately, 251and the other components are set to represent the specified calendar time, 252but with their values forced to their normal ranges; the final value of 253.Fa tm_mday 254is not set until 255.Fa tm_mon 256and 257.Fa tm_year 258are determined. 259.El 260.Pp 261The function returns the specified calendar time; 262if the calendar time cannot be represented, it returns 263.Va "(time_t)-1" . 264This can happen either because the resulting conversion would not fit 265in a 266.Vt time_t 267variable, or because the time specified happens to be in the daylight 268savings gap and 269.Fa tm_isdst 270was set to 271.Dv \-1 . 272Other 273.Fn mktime 274implementations do not return an error in the second case and return 275the appropriate time offset after the daylight savings gap. 276There is code to mimick this behavior, but it is not enabled by default. 277.It Fn mktime_z "tz" "tm" 278The 279.Fn mktime_z 280function is similar to 281.Fn mktime 282but it also takes a 283.Ft "const timezone_t" 284argument, returned by a previous call to 285.Fn tzalloc , 286or a null pointer denoting 287Coordinated Universal Time 288.Pq Tn UTC . 289.It Fn tzalloc "zone" 290The 291.Fn tzalloc 292function takes as an argument a timezone name and returns a 293.Ft timezone_t 294object suitable to be used in the 295.Fn ctime_rz , 296.Fn localtime_rz , 297and 298.Fn mktime_z 299functions. 300.Pp 301A null pointer may be passed to 302.Fn tzalloc 303instead of a timezone name, to refer to 304Coordinated Universal Time 305.Pq Tn UTC . 306.Pp 307Note that instead of setting the environment variable 308.Va TZ , 309and globally changing the behavior of the calling program, one can use 310multiple timezones at the same time by using separate 311.Ft timezone_t 312objects allocated by 313.Fn tzalloc 314and calling the 315.Dq z 316variants of the functions. 317.It Fn tzfree "tz" 318The 319.Fn tzfree 320function deallocates 321.Fa tz , 322which was previously allocated by 323.Fn tzalloc . 324.It Fn "tzgetname" 325Finally, 326.Fn tzgetname 327returns the name for the given 328.Fa tz . 329If 330.Fa isdst 331is 332.Va 0 , 333the call is equivalent to 334.Va tzname[0] . 335If 336.Fa isdst 337is set to 338.Va 1 339the call is equivalent to 340.Va tzname[1] . 341.El 342.Pp 343Declarations of all the functions and externals, and the 344.Ft tm 345structure, are in the 346.In time.h 347header file. 348The structure (of type) 349.Ft struct tm 350includes the following fields: 351.Bd -literal 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 */ 363.Ed 364.Pp 365The 366.Fa tm_zone 367and 368.Fa tm_gmtoff 369fields exist, and are filled in, only if 370arrangements to do so were made when the library containing these functions 371was created. 372There is no guarantee that these fields will continue to exist in this form 373in future releases of this code. 374.Bl -bullet 375.It 376.Va tm_isdst 377is non-zero if summer time is in effect. 378.It 379.Va tm_gmtoff 380is the offset (in seconds) of the time represented from UT, 381with positive values indicating east of the Prime Meridian. 382The field's name is derived from Greenwich Mean Time, a precursor of UT. 383.El 384.Sh RETURN VALUES 385.Bl -bullet 386.It 387On success the 388.Fn asctime 389and 390.Fn ctime 391functions return a pointer to a static character buffer, and the 392.Fn asctime_r , 393.Fn ctime_r , 394and 395.Fn ctime_rz 396function return a pointer to the user-supplied buffer. 397On failure they all return 398.Dv NULL 399and no errors are defined for them. 400.It 401On success the 402.Fn gmtime , 403and 404.Fn localtime 405functions return a pointer to a statically allocated 406.Va "struct tm" 407whereas the 408.Fn gmtime_r , 409.Fn localtime_r , 410and 411.Fn localtime_rz , 412functions return a pointer to the user-supplied 413.Va "struct tm" . 414On failure they all return 415.Dv NULL 416and the global variable 417.Va errno 418is set to indicate the error. 419.It 420The 421.Fn mktime 422and 423.Fn mktime_z 424function returns the specified time since the Epoch as a 425.Vt time_t 426type value. 427If the time cannot be represented, then 428.Fn mktime 429and 430.Fn mktime_z 431return 432.Va "(time_t)-1" 433setting the global variable 434.Va errno 435to indicate the error. 436.It 437The 438.Fn tzalloc 439function returns a pointer to a 440.Ft timezone_t 441object or 442.Dv NULL 443on failure, setting 444.Va errno 445to indicate the error. 446It may also return 447.Dv NULL 448when the 449.Fa name 450argument is 451.Dv NULL , 452and this is not an error, but a way of referring to 453Coordinated Universal Time 454.Pq Tn UTC . 455.It 456.Fn tzgetzone 457function returns string containing the name of the timezone given in 458.Fa tz . 459.El 460.Sh FILES 461.Bl -tag -width /usr/share/zoneinfo/posixrules -compact 462.It Pa /etc/localtime 463local time zone file 464.It Pa /usr/share/zoneinfo 465time zone information directory 466.It Pa /usr/share/zoneinfo/posixrules 467used with POSIX-style TZ's 468.It Pa /usr/share/zoneinfo/GMT 469for UTC leap seconds 470.El 471.Pp 472If 473.Pa /usr/share/zoneinfo/GMT 474is absent, UTC leap seconds are loaded from 475.Pa /usr/share/zoneinfo/posixrules . 476.Sh ERRORS 477The described functions may fail with 478.Bl -tag -width Er 479.It Bq Er EINVAL 480The result cannot be represented because a parameter is incorrect, or 481the conversion failed because no such time exists (for example a time 482in the DST gap). 483.It Bq Er EOVERFLOW 484The result cannot be represented because the time requested is out of bounds 485and the time calculation resulted in overflow. 486.El 487.Pp 488All functions that return values, except their 489.Dq z 490variants, can also return the same errors as 491.Xr open 2 492and 493.Xr malloc 3 . 494.Sh SEE ALSO 495.Xr getenv 3 , 496.Xr strftime 3 , 497.Xr time 3 , 498.Xr tm 3 , 499.Xr tzset 3 , 500.Xr tzfile 5 501.Sh STANDARDS 502The 503.Fn ctime , 504.Fn difftime , 505.Fn asctime , 506.Fn localtime , 507.Fn gmtime 508and 509.Fn mktime 510functions conform to 511.St -ansiC . 512Rest of the functions conform to 513.St -p1003.1-2008 . 514.Sh CAVEATS 515The functions that do not take an explicit 516.Ft timezone_t 517argument return values point to static data; the data is overwritten by 518each call. 519For the above functions the 520.Fa tm_zone 521field of a returned 522.Va "struct tm" 523points to a static array of characters, which 524will also be overwritten at the next call 525(and by calls to 526.Xr tzset 3 ) . 527The functions that do take an explicit 528.Ft timezone_t 529argument and set the fields of a supplied 530.Va "struct tm" 531should not call 532.Fn tzfree 533since the 534.Fa tm_zone 535field of the 536.Va "struct tm" 537points to data allocated by 538.Fn tzalloc . 539.Pp 540The 541.Fn asctime 542and 543.Fn ctime 544functions behave strangely for years before 1000 or after 9999. 545The 1989 and 1999 editions of the C Standard say 546that years from \-99 through 999 are converted without 547extra spaces, but this conflicts with longstanding 548tradition and with this implementation. 549Traditional implementations of these two functions are 550restricted to years in the range 1900 through 2099. 551To avoid this portability mess, new programs should use 552.Fn strftime 553instead. 554.\" @(#)newctime.3 8.3 555.\" This file is in the public domain, so clarified as of 556.\" 2009-05-17 by Arthur David Olson. 557