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