1newctime(3) Library Functions Manual newctime(3) 2 3NAME 4 asctime, ctime, difftime, gmtime, localtime, mktime - convert date and 5 time 6 7SYNOPSIS 8 #include <time.h> 9 10 extern char *tzname[]; /* (optional) */ 11 12 [[deprecated]] char *ctime(time_t const *clock); 13 14 char *ctime_r(time_t const *clock, char *buf); 15 16 double difftime(time_t time1, time_t time0); 17 18 [[deprecated]] char *asctime(struct tm const *tm); 19 20 char *asctime_r(struct tm const *restrict tm, 21 char *restrict result); 22 23 struct tm *localtime(time_t const *clock); 24 25 struct tm *localtime_r(time_t const *restrict clock, 26 struct tm *restrict result); 27 28 struct tm *localtime_rz(timezone_t restrict zone, 29 time_t const *restrict clock, 30 struct tm *restrict result); 31 32 struct tm *gmtime(time_t const *clock); 33 34 struct tm *gmtime_r(time_t const *restrict clock, 35 struct tm *restrict result); 36 37 time_t mktime(struct tm *tm); 38 39 time_t mktime_z(timezone_t restrict zone, 40 struct tm *restrict tm); 41 42 cc ... -ltz 43 44DESCRIPTION 45 The ctime function converts a long integer, pointed to by clock, and 46 returns a pointer to a string of the form 47 Thu Nov 24 18:22:48 1986\n\0 48 Years requiring fewer than four characters are padded with leading 49 zeroes. For years longer than four characters, the string is of the 50 form 51 Thu Nov 24 18:22:48 81986\n\0 52 with five spaces before the year. These unusual formats are designed 53 to make it less likely that older software that expects exactly 26 54 bytes of output will mistakenly output misleading values for out-of- 55 range years. 56 57 The *clock timestamp represents the time in seconds since 1970-01-01 58 00:00:00 Coordinated Universal Time (UTC). The POSIX standard says 59 that timestamps must be nonnegative and must ignore leap seconds. Many 60 implementations extend POSIX by allowing negative timestamps, and can 61 therefore represent timestamps that predate the introduction of UTC and 62 are some other flavor of Universal Time (UT). Some implementations 63 support leap seconds, in contradiction to POSIX. 64 65 The ctime function is deprecated starting in C23. Callers can use 66 localtime_r and strftime instead. 67 68 The localtime and gmtime functions return pointers to "tm" structures, 69 described below. The localtime function corrects for the time zone and 70 any time zone adjustments (such as Daylight Saving Time in the United 71 States). After filling in the "tm" structure, localtime sets the 72 tm_isdst'th element of tzname to a pointer to a string that's the time 73 zone abbreviation to be used with localtime's return value. 74 75 The gmtime function converts to Coordinated Universal Time. 76 77 The asctime function converts a time value contained in a "tm" 78 structure to a string, as shown in the above example, and returns a 79 pointer to the string. This function is deprecated starting in C23. 80 Callers can use strftime instead. 81 82 The mktime function converts the broken-down time, expressed as local 83 time, in the structure pointed to by tm into a calendar time value with 84 the same encoding as that of the values returned by the time function. 85 The original values of the tm_wday and tm_yday components of the 86 structure are ignored, and the original values of the other components 87 are not restricted to their normal ranges. (A positive or zero value 88 for tm_isdst causes mktime to presume initially that daylight saving 89 time respectively, is or is not in effect for the specified time. A 90 negative value for tm_isdst causes the mktime function to attempt to 91 divine whether daylight saving time is in effect for the specified 92 time; in this case it does not use a consistent rule and may give a 93 different answer when later presented with the same argument.) On 94 successful completion, the values of the tm_wday and tm_yday components 95 of the structure are set appropriately, and the other components are 96 set to represent the specified calendar time, but with their values 97 forced to their normal ranges; the final value of tm_mday is not set 98 until tm_mon and tm_year are determined. The mktime function returns 99 the specified calendar time; If the calendar time cannot be 100 represented, it returns -1. 101 102 The difftime function returns the difference between two calendar 103 times, (time1 - time0), expressed in seconds. 104 105 The ctime_r, localtime_r, gmtime_r, and asctime_r functions are like 106 their unsuffixed counterparts, except that they accept an additional 107 argument specifying where to store the result if successful. 108 109 The localtime_rz and mktime_z functions are like their unsuffixed 110 counterparts, except that they accept an extra initial zone argument 111 specifying the timezone to be used for conversion. If zone is null, UT 112 is used; otherwise, zone should be have been allocated by tzalloc and 113 should not be freed until after all uses (e.g., by calls to strftime) 114 of the filled-in tm_zone fields. 115 116 Declarations of all the functions and externals, and the "tm" 117 structure, are in the <time.h> header file. The structure (of type) 118 struct tm includes the following fields: 119 120 int tm_sec; /* seconds (0-60) */ 121 int tm_min; /* minutes (0-59) */ 122 int tm_hour; /* hours (0-23) */ 123 int tm_mday; /* day of month (1-31) */ 124 int tm_mon; /* month of year (0-11) */ 125 int tm_year; /* year - 1900 */ 126 int tm_wday; /* day of week (Sunday = 0) */ 127 int tm_yday; /* day of year (0-365) */ 128 int tm_isdst; /* is daylight saving time in effect? */ 129 char *tm_zone; /* time zone abbreviation (optional) */ 130 long tm_gmtoff; /* offset from UT in seconds (optional) */ 131 132 The tm_isdst field is non-zero if daylight saving time is in effect. 133 134 The tm_gmtoff field is the offset (in seconds) of the time represented 135 from UT, with positive values indicating east of the Prime Meridian. 136 The field's name is derived from Greenwich Mean Time, a precursor of 137 UT. 138 139 In struct tm the tm_zone and tm_gmtoff fields exist, and are filled in, 140 only if arrangements to do so were made when the library containing 141 these functions was created. Similarly, the tzname variable is 142 optional; also, there is no guarantee that tzname will continue to 143 exist in this form in future releases of this code. 144 145FILES 146 /etc/localtime local timezone file 147 /usr/share/zoneinfo timezone directory 148 /usr/share/zoneinfo/posixrules default DST rules (obsolete) 149 /usr/share/zoneinfo/GMT for UTC leap seconds 150 151 If /usr/share/zoneinfo/GMT is absent, UTC leap seconds are loaded from 152 /usr/share/zoneinfo/GMT0 if present. 153 154SEE ALSO 155 getenv(3), newstrftime(3), newtzset(3), time(2), tzfile(5) 156 157NOTES 158 The return values of asctime, ctime, gmtime, and localtime point to 159 static data overwritten by each call. The tzname variable (once set) 160 and the tm_zone field of a returned struct tm both point to an array of 161 characters that can be freed or overwritten by later calls to the 162 functions localtime, tzfree, and tzset, if these functions affect the 163 timezone information that specifies the abbreviation in question. The 164 remaining functions and data are thread-safe. 165 166 The asctime, asctime_r, ctime, and ctime_r functions behave strangely 167 for years before 1000 or after 9999. The 1989 and 1999 editions of the 168 C Standard say that years from -99 through 999 are converted without 169 extra spaces, but this conflicts with longstanding tradition and with 170 this implementation. The 2011 edition says that the behavior is 171 undefined if the year is before 1000 or after 9999. Traditional 172 implementations of these two functions are restricted to years in the 173 range 1900 through 2099. To avoid this portability mess, new programs 174 should use strftime instead. 175 176Time Zone Database newctime(3) 177