1.\" $NetBSD: ctime.3,v 1.58 2018/02/11 13:28:26 wiz Exp $ 2.\" 3.\" XXX: License missing? 4.\" 5.Dd February 7, 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 summer time (for example, Daylight Saving Time 262in the U.S.A.) respectively, 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 summer 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 char *tm_zone; /* abbreviation of timezone name (optional) */ 338 long tm_gmtoff; /* offset from UT in seconds (optional) */ 339.Ed 340.Bl -bullet 341.It 342.Va tm_isdst 343is non-zero if summer time is in effect. 344.It 345.Va tm_gmtoff 346is the offset (in seconds) of the time represented from UT, 347with positive values indicating east of the Prime Meridian. 348The field's name is derived from Greenwich Mean Time, a precursor of UT. 349.El 350In 351.Ft struct tm 352the 353.Fa tm_zone 354and 355.Fa tm_gmtoff 356fields exist, and are filled in, only if arrangements to do 357so were made when the library containing these functions was 358created. 359Similarly, the 360.Va tzname 361variable is optional. 362There is no guarantee that these fields and this variable will 363continue to exist in this form in future releases of this code. 364.Sh RETURN VALUES 365.Bl -bullet 366.It 367On success the 368.Fn asctime 369and 370.Fn ctime 371functions return a pointer to a static character buffer, and the 372.Fn asctime_r , 373.Fn ctime_r , 374and 375.Fn ctime_rz 376function return a pointer to the user-supplied buffer. 377On failure they all return 378.Dv NULL 379and no errors are defined for them. 380.It 381On success the 382.Fn gmtime , 383and 384.Fn localtime 385functions return a pointer to a statically allocated 386.Va "struct tm" 387whereas the 388.Fn gmtime_r , 389.Fn localtime_r , 390and 391.Fn localtime_rz , 392functions return a pointer to the user-supplied 393.Va "struct tm" . 394On failure they all return 395.Dv NULL 396and the global variable 397.Va errno 398is set to indicate the error. 399.It 400The 401.Fn mktime 402and 403.Fn mktime_z 404function returns the specified time since the Epoch as a 405.Vt time_t 406type value. 407If the time cannot be represented, then 408.Fn mktime 409and 410.Fn mktime_z 411return 412.Va "(time_t)-1" 413setting the global variable 414.Va errno 415to indicate the error. 416.It 417The 418.Fn tzalloc 419function returns a pointer to a 420.Ft timezone_t 421object or 422.Dv NULL 423on failure, setting 424.Va errno 425to indicate the error. 426It may also return 427.Dv NULL 428when the 429.Fa name 430argument is 431.Dv NULL , 432and this is not an error, but a way of referring to 433Coordinated Universal Time 434.Pq UTC . 435.It 436.Fn tzgetzone 437function returns string containing the name of the timezone given in 438.Fa tz . 439.El 440.Sh FILES 441.Bl -tag -width /usr/share/zoneinfo/posixrules -compact 442.It Pa /etc/localtime 443local time zone file 444.It Pa /usr/share/zoneinfo 445time zone information directory 446.\" .It Pa usr/share/zoneinfo/localtime 447.\" local time zone file 448.It Pa /usr/share/zoneinfo/posixrules 449used with POSIX-style TZ's 450.It Pa /usr/share/zoneinfo/GMT 451for UTC leap seconds 452.El 453.Pp 454If 455.Pa /usr/share/zoneinfo/GMT 456is absent, UTC leap seconds are loaded from 457.Pa /usr/share/zoneinfo/posixrules . 458.Sh ERRORS 459The described functions may fail with 460.Bl -tag -width Er 461.It Bq Er EINVAL 462The result cannot be represented because a parameter is incorrect, or 463the conversion failed because no such time exists (for example a time 464in the DST gap). 465.It Bq Er EOVERFLOW 466The result cannot be represented because the time requested is out of bounds 467and the time calculation resulted in overflow. 468.El 469.Pp 470All functions that return values, except their 471.Dq z 472variants, can also return the same errors as 473.Xr open 2 474and 475.Xr malloc 3 . 476.Sh SEE ALSO 477.Xr getenv 3 , 478.Xr strftime 3 , 479.Xr time 3 , 480.Xr tm 3 , 481.Xr tzset 3 , 482.Xr tzfile 5 483.Sh STANDARDS 484The 485.Fn ctime , 486.Fn difftime , 487.Fn asctime , 488.Fn localtime , 489.Fn gmtime 490and 491.Fn mktime 492functions conform to 493.St -ansiC . 494Rest of the functions conform to 495.St -p1003.1-2008 . 496.Sh CAVEATS 497The functions that do not take an explicit 498.Ft timezone_t 499argument return values pointing to static data; the data is overwritten by 500each call. 501For the above functions the 502.Dv tzname 503variable (once set) and the 504.Fa tm_zone 505field of a returned 506.Va "struct tm" 507point to an array of characters that 508can be freed or overwritten by later calls to the functions 509.Fn localtime , 510.Fn tzfree , 511and 512.Fn tzset , 513if these functions affect the time zone information that specifies the 514abbreviation in question. 515The remaining functions and data are thread-safe. 516The functions that do take an explicit 517.Ft timezone_t 518argument and set the fields of a supplied 519.Va "struct tm" 520should not call 521.Fn tzfree 522since the 523.Fa tm_zone 524field of the 525.Va "struct tm" 526points to data allocated by 527.Fn tzalloc . 528.Pp 529The 530.Fn asctime , 531.Fn asctime_r , 532.Fn ctime , 533.Fn ctime_r , 534and 535.Fn ctime_rz , 536functions behave strangely for years before 1000 or after 9999. 537The 1989 and 1999 editions of the C Standard say 538that years from \-99 through 999 are converted without 539extra spaces, but this conflicts with longstanding 540tradition and with this implementation. 541The 2011 edition says that the behavior 542is undefined if the year is before 1000 or after 9999. 543Traditional implementations of these two functions are 544restricted to years in the range 1900 through 2099. 545To avoid this portability mess, new programs should use 546.Fn strftime 547instead. 548.\" @(#)newctime.3 8.3 549.\" This file is in the public domain, so clarified as of 550.\" 2009-05-17 by Arthur David Olson. 551