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 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 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 localtime and gmtime functions return pointers to "tm" structures, 66 described below. The localtime function corrects for the time zone and 67 any time zone adjustments (such as Daylight Saving Time in the United 68 States). After filling in the "tm" structure, localtime sets the 69 tm_isdst'th element of tzname to a pointer to a string that's the time 70 zone abbreviation to be used with localtime's return value. 71 72 The gmtime function converts to Coordinated Universal Time. 73 74 The asctime function converts a time value contained in a "tm" 75 structure to a string, as shown in the above example, and returns a 76 pointer to the string. 77 78 The mktime function converts the broken-down time, expressed as local 79 time, in the structure pointed to by tm into a calendar time value with 80 the same encoding as that of the values returned by the time function. 81 The original values of the tm_wday and tm_yday components of the 82 structure are ignored, and the original values of the other components 83 are not restricted to their normal ranges. (A positive or zero value 84 for tm_isdst causes mktime to presume initially that daylight saving 85 time respectively, is or is not in effect for the specified time. A 86 negative value for tm_isdst causes the mktime function to attempt to 87 divine whether daylight saving time is in effect for the specified 88 time; in this case it does not use a consistent rule and may give a 89 different answer when later presented with the same argument.) On 90 successful completion, the values of the tm_wday and tm_yday components 91 of the structure are set appropriately, and the other components are 92 set to represent the specified calendar time, but with their values 93 forced to their normal ranges; the final value of tm_mday is not set 94 until tm_mon and tm_year are determined. The mktime function returns 95 the specified calendar time; If the calendar time cannot be 96 represented, it returns -1. 97 98 The difftime function returns the difference between two calendar 99 times, (time1 - time0), expressed in seconds. 100 101 The ctime_r, localtime_r, gmtime_r, and asctime_r functions are like 102 their unsuffixed counterparts, except that they accept an additional 103 argument specifying where to store the result if successful. 104 105 The localtime_rz and mktime_z functions are like their unsuffixed 106 counterparts, except that they accept an extra initial zone argument 107 specifying the timezone to be used for conversion. If zone is null, UT 108 is used; otherwise, zone should be have been allocated by tzalloc and 109 should not be freed until after all uses (e.g., by calls to strftime) 110 of the filled-in tm_zone fields. 111 112 Declarations of all the functions and externals, and the "tm" 113 structure, are in the <time.h> header file. The structure (of type) 114 struct tm includes the following fields: 115 116 int tm_sec; /* seconds (0-60) */ 117 int tm_min; /* minutes (0-59) */ 118 int tm_hour; /* hours (0-23) */ 119 int tm_mday; /* day of month (1-31) */ 120 int tm_mon; /* month of year (0-11) */ 121 int tm_year; /* year - 1900 */ 122 int tm_wday; /* day of week (Sunday = 0) */ 123 int tm_yday; /* day of year (0-365) */ 124 int tm_isdst; /* is daylight saving time in effect? */ 125 char *tm_zone; /* time zone abbreviation (optional) */ 126 long tm_gmtoff; /* offset from UT in seconds (optional) */ 127 128 The tm_isdst field is non-zero if daylight saving time is in effect. 129 130 The tm_gmtoff field is the offset (in seconds) of the time represented 131 from UT, with positive values indicating east of the Prime Meridian. 132 The field's name is derived from Greenwich Mean Time, a precursor of 133 UT. 134 135 In struct tm the tm_zone and tm_gmtoff fields exist, and are filled in, 136 only if arrangements to do so were made when the library containing 137 these functions was created. Similarly, the tzname variable is 138 optional. There is no guarantee that these fields and this variable 139 will continue to exist in this form in future releases of this code. 140 141FILES 142 /usr/share/zoneinfo timezone information directory 143 /usr/share/zoneinfo/localtime local timezone file 144 /usr/share/zoneinfo/posixrules used with POSIX-style TZ's 145 /usr/share/zoneinfo/GMT for UTC leap seconds 146 147 If /usr/share/zoneinfo/GMT is absent, UTC leap seconds are loaded from 148 /usr/share/zoneinfo/posixrules. 149 150SEE ALSO 151 getenv(3), newstrftime(3), newtzset(3), time(2), tzfile(5) 152 153NOTES 154 The return values of asctime, ctime, gmtime, and localtime point to 155 static data overwritten by each call. The tzname variable (once set) 156 and the tm_zone field of a returned struct tm both point to an array of 157 characters that can be freed or overwritten by later calls to the 158 functions localtime, tzfree, and tzset, if these functions affect the 159 timezone information that specifies the abbreviation in question. The 160 remaining functions and data are thread-safe. 161 162 The asctime, asctime_r, ctime, and ctime_r functions behave strangely 163 for years before 1000 or after 9999. The 1989 and 1999 editions of the 164 C Standard say that years from -99 through 999 are converted without 165 extra spaces, but this conflicts with longstanding tradition and with 166 this implementation. The 2011 edition says that the behavior is 167 undefined if the year is before 1000 or after 9999. Traditional 168 implementations of these two functions are restricted to years in the 169 range 1900 through 2099. To avoid this portability mess, new programs 170 should use strftime instead. 171 172 NEWCTIME(3) 173