1ASCTIME(3P) POSIX Programmer's Manual ASCTIME(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
11
13 asctime, asctime_r — convert date and time to a string
14
16 #include <time.h>
17
18 char *asctime(const struct tm *timeptr);
19 char *asctime_r(const struct tm *restrict tm, char *restrict buf);
20
22 For asctime(): The functionality described on this reference page is
23 aligned with the ISO C standard. Any conflict between the requirements
24 described here and the ISO C standard is unintentional. This volume of
25 POSIX.1‐2008 defers to the ISO C standard.
26
27 The asctime() function shall convert the broken-down time in the struc‐
28 ture pointed to by timeptr into a string in the form:
29
30 Sun Sep 16 01:03:52 1973\n\0
31
32 using the equivalent of the following algorithm:
33
34 char *asctime(const struct tm *timeptr)
35 {
36 static char wday_name[7][3] = {
37 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
38 };
39 static char mon_name[12][3] = {
40 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
41 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
42 };
43 static char result[26];
44
45 sprintf(result, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n",
46 wday_name[timeptr->tm_wday],
47 mon_name[timeptr->tm_mon],
48 timeptr->tm_mday, timeptr->tm_hour,
49 timeptr->tm_min, timeptr->tm_sec,
50 1900 + timeptr->tm_year);
51 return result;
52 }
53
54 However, the behavior is undefined if timeptr->tm_wday or
55 timeptr->tm_mon are not within the normal ranges as defined in
56 <time.h>, or if timeptr->tm_year exceeds {INT_MAX}−1990, or if the
57 above algorithm would attempt to generate more than 26 bytes of output
58 (including the terminating null).
59
60 The tm structure is defined in the <time.h> header.
61
62 The asctime(), ctime(), gmtime(), and localtime() functions shall
63 return values in one of two static objects: a broken-down time struc‐
64 ture and an array of type char. Execution of any of the functions may
65 overwrite the information returned in either of these objects by any of
66 the other functions.
67
68 The asctime() function need not be thread-safe.
69
70 The asctime_r() function shall convert the broken-down time in the
71 structure pointed to by tm into a string (of the same form as that
72 returned by asctime(), and with the same undefined behavior when input
73 or output is out of range) that is placed in the user-supplied buffer
74 pointed to by buf (which shall contain at least 26 bytes) and then
75 return buf.
76
78 Upon successful completion, asctime() shall return a pointer to the
79 string. If the function is unsuccessful, it shall return NULL.
80
81 Upon successful completion, asctime_r() shall return a pointer to a
82 character string containing the date and time. This string is pointed
83 to by the argument buf. If the function is unsuccessful, it shall
84 return NULL.
85
87 No errors are defined.
88
89 The following sections are informative.
90
92 None.
93
95 These functions are included only for compatibility with older imple‐
96 mentations. They have undefined behavior if the resulting string would
97 be too long, so the use of these functions should be discouraged. On
98 implementations that do not detect output string length overflow, it is
99 possible to overflow the output buffers in such a way as to cause
100 applications to fail, or possible system security violations. Also,
101 these functions do not support localized date and time formats. To
102 avoid these problems, applications should use strftime() to generate
103 strings from broken-down times.
104
105 Values for the broken-down time structure can be obtained by calling
106 gmtime() or localtime().
107
108 The asctime_r() function is thread-safe and shall return values in a
109 user-supplied buffer instead of possibly using a static data area that
110 may be overwritten by each call.
111
113 The standard developers decided to mark the asctime() and asctime_r()
114 functions obsolescent even though asctime() is in the ISO C standard
115 due to the possibility of buffer overflow. The ISO C standard also pro‐
116 vides the strftime() function which can be used to avoid these prob‐
117 lems.
118
120 These functions may be removed in a future version.
121
123 clock(), ctime(), difftime(), gmtime(), localtime(), mktime(), strf‐
124 time(), strptime(), time(), utime()
125
126 The Base Definitions volume of POSIX.1‐2008, <time.h>
127
129 Portions of this text are reprinted and reproduced in electronic form
130 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
131 -- Portable Operating System Interface (POSIX), The Open Group Base
132 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
133 cal and Electronics Engineers, Inc and The Open Group. (This is
134 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
135 event of any discrepancy between this version and the original IEEE and
136 The Open Group Standard, the original IEEE and The Open Group Standard
137 is the referee document. The original Standard can be obtained online
138 at http://www.unix.org/online.html .
139
140 Any typographical or formatting errors that appear in this page are
141 most likely to have been introduced during the conversion of the source
142 files to man page format. To report such errors, see https://www.ker‐
143 nel.org/doc/man-pages/reporting_bugs.html .
144
145
146
147IEEE/The Open Group 2013 ASCTIME(3P)