1 NEWCTIME(3) Library Functions Manual NEWCTIME(3)
4 asctime, ctime, difftime, gmtime, localtime, mktime - convert date and
10 extern char *tzname[2];
12 char *ctime(time_t const *clock);
14 char *ctime_r(time_t const *clock, char *buf);
16 double difftime(time_t time1, time_t time0);
18 char *asctime(struct tm const *tm);
20 char *asctime_r(struct tm const *restrict tm,
21 char *restrict result);
23 struct tm *localtime(time_t const *clock);
25 struct tm *localtime_r(time_t const *restrict clock,
26 struct tm *restrict result);
28 struct tm *localtime_rz(timezone_t restrict zone,
29 time_t const *restrict clock,
30 struct tm *restrict result);
32 struct tm *gmtime(time_t const *clock);
34 struct tm *gmtime_r(time_t const *restrict clock,
35 struct tm *restrict result);
37 time_t mktime(struct tm *tm);
39 time_t mktime_z(timezone_t restrict zone,
40 struct tm *restrict tm);
45 Ctime converts a long integer, pointed to by clock, and returns a
46 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
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-
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.
65 Localtime and gmtime return pointers to "tm" structures, described
66 below. Localtime corrects for the time zone and any time zone
67 adjustments (such as Daylight Saving Time in the United States). After
68 filling in the "tm" structure, localtime sets the tm_isdst'th element
69 of tzname to a pointer to a string that's the time zone abbreviation to
70 be used with localtime's return value.
72 Gmtime converts to Coordinated Universal Time.
74 Asctime converts a time value contained in a "tm" structure to a
75 string, as shown in the above example, and returns a pointer to the
78 Mktime converts the broken-down time, expressed as local time, in the
79 structure pointed to by tm into a calendar time value with the same
80 encoding as that of the values returned by the time function. The
81 original values of the tm_wday and tm_yday components of the structure
82 are ignored, and the original values of the other components are not
83 restricted to their normal ranges. (A positive or zero value for
84 tm_isdst causes mktime to presume initially that summer time (for
85 example, Daylight Saving Time in the U.S.A.) respectively, is or is
86 not in effect for the specified time. A negative value for tm_isdst
87 causes the mktime function to attempt to divine whether summer time is
88 in effect for the specified time; in this case it does not use a
89 consistent rule and may give a different answer when later presented
90 with the same argument.) On successful completion, the values of the
91 tm_wday and tm_yday components of the structure are set appropriately,
92 and the other components are set to represent the specified calendar
93 time, but with their values forced to their normal ranges; the final
94 value of tm_mday is not set until tm_mon and tm_year are determined.
95 Mktime returns the specified calendar time; If the calendar time cannot
96 be represented, it returns -1.
98 Difftime returns the difference between two calendar times, (time1 -
99 time0), expressed in seconds.
101 Ctime_r, localtime_r, gmtime_r, and asctime_r are like their unsuffixed
102 counterparts, except that they accept an additional argument specifying
103 where to store the result if successful.
105 Localtime_rz and mktime_z are like their unsuffixed counterparts,
106 except that they accept an extra initial zone argument specifying the
107 time zone to be used for conversion. If zone is null, UTC is used;
108 otherwise, zone should be have been allocated by tzalloc and should not
109 be freed until after all uses (e.g., by calls to strftime) of the
110 filled-in tm_zone fields.
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:
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 summer time in effect? */
125 char *tm_zone; /* abbreviation of time zone name */
126 long tm_gmtoff; /* offset from UT in seconds */
128 The tm_zone and tm_gmtoff fields exist, and are filled in, only if
129 arrangements to do so were made when the library containing these
130 functions was created. There is no guarantee that these fields will
131 continue to exist in this form in future releases of this code.
133 Tm_isdst is non-zero if summer time is in effect.
135 Tm_gmtoff is the offset (in seconds) of the time represented from UT,
136 with positive values indicating east of the Prime Meridian. The
137 field's name is derived from Greenwich Mean Time, a precursor of UT.
140 /usr/local/etc/zoneinfo time zone information directory
141 /usr/local/etc/zoneinfo/localtime local time zone file
142 /usr/local/etc/zoneinfo/posixrules used with POSIX-style TZ's
143 /usr/local/etc/zoneinfo/GMT for UTC leap seconds
145 If /usr/local/etc/zoneinfo/GMT is absent, UTC leap seconds are loaded
146 from /usr/local/etc/zoneinfo/posixrules.
149 getenv(3), newstrftime(3), newtzset(3), time(2), tzfile(5)
152 The return values of asctime, ctime, gmtime, and localtime point to
153 static data overwritten by each call. The tm_zone field of a returned
154 struct tm points to a static array of characters, which can be
155 overwritten by later calls to tzset. The remaining functions and data
158 Asctime, asctime_r, ctime, and ctime_r behave strangely for years
159 before 1000 or after 9999. The 1989 and 1999 editions of the C
160 Standard say that years from -99 through 999 are converted without
161 extra spaces, but this conflicts with longstanding tradition and with
162 this implementation. The 2011 edition says that the behavior is
163 undefined if the year is before 1000 or after 9999. Traditional
164 implementations of these two functions are restricted to years in the
165 range 1900 through 2099. To avoid this portability mess, new programs
166 should use strftime instead.