]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/ctime.3
Many pages: Use correct letter case in page titles (TH)
[thirdparty/man-pages.git] / man3 / ctime.3
CommitLineData
fea681da
MK
1.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2.\"
5fbde956 3.\" SPDX-License-Identifier: Linux-man-pages-copyleft
fea681da
MK
4.\"
5.\" References consulted:
6.\" Linux libc source code
7.\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
8.\" 386BSD man pages
9.\" Modified Sat Jul 24 19:49:27 1993 by Rik Faith (faith@cs.unc.edu)
10.\" Modified Fri Apr 26 12:38:55 MET DST 1996 by Martin Schulze (joey@linux.de)
11.\" Modified 2001-11-13, aeb
12.\" Modified 2001-12-13, joey, aeb
3e53c142 13.\" Modified 2004-11-16, mtk
fea681da 14.\"
4c1c5274 15.TH ctime 3 (date) "Linux man-pages (unreleased)"
fea681da
MK
16.SH NAME
17asctime, ctime, gmtime, localtime, mktime, asctime_r, ctime_r, gmtime_r,
18localtime_r \- transform date and time to broken-down time or ASCII
b813014f
AC
19.SH LIBRARY
20Standard C library
21.RI ( libc ", " \-lc )
fea681da
MK
22.SH SYNOPSIS
23.nf
24.B #include <time.h>
68e4db0a 25.PP
fea681da 26.BI "char *asctime(const struct tm *" tm );
bd99e6b7
AC
27.BI "char *asctime_r(const struct tm *restrict " tm ,
28.BI " char " buf "[restrict 26]);"
68e4db0a 29.PP
fea681da 30.BI "char *ctime(const time_t *" timep );
bd99e6b7
AC
31.BI "char *ctime_r(const time_t *restrict " timep ,
32.BI " char " buf "[restrict 26]);"
68e4db0a 33.PP
fea681da 34.BI "struct tm *gmtime(const time_t *" timep );
cd7ebdf9
AC
35.BI "struct tm *gmtime_r(const time_t *restrict " timep ,
36.BI " struct tm *restrict " result );
68e4db0a 37.PP
fea681da 38.BI "struct tm *localtime(const time_t *" timep );
cd7ebdf9
AC
39.BI "struct tm *localtime_r(const time_t *restrict " timep ,
40.BI " struct tm *restrict " result );
68e4db0a 41.PP
fea681da
MK
42.BI "time_t mktime(struct tm *" tm );
43.fi
68e4db0a 44.PP
d39ad78f 45.RS -4
cc4615cc
MK
46Feature Test Macro Requirements for glibc (see
47.BR feature_test_macros (7)):
d39ad78f 48.RE
68e4db0a 49.PP
cc4615cc
MK
50.BR asctime_r (),
51.BR ctime_r (),
52.BR gmtime_r (),
53.BR localtime_r ():
9d2adbae
MK
54.nf
55 _POSIX_C_SOURCE
56 || /* Glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
57.fi
fea681da 58.SH DESCRIPTION
60a90ecd
MK
59The
60.BR ctime (),
d556548b 61.BR gmtime (),
60a90ecd
MK
62and
63.BR localtime ()
64functions all take
213d2580 65an argument of data type \fItime_t\fP, which represents calendar time.
fea681da 66When interpreted as an absolute time value, it represents the number of
f49c451a 67seconds elapsed since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
fea681da 68.PP
60a90ecd
MK
69The
70.BR asctime ()
71and
72.BR mktime ()
73functions both take an argument
213d2580 74representing broken-down time, which is a representation
1c2816e2 75separated into year, month, day, and so on.
fea681da 76.PP
cf96188b
AC
77Broken-down time is stored in the structure
78.IR tm ,
79described in
80.BR tm (3type).
fea681da
MK
81.PP
82The call
83.BI ctime( t )
84is equivalent to
85.BI asctime(localtime( t )) \fR.
0e8fe3b4
MK
86It converts the calendar time \fIt\fP into a
87null-terminated string of the form
ba39b288
MK
88.PP
89.in +4n
90.EX
d1a71985 91"Wed Jun 30 21:49:08 1993\en"
ba39b288 92.EE
a202ed93 93.in
ba39b288 94.PP
84c517a4
MK
95The abbreviations for the days of the week are "Sun", "Mon", "Tue", "Wed",
96"Thu", "Fri", and "Sat".
97The abbreviations for the months are "Jan",
98"Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", and
99"Dec".
c13182ef 100The return value points to a statically allocated string which
fea681da 101might be overwritten by subsequent calls to any of the date and time
c13182ef 102functions.
546f67f3
MK
103The function also sets the external
104variables \fItzname\fP, \fItimezone\fP, and \fIdaylight\fP (see
fea681da 105.BR tzset (3))
5b0dc1ba 106with information about the current timezone.
da27f178 107The reentrant version
60a90ecd
MK
108.BR ctime_r ()
109does the same, but stores the
ef38dda0
MK
110string in a user-supplied buffer
111which should have room for at least 26 bytes.
c13182ef 112It need not
546f67f3 113set \fItzname\fP, \fItimezone\fP, and \fIdaylight\fP.
fea681da 114.PP
60a90ecd
MK
115The
116.BR gmtime ()
117function converts the calendar time \fItimep\fP to
fea681da 118broken-down time representation, expressed in Coordinated Universal Time
c13182ef
MK
119(UTC).
120It may return NULL when the year does not fit into an integer.
fea681da
MK
121The return value points to a statically allocated struct which might be
122overwritten by subsequent calls to any of the date and time functions.
60a90ecd
MK
123The
124.BR gmtime_r ()
125function does the same, but stores the data in a
fea681da
MK
126user-supplied struct.
127.PP
60a90ecd
MK
128The
129.BR localtime ()
130function converts the calendar time \fItimep\fP to
5f14486c
MK
131broken-down time representation,
132expressed relative to the user's specified timezone.
c13182ef 133The function acts as if it called
fea681da 134.BR tzset (3)
c13182ef 135and sets the external variables \fItzname\fP with
5b0dc1ba 136information about the current timezone, \fItimezone\fP with the difference
fea681da 137between Coordinated Universal Time (UTC) and local standard time in
c7094399 138seconds, and \fIdaylight\fP to a nonzero value if daylight savings
fea681da
MK
139time rules apply during some part of the year.
140The return value points to a statically allocated struct which might be
141overwritten by subsequent calls to any of the date and time functions.
60a90ecd
MK
142The
143.BR localtime_r ()
144function does the same, but stores the data in a
c13182ef 145user-supplied struct.
546f67f3 146It need not set \fItzname\fP, \fItimezone\fP, and \fIdaylight\fP.
fea681da 147.PP
60a90ecd
MK
148The
149.BR asctime ()
150function converts the broken-down time value
0e8fe3b4 151\fItm\fP into a null-terminated string with the same format as
60a90ecd 152.BR ctime ().
c13182ef 153The return value points to a statically allocated string which might be
fea681da 154overwritten by subsequent calls to any of the date and time functions.
60a90ecd
MK
155The
156.BR asctime_r ()
157function does the same, but stores the string in
ef38dda0 158a user-supplied buffer which should have room for at least 26 bytes.
fea681da 159.PP
60a90ecd
MK
160The
161.BR mktime ()
162function converts a broken-down time structure, expressed
c13182ef
MK
163as local time, to calendar time representation.
164The function ignores
9ef659a9
MK
165the values supplied by the caller in the
166.I tm_wday
167and
168.I tm_yday
169fields.
f9db4400
MK
170The value specified in the
171.I tm_isdst
172field informs
173.BR mktime ()
174whether or not daylight saving time (DST)
175is in effect for the time supplied in the
176.I tm
177structure:
178a positive value means DST is in effect;
179zero means that DST is not in effect;
180and a negative value means that
9ef659a9 181.BR mktime ()
f9db4400
MK
182should (use timezone information and system databases to)
183attempt to determine whether DST is in effect at the specified time.
847e0d88 184.PP
f9db4400
MK
185The
186.BR mktime ()
187function modifies the fields of the
1ae6b2c7 188.I tm
9ef659a9
MK
189structure as follows:
190.I tm_wday
191and
192.I tm_yday
193are set to values determined from the contents of the other fields;
194if structure members are outside their valid interval, they will be
f9db4400
MK
195normalized (so that, for example, 40 October is changed into 9 November);
196.I tm_isdst
197is set (regardless of its initial value)
198to a positive value or to 0, respectively,
acddbaea 199to indicate whether DST is or is not in effect at the specified time.
60a90ecd
MK
200Calling
201.BR mktime ()
202also sets the external variable \fItzname\fP with
5b0dc1ba 203information about the current timezone.
847e0d88 204.PP
c13182ef 205If the specified broken-down
be9634cf 206time cannot be represented as calendar time (seconds since the Epoch),
60a90ecd 207.BR mktime ()
ab3206e7 208returns
009df872 209.I (time_t)\ \-1
7d2cb9d5 210and does not alter the
f9db4400 211members of the broken-down time structure.
47297adb 212.SH RETURN VALUE
f5633e8d
MK
213On success,
214.BR gmtime ()
215and
216.BR localtime ()
217return a pointer to a
218.IR "struct\ tm" .
847e0d88 219.PP
f5633e8d
MK
220On success,
221.BR gmtime_r ()
222and
223.BR localtime_r ()
224return the address of the structure pointed to by
225.IR result .
847e0d88 226.PP
f5633e8d
MK
227On success,
228.BR asctime ()
229and
230.BR ctime ()
231return a pointer to a string.
847e0d88 232.PP
f5633e8d
MK
233On success,
234.BR asctime_r ()
235and
236.BR ctime_r ()
237return a pointer to the string pointed to by
238.IR buf .
847e0d88 239.PP
f5633e8d
MK
240On success,
241.BR mktime ()
d957e9a4
MK
242returns the calendar time (seconds since the Epoch),
243expressed as a value of type
f5633e8d 244.IR time_t .
847e0d88 245.PP
f5633e8d
MK
246On error,
247.BR mktime ()
248returns the value
0a23e9aa 249.IR "(time_t)\ \-1" .
f5633e8d
MK
250The remaining functions return NULL on error.
251On error,
252.I errno
855d489a 253is set to indicate the error.
d957e9a4
MK
254.SH ERRORS
255.TP
256.B EOVERFLOW
1b9d5819 257The result cannot be represented.
c8b68136
ZL
258.SH ATTRIBUTES
259For an explanation of the terms used in this section, see
260.BR attributes (7).
74714ea8 261.ad l
c466875e 262.nh
c8b68136
ZL
263.TS
264allbox;
b32feea5 265lb lb lbx
c8b68136
ZL
266l l l.
267Interface Attribute Value
268T{
269.BR asctime ()
b32feea5
MK
270T} Thread safety T{
271MT-Unsafe race:asctime locale
272T}
c8b68136
ZL
273T{
274.BR asctime_r ()
b32feea5
MK
275T} Thread safety T{
276MT-Safe locale
277T}
c8b68136
ZL
278T{
279.BR ctime ()
280T} Thread safety T{
281MT-Unsafe race:tmbuf
c8b68136
ZL
282race:asctime env locale
283T}
284T{
285.BR ctime_r (),
286.BR gmtime_r (),
287.BR localtime_r (),
288.BR mktime ()
b32feea5
MK
289T} Thread safety T{
290MT-Safe env locale
291T}
c8b68136
ZL
292T{
293.BR gmtime (),
294.BR localtime ()
b32feea5
MK
295T} Thread safety T{
296MT-Unsafe race:tmbuf env locale
297T}
c8b68136 298.TE
c466875e 299.hy
74714ea8 300.ad
c466875e 301.sp 1
3113c7f3 302.SH STANDARDS
2b2581ee
MK
303POSIX.1-2001.
304C89 and C99 specify
305.BR asctime (),
306.BR ctime (),
307.BR gmtime (),
308.BR localtime (),
309and
44a2c328 310.BR mktime ().
ca5711a5
MK
311POSIX.1-2008 marks
312.BR asctime (),
313.BR asctime_r (),
314.BR ctime (),
315and
316.BR ctime_r ()
9e36b352
MK
317as obsolete,
318recommending the use of
319.BR strftime (3)
320instead.
0300d62f
AC
321.PP
322POSIX doesn't specify the parameters of
323.BR ctime_r ()
324to be
325.IR restrict ;
326that is specific to glibc.
fea681da
MK
327.SH NOTES
328The four functions
63aa9df0
MK
329.BR asctime (),
330.BR ctime (),
d556548b 331.BR gmtime (),
fea681da 332and
63aa9df0 333.BR localtime ()
fea681da 334return a pointer to static data and hence are not thread-safe.
800a076a 335The thread-safe versions,
63aa9df0
MK
336.BR asctime_r (),
337.BR ctime_r (),
d556548b 338.BR gmtime_r (),
fea681da 339and
800a076a
MK
340.BR localtime_r (),
341are specified by SUSv2.
847e0d88 342.PP
8167059b
MK
343POSIX.1-2001 says:
344"The
345.BR asctime (),
346.BR ctime (),
347.BR gmtime (),
348and
349.BR localtime ()
350functions shall return values in one of two static objects:
351a broken-down time structure and an array of type
352.IR char .
353Execution of any of the functions may overwrite the information returned
354in either of these objects by any of the other functions."
355This can occur in the glibc implementation.
dd3568a1 356.PP
8167059b 357In many implementations, including glibc, a 0 in
3e53c142
MK
358.I tm_mday
359is interpreted as meaning the last day of the preceding month.
dd3568a1 360.PP
259ce44e 361According to POSIX.1-2001,
7b16ff49
MK
362.BR localtime ()
363is required to behave as though
0b80cf56 364.BR tzset (3)
7b16ff49
MK
365was called, while
366.BR localtime_r ()
367does not have this requirement.
368.\" See http://thread.gmane.org/gmane.comp.time.tz/2034/
68fc27f7 369For portable code,
0b80cf56 370.BR tzset (3)
7b16ff49
MK
371should be called before
372.BR localtime_r ().
47297adb 373.SH SEE ALSO
fea681da
MK
374.BR date (1),
375.BR gettimeofday (2),
376.BR time (2),
377.BR utime (2),
378.BR clock (3),
379.BR difftime (3),
fea681da
MK
380.BR strftime (3),
381.BR strptime (3),
7877f012 382.BR timegm (3),
eafd5ce1
MK
383.BR tzset (3),
384.BR time (7)