1.\" $OpenBSD: ctime.3,v 1.36 2008/10/23 20:33:06 guenther Exp $ 2.\" 3.\" 4.Dd $Mdocdate: October 23 2008 $ 5.Dt CTIME 3 6.Os 7.Sh NAME 8.Nm asctime , 9.Nm asctime_r , 10.Nm ctime , 11.Nm ctime_r , 12.Nm difftime , 13.Nm gmtime , 14.Nm gmtime_r , 15.Nm localtime , 16.Nm localtime_r , 17.Nm mktime , 18.Nm timegm , 19.Nm timelocal 20.Nd convert date and time to ASCII 21.Sh SYNOPSIS 22.Fd #include <sys/types.h> 23.Fd #include <time.h> 24.Pp 25.Fd extern char *tzname[2]; 26.Ft "char *" 27.Fn ctime "const time_t *clock" 28.Ft "char *" 29.Fn ctime_r "const time_t *clock" "char *buf" 30.Ft double 31.Fn difftime "time_t time1" "time_t time0" 32.Ft "char *" 33.Fn asctime "const struct tm *tm" 34.Ft "char *" 35.Fn asctime_r "const struct tm *tm" "char *buf" 36.Ft "struct tm *" 37.Fn localtime "const time_t *clock" 38.Ft "struct tm *" 39.Fn localtime_r "const time_t *clock" "struct tm *result" 40.Ft "struct tm *" 41.Fn gmtime "const time_t *clock" 42.Ft "struct tm *" 43.Fn gmtime_r "const time_t *clock" "struct tm *result" 44.Ft time_t 45.Fn mktime "struct tm *tm" 46.Ft time_t 47.Fn timegm "struct tm *tm" 48.Ft time_t 49.Fn timelocal "struct tm *tm" 50.Sh DESCRIPTION 51The 52.Fn ctime 53function converts a 54.Li time_t , 55pointed to by 56.Fa clock , 57representing the time in seconds since 5800:00:00 UTC, 1970-01-01, 59and returns a pointer to a 60string of the form 61.Pp 62.Dl Thu Nov 24 18:22:48 1986\en 63.Pp 64Years requiring fewer than four characters are padded with leading zeroes. 65For years longer than four characters, the string is of the form 66.Pp 67.Dl Thu Nov 24 18:22:48\ \ \ \ \ 81986\en 68.Pp 69with five spaces before the year. 70These unusual formats are designed to make it less likely that older 71software that expects exactly 26 bytes of output will mistakenly output 72misleading values for out-of-range years. 73.Pp 74The 75.Fn ctime_r 76function converts the calendar time pointed to by 77.Fa clock 78to local time in exactly the same way as 79.Fn ctime 80and puts the string into the array pointed to by 81.Fa buf 82(which contains at least 26 bytes) and returns 83.Fa buf . 84Unlike 85.Fn ctime , 86the thread-safe version 87.Fn ctime_r 88is not required to set 89.Fa tzname . 90.Pp 91The 92.Fn localtime 93and 94.Fn gmtime 95functions return pointers to 96.Li tm 97structures, described below. 98.Fn localtime 99corrects for the time zone and any time zone adjustments 100(such as Daylight Saving Time in the United States). 101After filling in the 102.Li tm 103structure, 104.Fn localtime 105sets the 106.Fa tm_isdst Ns 'th 107element of 108.Fa tzname 109to a pointer to an 110ASCII string that's the time zone abbreviation to be used with 111the return value of 112.Fn localtime . 113.Pp 114.Fn gmtime 115converts to Coordinated Universal Time. 116.Pp 117The 118.Fn localtime_r 119and 120.Fn gmtime_r 121functions convert the calendar time pointed to by 122.Fa clock 123into a broken-down time in exactly the same way as their non-reentrant 124counterparts, 125.Fn localtime 126and 127.Fn gmtime , 128but instead store the result directly into the structure pointed to by 129.Fa result . 130Unlike 131.Fn localtime , 132the reentrant version is not required to set 133.Fa tzname . 134.Pp 135.Fn asctime 136converts a time value contained in a 137.Li tm 138structure to a string, 139as shown in the above example, 140and returns a pointer to the string. 141.Fn asctime_r 142uses the buffer pointed to by 143.Fa buf 144(which should contain at least 26 bytes) and then 145returns 146.Fa buf . 147.Pp 148.Fn mktime 149converts the broken-down time, 150expressed as local time, 151in the structure pointed to by 152.Fa tm 153into a calendar time value with the same encoding as that of the values 154returned by the 155.Fn time 156function. 157The original values of the 158.Fa tm_wday 159and 160.Fa tm_yday 161components of the structure are ignored, 162and the original values of the other components are not restricted 163to their normal ranges. 164(A positive or zero value for 165.Fa tm_isdst 166causes 167.Fn mktime 168to presume initially that summer time (for example, Daylight Saving Time 169in the U.S.A.)\& 170respectively, 171is or is not in effect for the specified time. 172A negative value for 173.Fa tm_isdst 174causes the 175.Fn mktime 176function to attempt to divine whether summer time is in effect 177for the specified time; in this case it does not use a consistent 178rule and may give a different answer when later 179presented with the same argument.) 180On successful completion, the values of the 181.Fa tm_wday 182and 183.Fa tm_yday 184components of the structure are set appropriately, 185and the other components are set to represent the specified calendar time, 186but with their values forced to their normal ranges; the final value of 187.Fa tm_mday 188is not set until 189.Fa tm_mon 190and 191.Fa tm_year 192are determined. 193.Fn mktime 194returns the specified calendar time; 195if the calendar time cannot be represented, 196it returns \-1. 197.Pp 198.Fn timelocal 199is a deprecated interface that is equivalent to calling 200.Fn mktime 201with a negative value for 202.Fa tm_isdst . 203.Pp 204.Fn timegm 205is a deprecated interface that converts the broken-down time, as returned by 206.Fn gmtime , 207into a calendar time value with the same encoding as that of the values 208returned by the 209.Fn time 210function. 211.Pp 212.Fn difftime 213returns the difference between two calendar times, 214.Pf ( Fa time1 No \- Fa time0 ) , 215expressed in seconds. 216.Pp 217Declarations of all the functions and externals, and the 218.Li tm 219structure, are in the 220.Aq Pa time.h 221header file. 222The structure (of type) 223.Li struct tm 224includes the following fields: 225.Bd -literal -offset indent 226 int tm_sec; /* seconds (0 \- 60) */ 227 int tm_min; /* minutes (0 \- 59) */ 228 int tm_hour; /* hours (0 \- 23) */ 229 int tm_mday; /* day of month (1 \- 31) */ 230 int tm_mon; /* month of year (0 \- 11) */ 231 int tm_year; /* year \- 1900 */ 232 int tm_wday; /* day of week (Sunday = 0) */ 233 int tm_yday; /* day of year (0 \- 365) */ 234 int tm_isdst; /* is summer time in effect? */ 235 long tm_gmtoff; /* offset from UTC in seconds */ 236 char *tm_zone; /* abbreviation of timezone name */ 237.Ed 238.Pp 239The 240.Fa tm_zone 241and 242.Fa tm_gmtoff 243fields exist, and are filled in by 244.Fn mktime , 245.Fn localtime , 246.Fn timegm , 247and 248.Fn gmtime , 249but are not standardized. 250There is no guarantee that these fields will continue to exist 251in this form and they may be altered or removed in a future release. 252.Pp 253.Fa tm_isdst 254is non-zero if summer time is in effect. 255.Pp 256.Fa tm_gmtoff 257is the offset (in seconds) of the time represented 258from UTC, with positive values indicating east 259of the Prime Meridian. 260.Sh FILES 261.Bl -tag -width "/usr/share/zoneinfo/posixrules" -compact 262.It Pa /usr/share/zoneinfo 263time zone information directory 264.It Pa /etc/localtime 265local time zone file 266.It Pa /usr/share/zoneinfo/posixrules 267used with POSIX-style TZ's 268.It Pa /usr/share/zoneinfo/GMT 269for UTC leap seconds 270.El 271.Pp 272If 273.Pa /usr/share/zoneinfo/GMT 274is absent, 275UTC leap seconds are loaded from 276.Pa /usr/share/zoneinfo/posixrules . 277.Sh SEE ALSO 278.Xr getenv 3 , 279.Xr strftime 3 , 280.Xr time 3 , 281.Xr tzset 3 , 282.Xr tzfile 5 , 283.Xr zic 8 284.Sh NOTES 285The return values 286of the non re-entrant functions 287point to static data; 288the data is overwritten by each call. 289The 290.Fa tm_zone 291field of a returned 292.Li struct tm 293points to a static array of characters, which 294will also be overwritten at the next call 295(and by calls to 296.Xr tzset 3 ) . 297.Pp 298.Fn asctime 299and 300.Fn ctime 301behave strangely for years before 1000 or after 9999. 302The 1989 and 1999 editions of the C Standard say 303that years from \-99 through 999 are converted without 304extra spaces, but this conflicts with longstanding 305tradition and with this implementation. 306Traditional implementations of these two functions are 307restricted to years in the range 1900 through 2099. 308To avoid this portability mess, new programs should use 309.Fn strftime 310instead. 311.Pp 312The default system time zone may be set by running 313.Dq Li zic -l timezone 314as the superuser. 315.Pp 316Avoid using out-of-range values with 317.Fn mktime 318when setting up lunch with promptness sticklers in Riyadh. 319.\" based on @(#)newctime.3 8.2 320.\" This file is in the public domain, so clarified as of 321.\" 2003-02-14 by Arthur David Olson (arthur_david_olson@nih.gov). 322