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