1GETDATE(3P) POSIX Programmer's Manual GETDATE(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
12 getdate - convert user format date and time
13
15 #include <time.h>
16
17 struct tm *getdate(const char *string);
18
19
21 The getdate() function shall convert a string representation of a date
22 or time into a broken-down time.
23
24 The external variable or macro getdate_err is used by getdate() to
25 return error values.
26
27 Templates are used to parse and interpret the input string. The tem‐
28 plates are contained in a text file identified by the environment vari‐
29 able DATEMSK. The DATEMSK variable should be set to indicate the full
30 pathname of the file that contains the templates. The first line in the
31 template that matches the input specification is used for interpreta‐
32 tion and conversion into the internal time format.
33
34 The following conversion specifications shall be supported:
35
36 %% Equivalent to % .
37
38 %a Abbreviated weekday name.
39
40 %A Full weekday name.
41
42 %b Abbreviated month name.
43
44 %B Full month name.
45
46 %c Locale's appropriate date and time representation.
47
48 %C Century number [00,99]; leading zeros are permitted but not
49 required.
50
51 %d Day of month [01,31]; the leading 0 is optional.
52
53 %D Date as %m / %d / %y .
54
55 %e Equivalent to %d .
56
57 %h Abbreviated month name.
58
59 %H Hour [00,23].
60
61 %I Hour [01,12].
62
63 %m Month number [01,12].
64
65 %M Minute [00,59].
66
67 %n Equivalent to <newline>.
68
69 %p Locale's equivalent of either AM or PM.
70
71 %r The locale's appropriate representation of time in AM and PM
72 notation. In the POSIX locale, this shall be equivalent to %I :
73 %M : %S %p .
74
75 %R Time as %H : %M .
76
77 %S Seconds [00,60]. The range goes to 60 (rather than stopping at
78 59) to allow positive leap seconds to be expressed. Since leap
79 seconds cannot be predicted by any algorithm, leap second data
80 must come from some external source.
81
82 %t Equivalent to <tab>.
83
84 %T Time as %H : %M : %S .
85
86 %w Weekday number (Sunday = [0,6]).
87
88 %x Locale's appropriate date representation.
89
90 %X Locale's appropriate time representation.
91
92 %y Year within century. When a century is not otherwise specified,
93 values in the range [69,99] shall refer to years 1969 to 1999
94 inclusive, and values in the range [00,68] shall refer to years
95 2000 to 2068 inclusive.
96
97 Note:
98 It is expected that in a future version of IEEE Std 1003.1-2001
99 the default century inferred from a 2-digit year will change.
100 (This would apply to all commands accepting a 2-digit year as
101 input.)
102
103
104 %Y Year as "ccyy" (for example, 2001).
105
106 %Z Timezone name or no characters if no timezone exists. If the
107 timezone supplied by %Z is not the timezone that getdate()
108 expects, an invalid input specification error shall result. The
109 getdate() function calculates an expected timezone based on
110 information supplied to the function (such as the hour, day, and
111 month).
112
113
114 The match between the template and input specification performed by
115 getdate() shall be case-insensitive.
116
117 The month and weekday names can consist of any combination of upper and
118 lowercase letters. The process can request that the input date or time
119 specification be in a specific language by setting the LC_TIME category
120 (see setlocale()).
121
122 Leading zeros are not necessary for the descriptors that allow leading
123 zeros. However, at most two digits are allowed for those descriptors,
124 including leading zeros. Extra whitespace in either the template file
125 or in string shall be ignored.
126
127 The results are undefined if the conversion specifications %c, %x, and
128 %X include unsupported conversion specifications.
129
130 The following rules apply for converting the input specification into
131 the internal format:
132
133 * If %Z is being scanned, then getdate() shall initialize the broken-
134 down time to be the current time in the scanned timezone. Otherwise,
135 it shall initialize the broken-down time based on the current local
136 time as if localtime() had been called.
137
138 * If only the weekday is given, the day chosen shall be the day,
139 starting with today and moving into the future, which first matches
140 the named day.
141
142 * If only the month (and no year) is given, the month chosen shall be
143 the month, starting with the current month and moving into the
144 future, which first matches the named month. The first day of the
145 month shall be assumed if no day is given.
146
147 * If no hour, minute, and second are given, the current hour, minute,
148 and second shall be assumed.
149
150 * If no date is given, the hour chosen shall be the hour, starting
151 with the current hour and moving into the future, which first
152 matches the named hour.
153
154 If a conversion specification in the DATEMSK file does not correspond
155 to one of the conversion specifications above, the behavior is unspeci‐
156 fied.
157
158 The getdate() function need not be reentrant. A function that is not
159 required to be reentrant is not required to be thread-safe.
160
162 Upon successful completion, getdate() shall return a pointer to a
163 struct tm. Otherwise, it shall return a null pointer and set get‐
164 date_err to indicate the error.
165
167 The getdate() function shall fail in the following cases, setting get‐
168 date_err to the value shown in the list below. Any changes to errno are
169 unspecified.
170
171 1. The DATEMSK environment variable is null or undefined.
172
173 2. The template file cannot be opened for reading.
174
175 3. Failed to get file status information.
176
177 4. The template file is not a regular file.
178
179 5. An I/O error is encountered while reading the template file.
180
181 6. Memory allocation failed (not enough memory available).
182
183 7. There is no line in the template that matches the input.
184
185 8. Invalid input specification. For example, February 31; or a time is
186 specified that cannot be represented in a time_t (representing the
187 time in seconds since the Epoch).
188
189 The following sections are informative.
190
192 1. The following example shows the possible contents of a template:
193
194
195 %m
196 %A %B %d, %Y, %H:%M:%S
197 %A
198 %B
199 %m/%d/%y %I %p
200 %d,%m,%Y %H:%M
201 at %A the %dst of %B in %Y
202 run job at %I %p,%B %dnd
203 %A den %d. %B %Y %H.%M Uhr
204
205 2. The following are examples of valid input specifications for the
206 template in Example 1:
207
208
209 getdate("10/1/87 4 PM");
210 getdate("Friday");
211 getdate("Friday September 18, 1987, 10:30:30");
212 getdate("24,9,1986 10:30");
213 getdate("at monday the 1st of december in 1986");
214 getdate("run job at 3 PM, december 2nd");
215
216 If the LC_TIME category is set to a German locale that includes freitag
217 as a weekday name and oktober as a month name, the following would be
218 valid:
219
220
221 getdate("freitag den 10. oktober 1986 10.30 Uhr");
222
223 3. The following example shows how local date and time specification
224 can be defined in the template:
225
226 Invocation Line in Template
227 getdate("11/27/86") %m/%d/%y
228 getdate("27.11.86") %d.%m.%y
229 getdate("86-11-27") %y-%m-%d
230 getdate("Friday 12:00:00") %A %H:%M:%S
231
232 4. The following examples help to illustrate the above rules assuming
233 that the current date is Mon Sep 22 12:19:47 EDT 1986 and the
234 LC_TIME category is set to the default C locale:
235
236 Input Line in Template Date
237 Mon %a Mon Sep 22 12:19:47 EDT 1986
238 Sun %a Sun Sep 28 12:19:47 EDT 1986
239 Fri %a Fri Sep 26 12:19:47 EDT 1986
240 September %B Mon Sep 1 12:19:47 EDT 1986
241 January %B Thu Jan 1 12:19:47 EST 1987
242 December %B Mon Dec 1 12:19:47 EST 1986
243 Sep Mon %b %a Mon Sep 1 12:19:47 EDT 1986
244 Jan Fri %b %a Fri Jan 2 12:19:47 EST 1987
245 Dec Mon %b %a Mon Dec 1 12:19:47 EST 1986
246 Jan Wed 1989 %b %a %Y Wed Jan 4 12:19:47 EST 1989
247 Fri 9 %a %H Fri Sep 26 09:00:00 EDT 1986
248 Feb 10:30 %b %H:%S Sun Feb 1 10:00:30 EST 1987
249 10:30 %H:%M Tue Sep 23 10:30:00 EDT 1986
250 13:30 %H:%M Mon Sep 22 13:30:00 EDT 1986
251
253 Although historical versions of getdate() did not require that <time.h>
254 declare the external variable getdate_err, this volume of
255 IEEE Std 1003.1-2001 does require it. The standard developers encourage
256 applications to remove declarations of getdate_err and instead incorpo‐
257 rate the declaration by including <time.h>.
258
259 Applications should use %Y (4-digit years) in preference to %y (2-digit
260 years).
261
263 In standard locales, the conversion specifications %c, %x, and %X do
264 not include unsupported conversion specifiers and so the text regarding
265 results being undefined is not a problem in that case.
266
268 None.
269
271 ctime(), localtime(), setlocale(), strftime(), times(), the Base Defi‐
272 nitions volume of IEEE Std 1003.1-2001, <time.h>
273
275 Portions of this text are reprinted and reproduced in electronic form
276 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
277 -- Portable Operating System Interface (POSIX), The Open Group Base
278 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
279 Electrical and Electronics Engineers, Inc and The Open Group. In the
280 event of any discrepancy between this version and the original IEEE and
281 The Open Group Standard, the original IEEE and The Open Group Standard
282 is the referee document. The original Standard can be obtained online
283 at http://www.opengroup.org/unix/online.html .
284
285
286
287IEEE/The Open Group 2003 GETDATE(3P)