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