1.\" $NetBSD: ctime.3,v 1.46 2011/11/02 23:06:08 christos Exp $ 2.\" 3.\" XXX: License missing? 4.\" 5.Dd November 2, 2011 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 , 99representing the time in seconds since 00:00:00 UTC, 1970-01-01, 100and returns a pointer to a string with the format described above. 101Years requiring fewer than four characters are padded with leading zeroes. 102For years longer than four characters, the string is of the form 103.Bd -literal -offset indent 104.D1 "Thu Nov 24 18:22:48 81986\en\e0" 105.Ed 106.Pp 107with five spaces before the year. 108These unusual formats are designed to make it less likely that older 109software that expects exactly 26 bytes of output will mistakenly output 110misleading values for out-of-range years. 111.It Fn ctime_r "clock" "buf" 112The 113.Fn ctime_r 114is similar to 115.Fn ctime , 116except it places the result of the conversion on the 117.Fa buf 118argument, which should be 26 or more bytes long, 119instead of using a global static buffer. 120.It Fn ctime_rz "tz" "clock" "buf" 121The 122.Fn ctime_rz 123function is similar to 124.Fn ctime_r , 125but it also takes a 126.Ft "const timezone_t" 127argument, as returned by a previous call to 128.Fn tzalloc . 129.It Fn difftime "time1" "time2" 130The 131.Fn difftime 132function returns the difference between two calendar times, 133.Fa ( time1 No - Fa time0 ) , 134expressed in seconds. 135.It Fn gmtime "clock" 136The 137.Fn gmtime 138function converts to Coordinated Universal Time 139.Pq Tn UTC 140and returns a pointer to the 141.Va tm 142structure described in 143.Xr tm 3 . 144.It Fn gmtime_r "clock" "result" 145The 146.Fn gmtime_r 147provides the same functionality as 148.Fn gmtime , 149differing in that the caller must supply a buffer area 150.Fa result 151to which the result is stored. 152.It Fn localtime "clock" 153Also 154.Fn localtime 155is comparable to 156.Fn gmtime . 157However, 158.Fn localtime 159corrects for the time zone and any time zone adjustments 160(such as Daylight Saving Time in the U.S.A.). 161After filling in the 162.Va tm 163structure, the function sets the 164.Fa tm_isdst Ns 'th 165element of 166.Fa tzname 167to a pointer to an 168ASCII string that is the time zone abbreviation to be used with 169.Fn localtime Ns 's 170return value. 171.It Fn localtime_r "clock" "result" 172As 173.Fn gmtime_r , 174the 175.Fn localtime_r 176takes an additional buffer 177.Fa result 178as a parameter and stores the result to it. 179Note however that 180.Fn localtime_r 181does not imply initialization of the local time conversion information; 182the application may need to do so by calling 183.Xr tzset 3 . 184.It Fn localtime_rz "tz" "clock" "result" 185The 186.Fn localtime_rz 187function is similar to 188.Fn localtime_r , 189but it also takes a 190.Ft "const timezone_t" 191argument, returned by a previous call to 192.Fn tzalloc . 193.It Fn mktime "tm" 194The 195.Fn mktime 196function converts the broken-down time, 197expressed as local time in the 198.Xr tm 3 199structure, into a calendar time value with 200the same encoding as that of the values returned by the 201.Xr time 3 202function. 203The following remarks should be taken into account. 204.Bl -bullet 205.It 206The original values of the 207.Fa tm_wday 208and 209.Fa tm_yday 210components of the structure are ignored, 211and the original values of the other components are not restricted 212to their normal ranges. 213(A positive or zero value for 214.Fa tm_isdst 215causes 216.Fn mktime 217to presume initially that summer time (for example, Daylight Saving Time 218in the U.S.A.) respectively, 219is or is not in effect for the specified time. 220.It 221A negative value for 222.Fa tm_isdst 223causes the 224.Fn mktime 225function to attempt to divine whether summer time is in effect 226for the specified time; in this case it does not use a consistent 227rule and may give a different answer when later 228presented with the same argument. 229.It 230On successful completion, the values of the 231.Fa tm_wday 232and 233.Fa tm_yday 234components of the structure are set appropriately, 235and the other components are set to represent the specified calendar time, 236but with their values forced to their normal ranges; the final value of 237.Fa tm_mday 238is not set until 239.Fa tm_mon 240and 241.Fa tm_year 242are determined. 243.El 244.Pp 245The function returns the specified calendar time; 246if the calendar time cannot be represented, it returns 247.Va "(time_t)-1" . 248This can happen either because the resulting conversion would not fit 249in a 250.Vt time_t 251variable, or because the time specified happens to be in the daylight 252savings gap and 253.Fa tm_isdst 254was set to 255.Dv \-1 . 256Other 257.Fn mktime 258implementations do not return an error in the second case and return 259the appropriate time offset after the daylight savings gap. 260There is code to mimick this behavior, but it is not enabled by default. 261.It Fn mktime_z "tz" "tm" 262The 263.Fn mktime_z 264function is similar to 265.Fn mktime 266but it also takes a 267.Ft "const timezone_t" 268argument, returned by a previous call to 269.Fn tzalloc . 270.It Fn tzalloc "zone" 271The 272.Fn tzalloc 273function takes as an argument a timezone name and returns a 274.Ft timezone_t 275object suitable to be used in the 276.Fn ctime_rz , 277.Fn localtime_rz , 278and 279.Fn mktime_z 280functions. 281.Pp 282Note that instead of setting the environment variable 283.Va TZ , 284and globally changing the behavior of the calling program, one can use 285multiple timezones at the same time by using separate 286.Ft timezone_t 287objects allocated by 288.Fn tzalloc 289and calling the 290.Dq z 291variants of the functions. 292.It Fn tzfree "tz" 293The 294.Fn tzfree 295function deallocates 296.Fa tz , 297which was previously allocated by 298.Fn tzalloc . 299.It Fn "tzgetname" 300Finally, 301.Fn tzgetname 302returns the name for the given 303.Fa tz . 304If 305.Fa isdst 306is 307.Va 0 , 308the call is equivalent to 309.Va tzname[0] . 310If 311.Fa isdst 312is set to 313.Va 1 314the call is equivalent to 315.Va tzname[1] . 316.El 317.Sh RETURN VALUES 318.Bl -bullet 319.It 320On success the 321.Fn asctime 322and 323.Fn ctime 324functions return a pointer to a static character buffer, and the 325.Fn asctime_r , 326.Fn ctime_r , 327and 328.Fn ctime_rz 329function return a pointer to the user-supplied buffer. 330On failure they all return 331.Dv NULL 332and no errors are defined for them. 333.It 334On success the 335.Fn gmtime , 336and 337.Fn localtime 338functions return a pointer to a statically allocated 339.Va "struct tm" 340whereas the 341.Fn gmtime_r , 342.Fn localtime_r , 343and 344.Fn localtime_rz , 345functions return a pointer to the user-supplied 346.Va "struct tm" . 347On failure they all return 348.Dv NULL 349and the global variable 350.Va errno 351is set to indicate the error. 352.It 353The 354.Fn mktime 355and 356.Fn mktime_z 357function returns the specified time since the Epoch as a 358.Vt time_t 359type value. 360If the time cannot be represented, then 361.Fn mktime 362and 363.Fn mktime_z 364return 365.Va "(time_t)-1" 366setting the global variable 367.Va errno 368to indicate the error. 369.It 370The 371.Fn tzalloc 372function returns a pointer to a 373.Ft timezone_t 374object or 375.Dv NULL 376on failure, setting 377.Va errno 378to indicate the error. 379.It 380.Fn tzgetzone 381function returns string containing the name of the timezone given in 382.Fa tz . 383.El 384.Sh FILES 385.Bl -tag -width /usr/share/zoneinfo/posixrules -compact 386.It Pa /etc/localtime 387local time zone file 388.It Pa /usr/share/zoneinfo 389time zone information directory 390.It Pa /usr/share/zoneinfo/posixrules 391used with POSIX-style TZ's 392.It Pa /usr/share/zoneinfo/GMT 393for UTC leap seconds 394.El 395.Pp 396If 397.Pa /usr/share/zoneinfo/GMT 398is absent, UTC leap seconds are loaded from 399.Pa /usr/share/zoneinfo/posixrules . 400.Sh ERRORS 401The described functions may fail with 402.Bl -tag -width Er 403.It Bq Er EINVAL 404The result cannot be represented because a parameter is incorrect, or 405the conversion failed because no such time exists (for example a time 406in the DST gap). 407.It Bq Er EOVERFLOW 408The result cannot be represented because the time requested is out of bounds 409and the time calculation resulted in overflow. 410.El 411.Pp 412All functions that return values, except their 413.Dq z 414variants, can also return the same errors as 415.Xr open 2 416and 417.Xr malloc 3 . 418.Sh SEE ALSO 419.Xr getenv 3 , 420.Xr strftime 3 , 421.Xr time 3 , 422.Xr tm 3 , 423.Xr tzset 3 , 424.Xr tzfile 5 425.Sh STANDARDS 426The 427.Fn ctime , 428.Fn difftime , 429.Fn asctime , 430.Fn localtime , 431.Fn gmtime 432and 433.Fn mktime 434functions conform to 435.St -ansiC . 436Rest of the functions conform to 437.St -p1003.1-2008 . 438.Sh CAVEATS 439The functions that do not take an explicit 440.Ft timezone_t 441argument return values point to static data; the data is overwritten by 442each call. 443For the above functions the 444.Fa tm_zone 445field of a returned 446.Va "struct tm" 447points to a static array of characters, which 448will also be overwritten at the next call 449(and by calls to 450.Xr tzset 3 ) . 451The functions that do take an explicit 452.Ft timezone_t 453argument and set the fields of a supplied 454.Va "struct tm" 455should not call 456.Fn tzfree 457since the 458.Fa tm_zone 459field of the 460.Va "struct tm" 461points to data allocated by 462.Fn tzalloc . 463.Pp 464The 465.Fn asctime 466and 467.Fn ctime 468functions behave strangely for years before 1000 or after 9999. 469The 1989 and 1999 editions of the C Standard say 470that years from \-99 through 999 are converted without 471extra spaces, but this conflicts with longstanding 472tradition and with this implementation. 473Traditional implementations of these two functions are 474restricted to years in the range 1900 through 2099. 475To avoid this portability mess, new programs should use 476.Fn strftime 477instead. 478.Pp 479Avoid using out-of-range values with 480.Fn mktime 481when setting up lunch with promptness sticklers in Riyadh. 482.\" @(#)newctime.3 8.3 483.\" This file is in the public domain, so clarified as of 484.\" 2009-05-17 by Arthur David Olson. 485