]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/ctime.3
_syscall.2, capget.2, ioprio_set.2, semop.2, send.2, ctime.3, dysize.3, fenv.3, fmtms...
[thirdparty/man-pages.git] / man3 / ctime.3
1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .\" References consulted:
26 .\" Linux libc source code
27 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
28 .\" 386BSD man pages
29 .\" Modified Sat Jul 24 19:49:27 1993 by Rik Faith (faith@cs.unc.edu)
30 .\" Modified Fri Apr 26 12:38:55 MET DST 1996 by Martin Schulze (joey@linux.de)
31 .\" Modified 2001-11-13, aeb
32 .\" Modified 2001-12-13, joey, aeb
33 .\" Modified 2004-11-16, mtk
34 .\"
35 .TH CTIME 3 2016-12-12 "" "Linux Programmer's Manual"
36 .SH NAME
37 asctime, ctime, gmtime, localtime, mktime, asctime_r, ctime_r, gmtime_r,
38 localtime_r \- transform date and time to broken-down time or ASCII
39 .SH SYNOPSIS
40 .nf
41 .B #include <time.h>
42 .PP
43 .BI "char *asctime(const struct tm *" tm );
44 .br
45 .BI "char *asctime_r(const struct tm *" tm ", char *" buf );
46 .PP
47 .BI "char *ctime(const time_t *" timep );
48 .br
49 .BI "char *ctime_r(const time_t *" timep ", char *" buf );
50 .PP
51 .BI "struct tm *gmtime(const time_t *" timep );
52 .br
53 .BI "struct tm *gmtime_r(const time_t *" timep ", struct tm *" result );
54 .PP
55 .BI "struct tm *localtime(const time_t *" timep );
56 .br
57 .BI "struct tm *localtime_r(const time_t *" timep ", struct tm *" result );
58 .PP
59 .BI "time_t mktime(struct tm *" tm );
60 .fi
61 .PP
62 .in -4n
63 Feature Test Macro Requirements for glibc (see
64 .BR feature_test_macros (7)):
65 .in
66 .ad l
67 .PP
68 .BR asctime_r (),
69 .BR ctime_r (),
70 .BR gmtime_r (),
71 .BR localtime_r ():
72 .RS
73 _POSIX_C_SOURCE
74 || /* Glibc versions <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
75 .RE
76 .ad
77 .SH DESCRIPTION
78 The
79 .BR ctime (),
80 .BR gmtime ()
81 and
82 .BR localtime ()
83 functions all take
84 an argument of data type \fItime_t\fP, which represents calendar time.
85 When interpreted as an absolute time value, it represents the number of
86 seconds elapsed since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
87 .PP
88 The
89 .BR asctime ()
90 and
91 .BR mktime ()
92 functions both take an argument
93 representing broken-down time, which is a representation
94 separated into year, month, day, and so on.
95 .PP
96 Broken-down time is stored
97 in the structure \fItm\fP, which is defined in \fI<time.h>\fP as follows:
98 .PP
99 .in +4n
100 .EX
101 struct tm {
102 int tm_sec; /* Seconds (0-60) */
103 int tm_min; /* Minutes (0-59) */
104 int tm_hour; /* Hours (0-23) */
105 int tm_mday; /* Day of the month (1-31) */
106 int tm_mon; /* Month (0-11) */
107 int tm_year; /* Year - 1900 */
108 int tm_wday; /* Day of the week (0-6, Sunday = 0) */
109 int tm_yday; /* Day in the year (0-365, 1 Jan = 0) */
110 int tm_isdst; /* Daylight saving time */
111 };
112 .EE
113 .in
114 .PP
115 The members of the \fItm\fP structure are:
116 .TP 10
117 .I tm_sec
118 The number of seconds after the minute, normally in the range 0 to 59,
119 but can be up to 60 to allow for leap seconds.
120 .TP
121 .I tm_min
122 The number of minutes after the hour, in the range 0 to 59.
123 .TP
124 .I tm_hour
125 The number of hours past midnight, in the range 0 to 23.
126 .TP
127 .I tm_mday
128 The day of the month, in the range 1 to 31.
129 .TP
130 .I tm_mon
131 The number of months since January, in the range 0 to 11.
132 .TP
133 .I tm_year
134 The number of years since 1900.
135 .TP
136 .I tm_wday
137 The number of days since Sunday, in the range 0 to 6.
138 .TP
139 .I tm_yday
140 The number of days since January 1, in the range 0 to 365.
141 .TP
142 .I tm_isdst
143 A flag that indicates whether daylight saving time is in effect at the
144 time described.
145 The value is positive if daylight saving time is in
146 effect, zero if it is not, and negative if the information is not
147 available.
148 .PP
149 The call
150 .BI ctime( t )
151 is equivalent to
152 .BI asctime(localtime( t )) \fR.
153 It converts the calendar time \fIt\fP into a
154 null-terminated string of the form
155 .PP
156 .in +4n
157 .EX
158 .RS
159 "Wed Jun 30 21:49:08 1993\\n"
160 .EE
161 ,in
162 .PP
163 The abbreviations for the days of the week are "Sun", "Mon", "Tue", "Wed",
164 "Thu", "Fri", and "Sat".
165 The abbreviations for the months are "Jan",
166 "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", and
167 "Dec".
168 The return value points to a statically allocated string which
169 might be overwritten by subsequent calls to any of the date and time
170 functions.
171 The function also sets the external
172 variables \fItzname\fP, \fItimezone\fP, and \fIdaylight\fP (see
173 .BR tzset (3))
174 with information about the current timezone.
175 The reentrant version
176 .BR ctime_r ()
177 does the same, but stores the
178 string in a user-supplied buffer
179 which should have room for at least 26 bytes.
180 It need not
181 set \fItzname\fP, \fItimezone\fP, and \fIdaylight\fP.
182 .PP
183 The
184 .BR gmtime ()
185 function converts the calendar time \fItimep\fP to
186 broken-down time representation, expressed in Coordinated Universal Time
187 (UTC).
188 It may return NULL when the year does not fit into an integer.
189 The return value points to a statically allocated struct which might be
190 overwritten by subsequent calls to any of the date and time functions.
191 The
192 .BR gmtime_r ()
193 function does the same, but stores the data in a
194 user-supplied struct.
195 .PP
196 The
197 .BR localtime ()
198 function converts the calendar time \fItimep\fP to
199 broken-down time representation,
200 expressed relative to the user's specified timezone.
201 The function acts as if it called
202 .BR tzset (3)
203 and sets the external variables \fItzname\fP with
204 information about the current timezone, \fItimezone\fP with the difference
205 between Coordinated Universal Time (UTC) and local standard time in
206 seconds, and \fIdaylight\fP to a nonzero value if daylight savings
207 time rules apply during some part of the year.
208 The return value points to a statically allocated struct which might be
209 overwritten by subsequent calls to any of the date and time functions.
210 The
211 .BR localtime_r ()
212 function does the same, but stores the data in a
213 user-supplied struct.
214 It need not set \fItzname\fP, \fItimezone\fP, and \fIdaylight\fP.
215 .PP
216 The
217 .BR asctime ()
218 function converts the broken-down time value
219 \fItm\fP into a null-terminated string with the same format as
220 .BR ctime ().
221 The return value points to a statically allocated string which might be
222 overwritten by subsequent calls to any of the date and time functions.
223 The
224 .BR asctime_r ()
225 function does the same, but stores the string in
226 a user-supplied buffer which should have room for at least 26 bytes.
227 .PP
228 The
229 .BR mktime ()
230 function converts a broken-down time structure, expressed
231 as local time, to calendar time representation.
232 The function ignores
233 the values supplied by the caller in the
234 .I tm_wday
235 and
236 .I tm_yday
237 fields.
238 The value specified in the
239 .I tm_isdst
240 field informs
241 .BR mktime ()
242 whether or not daylight saving time (DST)
243 is in effect for the time supplied in the
244 .I tm
245 structure:
246 a positive value means DST is in effect;
247 zero means that DST is not in effect;
248 and a negative value means that
249 .BR mktime ()
250 should (use timezone information and system databases to)
251 attempt to determine whether DST is in effect at the specified time.
252 .PP
253 The
254 .BR mktime ()
255 function modifies the fields of the
256 .IR tm
257 structure as follows:
258 .I tm_wday
259 and
260 .I tm_yday
261 are set to values determined from the contents of the other fields;
262 if structure members are outside their valid interval, they will be
263 normalized (so that, for example, 40 October is changed into 9 November);
264 .I tm_isdst
265 is set (regardless of its initial value)
266 to a positive value or to 0, respectively,
267 to indicate whether DST is or is not in effect at the specified time.
268 Calling
269 .BR mktime ()
270 also sets the external variable \fItzname\fP with
271 information about the current timezone.
272 .PP
273 If the specified broken-down
274 time cannot be represented as calendar time (seconds since the Epoch),
275 .BR mktime ()
276 returns
277 .I (time_t)\ \-1
278 and does not alter the
279 members of the broken-down time structure.
280 .SH RETURN VALUE
281 On success,
282 .BR gmtime ()
283 and
284 .BR localtime ()
285 return a pointer to a
286 .IR "struct\ tm" .
287 .PP
288 On success,
289 .BR gmtime_r ()
290 and
291 .BR localtime_r ()
292 return the address of the structure pointed to by
293 .IR result .
294 .PP
295 On success,
296 .BR asctime ()
297 and
298 .BR ctime ()
299 return a pointer to a string.
300 .PP
301 On success,
302 .BR asctime_r ()
303 and
304 .BR ctime_r ()
305 return a pointer to the string pointed to by
306 .IR buf .
307 .PP
308 On success,
309 .BR mktime ()
310 returns the calendar time (seconds since the Epoch),
311 expressed as a value of type
312 .IR time_t .
313 .PP
314 On error,
315 .BR mktime ()
316 returns the value
317 .IR "(time_t)\ -1" .
318 The remaining functions return NULL on error.
319 On error,
320 .I errno
321 is set to indicate the cause of the error.
322 .SH ERRORS
323 .TP
324 .B EOVERFLOW
325 The result cannot be represented.
326 .SH ATTRIBUTES
327 For an explanation of the terms used in this section, see
328 .BR attributes (7).
329 .ad l
330 .TS
331 allbox;
332 lbw14 lb lbw31
333 l l l.
334 Interface Attribute Value
335 T{
336 .BR asctime ()
337 T} Thread safety MT-Unsafe race:asctime locale
338 T{
339 .BR asctime_r ()
340 T} Thread safety MT-Safe locale
341 T{
342 .BR ctime ()
343 T} Thread safety T{
344 MT-Unsafe race:tmbuf
345 .br
346 race:asctime env locale
347 T}
348 T{
349 .BR ctime_r (),
350 .BR gmtime_r (),
351 .BR localtime_r (),
352 .BR mktime ()
353 T} Thread safety MT-Safe env locale
354 T{
355 .BR gmtime (),
356 .BR localtime ()
357 T} Thread safety MT-Unsafe race:tmbuf env locale
358 .TE
359 .ad
360 .SH CONFORMING TO
361 POSIX.1-2001.
362 C89 and C99 specify
363 .BR asctime (),
364 .BR ctime (),
365 .BR gmtime (),
366 .BR localtime (),
367 and
368 .BR mktime ().
369 POSIX.1-2008 marks
370 .BR asctime (),
371 .BR asctime_r (),
372 .BR ctime (),
373 and
374 .BR ctime_r ()
375 as obsolete,
376 recommending the use of
377 .BR strftime (3)
378 instead.
379 .SH NOTES
380 The four functions
381 .BR asctime (),
382 .BR ctime (),
383 .BR gmtime ()
384 and
385 .BR localtime ()
386 return a pointer to static data and hence are not thread-safe.
387 The thread-safe versions,
388 .BR asctime_r (),
389 .BR ctime_r (),
390 .BR gmtime_r ()
391 and
392 .BR localtime_r (),
393 are specified by SUSv2.
394 .PP
395 POSIX.1-2001 says:
396 "The
397 .BR asctime (),
398 .BR ctime (),
399 .BR gmtime (),
400 and
401 .BR localtime ()
402 functions shall return values in one of two static objects:
403 a broken-down time structure and an array of type
404 .IR char .
405 Execution of any of the functions may overwrite the information returned
406 in either of these objects by any of the other functions."
407 This can occur in the glibc implementation.
408 .LP
409 In many implementations, including glibc, a 0 in
410 .I tm_mday
411 is interpreted as meaning the last day of the preceding month.
412 .LP
413 The glibc version of \fIstruct tm\fP has additional fields
414 .PP
415 .in +4n
416 .EX
417 const char *tm_zone; /* Timezone abbreviation */
418 .EE
419 .in
420 .PP
421 defined when
422 .B _BSD_SOURCE
423 was set before including
424 .IR <time.h> .
425 This is a BSD extension, present in 4.3BSD-Reno.
426 .PP
427 According to POSIX.1-2004,
428 .BR localtime ()
429 is required to behave as though
430 .BR tzset (3)
431 was called, while
432 .BR localtime_r ()
433 does not have this requirement.
434 .\" See http://thread.gmane.org/gmane.comp.time.tz/2034/
435 For portable code,
436 .BR tzset (3)
437 should be called before
438 .BR localtime_r ().
439 .SH SEE ALSO
440 .BR date (1),
441 .BR gettimeofday (2),
442 .BR time (2),
443 .BR utime (2),
444 .BR clock (3),
445 .BR difftime (3),
446 .BR strftime (3),
447 .BR strptime (3),
448 .BR timegm (3),
449 .BR tzset (3),
450 .BR time (7)