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