1ctime(3) Library Functions Manual ctime(3)
2
3
4
6 asctime, ctime, gmtime, localtime, mktime, asctime_r, ctime_r, gm‐
7 time_r, localtime_r - transform date and time to broken-down time or
8 ASCII
9
11 Standard C library (libc, -lc)
12
14 #include <time.h>
15
16 char *asctime(const struct tm *tm);
17 char *asctime_r(const struct tm *restrict tm,
18 char buf[restrict 26]);
19
20 char *ctime(const time_t *timep);
21 char *ctime_r(const time_t *restrict timep,
22 char buf[restrict 26]);
23
24 struct tm *gmtime(const time_t *timep);
25 struct tm *gmtime_r(const time_t *restrict timep,
26 struct tm *restrict result);
27
28 struct tm *localtime(const time_t *timep);
29 struct tm *localtime_r(const time_t *restrict timep,
30 struct tm *restrict result);
31
32 time_t mktime(struct tm *tm);
33
34 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
35
36 asctime_r(), ctime_r(), gmtime_r(), localtime_r():
37 _POSIX_C_SOURCE
38 || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
39
41 The ctime(), gmtime(), and localtime() functions all take an argument
42 of data type time_t, which represents calendar time. When interpreted
43 as an absolute time value, it represents the number of seconds elapsed
44 since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
45
46 The asctime() and mktime() functions both take an argument representing
47 broken-down time, which is a representation separated into year, month,
48 day, and so on.
49
50 Broken-down time is stored in the structure tm, described in tm(3type).
51
52 The call ctime(t) is equivalent to asctime(localtime(t)). It converts
53 the calendar time t into a null-terminated string of the form
54
55 "Wed Jun 30 21:49:08 1993\n"
56
57 The abbreviations for the days of the week are "Sun", "Mon", "Tue",
58 "Wed", "Thu", "Fri", and "Sat". The abbreviations for the months are
59 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
60 "Nov", and "Dec". The return value points to a statically allocated
61 string which might be overwritten by subsequent calls to any of the
62 date and time functions. The function also sets the external variables
63 tzname, timezone, and daylight (see tzset(3)) with information about
64 the current timezone. The reentrant version ctime_r() does the same,
65 but stores the string in a user-supplied buffer which should have room
66 for at least 26 bytes. It need not set tzname, timezone, and daylight.
67
68 The gmtime() function converts the calendar time timep to broken-down
69 time representation, expressed in Coordinated Universal Time (UTC). It
70 may return NULL when the year does not fit into an integer. The return
71 value points to a statically allocated struct which might be overwrit‐
72 ten by subsequent calls to any of the date and time functions. The gm‐
73 time_r() function does the same, but stores the data in a user-supplied
74 struct.
75
76 The localtime() function converts the calendar time timep to broken-
77 down time representation, expressed relative to the user's specified
78 timezone. The function acts as if it called tzset(3) and sets the ex‐
79 ternal variables tzname with information about the current timezone,
80 timezone with the difference between Coordinated Universal Time (UTC)
81 and local standard time in seconds, and daylight to a nonzero value if
82 daylight savings time rules apply during some part of the year. The
83 return value points to a statically allocated struct which might be
84 overwritten by subsequent calls to any of the date and time functions.
85 The localtime_r() function does the same, but stores the data in a
86 user-supplied struct. It need not set tzname, timezone, and daylight.
87
88 The asctime() function converts the broken-down time value tm into a
89 null-terminated string with the same format as ctime(). The return
90 value points to a statically allocated string which might be overwrit‐
91 ten by subsequent calls to any of the date and time functions. The as‐
92 ctime_r() function does the same, but stores the string in a user-sup‐
93 plied buffer which should have room for at least 26 bytes.
94
95 The mktime() function converts a broken-down time structure, expressed
96 as local time, to calendar time representation. The function ignores
97 the values supplied by the caller in the tm_wday and tm_yday fields.
98 The value specified in the tm_isdst field informs mktime() whether or
99 not daylight saving time (DST) is in effect for the time supplied in
100 the tm structure: a positive value means DST is in effect; zero means
101 that DST is not in effect; and a negative value means that mktime()
102 should (use timezone information and system databases to) attempt to
103 determine whether DST is in effect at the specified time.
104
105 The mktime() function modifies the fields of the tm structure as fol‐
106 lows: tm_wday and tm_yday are set to values determined from the con‐
107 tents of the other fields; if structure members are outside their valid
108 interval, they will be normalized (so that, for example, 40 October is
109 changed into 9 November); tm_isdst is set (regardless of its initial
110 value) to a positive value or to 0, respectively, to indicate whether
111 DST is or is not in effect at the specified time. Calling mktime()
112 also sets the external variable tzname with information about the cur‐
113 rent timezone.
114
115 If the specified broken-down time cannot be represented as calendar
116 time (seconds since the Epoch), mktime() returns (time_t) -1 and does
117 not alter the members of the broken-down time structure.
118
120 On success, gmtime() and localtime() return a pointer to a struct tm.
121
122 On success, gmtime_r() and localtime_r() return the address of the
123 structure pointed to by result.
124
125 On success, asctime() and ctime() return a pointer to a string.
126
127 On success, asctime_r() and ctime_r() return a pointer to the string
128 pointed to by buf.
129
130 On success, mktime() returns the calendar time (seconds since the
131 Epoch), expressed as a value of type time_t.
132
133 On error, mktime() returns the value (time_t) -1. The remaining func‐
134 tions return NULL on error. On error, errno is set to indicate the er‐
135 ror.
136
138 EOVERFLOW
139 The result cannot be represented.
140
142 For an explanation of the terms used in this section, see at‐
143 tributes(7).
144
145 ┌───────────────┬───────────────┬──────────────────────────────────────┐
146 │Interface │ Attribute │ Value │
147 ├───────────────┼───────────────┼──────────────────────────────────────┤
148 │asctime() │ Thread safety │ MT-Unsafe race:asctime locale │
149 ├───────────────┼───────────────┼──────────────────────────────────────┤
150 │asctime_r() │ Thread safety │ MT-Safe locale │
151 ├───────────────┼───────────────┼──────────────────────────────────────┤
152 │ctime() │ Thread safety │ MT-Unsafe race:tmbuf race:asctime │
153 │ │ │ env locale │
154 ├───────────────┼───────────────┼──────────────────────────────────────┤
155 │ctime_r(), │ Thread safety │ MT-Safe env locale │
156 │gmtime_r(), │ │ │
157 │localtime_r(), │ │ │
158 │mktime() │ │ │
159 ├───────────────┼───────────────┼──────────────────────────────────────┤
160 │gmtime(), │ Thread safety │ MT-Unsafe race:tmbuf env locale │
161 │localtime() │ │ │
162 └───────────────┴───────────────┴──────────────────────────────────────┘
163
165 POSIX doesn't specify the parameters of ctime_r() to be restrict; that
166 is specific to glibc.
167
168 In many implementations, including glibc, a 0 in tm_mday is interpreted
169 as meaning the last day of the preceding month.
170
171 According to POSIX.1-2001, localtime() is required to behave as though
172 tzset(3) was called, while localtime_r() does not have this
173 requirement. For portable code, tzset(3) should be called before
174 localtime_r().
175
177 asctime()
178 ctime()
179 gmtime()
180 localtime()
181 mktime()
182 C11, POSIX.1-2008.
183
184 asctime_r()
185 ctime_r()
186 gmtime_r()
187 localtime_r()
188 POSIX.1-2008.
189
191 gmtime()
192 localtime()
193 mktime()
194 C89, POSIX.1-2001.
195
196 asctime()
197 ctime()
198 C89, POSIX.1-2001. Marked obsolete in POSIX.1-2008
199 (recommending strftime(3)).
200
201 gmtime_r()
202 localtime_r()
203 POSIX.1-2001.
204
205 asctime_r()
206 ctime_r()
207 POSIX.1-2001. Marked obsolete in POSIX.1-2008 (recommending
208 strftime(3)).
209
211 The four functions asctime(), ctime(), gmtime(), and localtime() return
212 a pointer to static data and hence are not thread-safe. The thread-
213 safe versions, asctime_r(), ctime_r(), gmtime_r(), and localtime_r(),
214 are specified by SUSv2.
215
216 POSIX.1-2001 says: "The asctime(), ctime(), gmtime(), and localtime()
217 functions shall return values in one of two static objects: a broken-
218 down time structure and an array of type char. Execution of any of the
219 functions may overwrite the information returned in either of these
220 objects by any of the other functions." This can occur in the glibc
221 implementation.
222
224 date(1), gettimeofday(2), time(2), utime(2), clock(3), difftime(3),
225 strftime(3), strptime(3), timegm(3), tzset(3), time(7)
226
227
228
229Linux man-pages 6.05 2023-07-20 ctime(3)