1getdate(3C) Standard C Library Functions getdate(3C)
2
3
4
6 getdate - convert user format date and time
7
9 #include <time.h>
10
11 struct tm *getdate(const char *string);
12 extern int getdate_err;
13
14
16 The getdate() function converts user-definable date and/or time speci‐
17 fications pointed to by string to a tm structure. The tm structure is
18 defined in the <time.h> header.
19
20
21 User-supplied templates are used to parse and interpret the input
22 string. The templates are text files created by the user and identi‐
23 fied via the environment variable DATEMSK. Each line in the template
24 represents an acceptable date and/or time specification using conver‐
25 sion specifications similar to those used by strftime(3C) and strp‐
26 time(3C). Dates before 1902 and after 2037 are illegal. The first line
27 in the template that matches the input specification is used for inter‐
28 pretation and conversion into the internal time format.
29
30 Conversion Specifications
31 The following conversion specifications are supported:
32
33 %% Same as %.
34
35
36 %a Locale's abbreviated weekday name.
37
38
39 %A Locale's full weekday name.
40
41
42 %b Locale's abbreviated month name.
43
44
45 %B Locale's full month name.
46
47
48 %c Locale's appropriate date and time representation.
49
50
51 %C Century number (the year divided by 100 and truncated to an inte‐
52 ger as a decimal number [1,99]); single digits are preceded by 0;
53 see standards(5). If used without the %y specifier, this format
54 specifier will assume the current year offset in whichever cen‐
55 tury is specified. The only valid years are between 1902-2037.
56
57
58 %d day of month [01,31]; leading zero is permitted but not required.
59
60
61 %D Date as %m/%d/%y.
62
63
64 %e Same as %d.
65
66
67 %h Locale's abbreviated month name.
68
69
70 %H Hour (24-hour clock) [0,23]; leading zero is permitted but not
71 required.
72
73
74 %I Hour (12-hour clock) [1,12]; leading zero is permitted but not
75 required.
76
77
78 %j Day number of the year [1,366]; leading zeros are permitted but
79 not required.
80
81
82 %m Month number [1,12]; leading zero is permitted but not required.
83
84
85 %M Minute [0,59]; leading zero is permitted but not required.
86
87
88 %n Any white space.
89
90
91 %p Locale's equivalent of either a.m. or p.m.
92
93
94 %r Appropriate time representation in the 12-hour clock format with
95 %p.
96
97
98 %R Time as %H:%M.
99
100
101 SUSv3
102 %S Seconds [0,60]; leading zero is permitted but not required. The
103 range of values is [00,60] rather than [00,59] to allow for the
104 occasional leap second.
105
106
107 Default and other standards
108 %S Seconds [0,61]; leading zero is permitted but not required. The
109 range of values is [00,61] rather than [00,59] to allow for the
110 occasional leap second and even more occasional double leap sec‐
111 ond.
112
113
114 %t Any white space.
115
116
117 %T Time as %H:%M:%S.
118
119
120 %U Week number of the year as a decimal number [0,53], with Sunday
121 as the first day of the week; leading zero is permitted but not
122 required.
123
124
125 %w Weekday as a decimal number [0,6], with 0 representing Sunday.
126
127
128 %W Week number of the year as a decimal number [0,53], with Monday
129 as the first day of the week; leading zero is permitted but not
130 required.
131
132
133 %x Locale's appropriate date representation.
134
135
136 %X Locale's appropriate time representation.
137
138
139 %y Year within century. When a century is not otherwise specified,
140 values in the range 69-99 refer to years in the twentieth century
141 (1969 to 1999 inclusive); values in the range 00-68 refer to
142 years in the twenty-first century (2000 to 2068 inclusive).
143
144
145 %Y Year, including the century (for example, 1993).
146
147
148 %Z Time zone name or no characters if no time zone exists.
149
150
151 Modified Conversion Specifications
152 Some conversion specifications can be modified by the E and O modifier
153 characters to indicate that an alternative format or specification
154 should be used rather than the one normally used by the unmodified
155 specification. If the alternative format or specification does not
156 exist in the current locale, the behavior be as if the unmodified
157 conversion specification were used.
158
159 %Ec Locale's alternative appropriate date and time representation.
160
161
162 %EC Name of the base year (period) in the locale's alternative rep‐
163 resentation.
164
165
166 %Ex Locale's alternative date representation.
167
168
169 %EX Locale's alternative time representation.
170
171
172 %Ey Offset from %EC (year only) in the locale's alternative repre‐
173 sentation.
174
175
176 %EY Full alternative year representation.
177
178
179 %Od Day of the month using the locale's alternative numeric sym‐
180 bols; leading zeros are permitted but not required.
181
182
183 %Oe Same as %Od.
184
185
186 %OH Hour (24-hour clock) using the locale's alternative numeric sym‐
187 bols.
188
189
190 %OI Hour (12-hour clock) using the locale's alternative numeric sym‐
191 bols.
192
193
194 %Om Month using the locale's alternative numeric symbols.
195
196
197 %OM Minutes using the locale's alternative numeric symbols.
198
199
200 %OS Seconds using the locale's alternative numeric symbols.
201
202
203 %OU Week number of the year (Sunday as the first day of the week)
204 using the locale's alternative numeric symbols.
205
206
207 %Ow Number of the weekday (Sunday=0) using the locale's alternative
208 numeric symbols.
209
210
211 %OW Week number of the year (Monday as the first day of the week)
212 using the locale's alternative numeric symbols.
213
214
215 %Oy Year (offset from %C) in the locale's alternative representa‐
216 tion and using the locale's alternative numeric symbols.
217
218
219 Internal Format Conversion
220 The following rules are applied for converting the input specification
221 into the internal format:
222
223 o If only the weekday is given, today is assumed if the given
224 day is equal to the current day and next week if it is less.
225
226 o If only the month is given, the current month is assumed if
227 the given month is equal to the current month and next year
228 if it is less and no year is given. (The first day of month
229 is assumed if no day is given.)
230
231 o If only the year is given, the values of the tm_mon,
232 tm_mday, tm_yday, tm_wday, and tm_isdst members of the
233 returned tm structure are not specified.
234
235 o If the century is given, but the year within the century is
236 not given, the current year within the century is assumed.
237
238 o If no hour, minute, and second are given, the current hour,
239 minute, and second are assumed.
240
241 o If no date is given, today is assumed if the given hour is
242 greater than the current hour and tomorrow is assumed if it
243 is less.
244
245 General Specifications
246 A conversion specification that is an ordinary character is executed by
247 scanning the next character from the buffer. If the character scanned
248 from the buffer differs from the one comprising the conversion specifi‐
249 cation, the specification fails, and the differing and subsequent char‐
250 acters remain unscanned.
251
252
253 A series of conversion specifications composed of %n, %t, white space
254 characters, or any combination is executed by scanning up to the first
255 character that is not white space (which remains unscanned), or until
256 no more characters can be scanned.
257
258
259 Any other conversion specification is executed by scanning characters
260 until a character matching the next conversion specification is
261 scanned, or until no more characters can be scanned. These characters,
262 except the one matching the next conversion specification, are then
263 compared to the locale values associated with the conversion specifier.
264 If a match is found, values for the appropriate tm structure members
265 are set to values corresponding to the locale information. If no match
266 is found, getdate() fails and no more characters are scanned.
267
268
269 The month names, weekday names, era names, and alternative numeric sym‐
270 bols can consist of any combination of upper and lower case letters.
271 The user can request that the input date or time specification be in a
272 specific language by setting the LC_TIME category using setlocale(3C).
273
275 If successful, getdate() returns a pointer to a tm structure; other‐
276 wise, it returns NULL and sets the global variable getdate_err to indi‐
277 cate the error. Subsequent calls to getdate() alter the contents of
278 getdate_err.
279
280
281 The following is a complete list of the getdate_err settings and their
282 meanings:
283
284 1 The DATEMSK environment variable is null or undefined.
285
286
287 2 The template file cannot be opened for reading.
288
289
290 3 Failed to get file status information.
291
292
293 4 The template file is not a regular file.
294
295
296 5 An error is encountered while reading the template file.
297
298
299 6 The malloc() function failed (not enough memory is available).
300
301
302 7 There is no line in the template that matches the input.
303
304
305 8 The input specification is invalid (for example, February 31).
306
307
309 The getdate() function makes explicit use of macros described on the
310 ctype(3C) manual page.
311
313 Example 1 Examples of the getdate() function.
314
315
316 The following example shows the possible contents of a template:
317
318
319 %m
320 %A %B %d %Y, %H:%M:%S
321 %A
322 %B
323 %m/%d/%y %I %p
324 %d,%m,%Y %H:%M
325 at %A the %dst of %B in %Y
326 run job at %I %p,%B %dnd
327 %A den %d. %B %Y %H.%M Uhr
328
329
330
331 The following are examples of valid input specifications for the above
332 template:
333
334
335 getdate("10/1/87 4 PM")
336 getdate("Friday")
337 getdate("Friday September 19 1987, 10:30:30")
338 getdate("24,9,1986 10:30")
339 getdate("at monday the 1st of december in 1986")
340 getdate("run job at 3 PM, december 2nd")
341
342
343
344 If the LANG environment variable is set to de (German), the following
345 is valid:
346
347
348 getdate("freitag den 10. oktober 1986 10.30 Uhr")
349
350
351
352 Local time and date specification are also supported. The following
353 examples show how local date and time specification can be defined in
354 the template.
355
356
357
358
359
360 ┌─────────────────────────────┬─────────────────────────────┐
361 │Invocation │Line in Template │
362 ├─────────────────────────────┼─────────────────────────────┤
363 │getdate("11/27/86") │%m/%d/%y │
364 │getdate("27.11.86") │%d.%m.%y │
365 │getdate("86-11-27") │%y-%m-%d │
366 │getdate("Friday 12:00:00") │%A %H:%M:%S │
367 └─────────────────────────────┴─────────────────────────────┘
368
369
370 The following examples illustrate the Internal Format Conversion rules.
371 Assume that the current date is Mon Sep 22 12:19:47 EDT 1986 and the
372 LANG environment variable is not set.
373
374
375
376
377
378 ┌───────────────┬────────────────┬─────────────────────────────┐
379 │Input │Template Line │Date │
380 ├───────────────┼────────────────┼─────────────────────────────┤
381 │Mon │%a │Mon Sep 22 12:19:48 EDT 1986 │
382 │Sun │%a │Sun Sep 28 12:19:49 EDT 1986 │
383 │Fri │%a │Fri Sep 26 12:19:49 EDT 1986 │
384 │September │%B │Mon Sep 1 12:19:49 EDT 1986 │
385 │January │%B │Thu Jan 1 12:19:49 EST 1987 │
386 │December │%B │Mon Dec 1 12:19:49 EDT 1986 │
387 │Sep Mon │%b %a │Mon Sep 1 12:19:50 EDT 1986 │
388 │Jan Fri │%b %a │Fri Jan 2 12:19:50 EST 1987 │
389 │Dec Mon │%b %a │Mon Dec 1 12:19:50 EST 1986 │
390 │Jan Wed 1989 │%b %a %Y │Wed Jan 4 12:19:51 EST 1989 │
391 │Fri 9 │%a %H │Fri Sep 26 09:00:00 EDT 1986 │
392 │Feb 10:30 │%b %H:%S │Sun Feb 1 10:00:30 EST 1987 │
393 │10:30 │%H:%M │Tue Sep 23 10:30:00 EDT 1986 │
394 │13:30 │%H:%M │Mon Sep 22 13:30:00 EDT 1986 │
395 └───────────────┴────────────────┴─────────────────────────────┘
396
398 See attributes(5) for descriptions of the following attributes:
399
400
401
402
403 ┌─────────────────────────────┬─────────────────────────────┐
404 │ATTRIBUTE TYPE │ATTRIBUTE VALUE │
405 ├─────────────────────────────┼─────────────────────────────┤
406 │CSI │Enabled │
407 ├─────────────────────────────┼─────────────────────────────┤
408 │Interface Stability │Standard │
409 ├─────────────────────────────┼─────────────────────────────┤
410 │MT-Level │MT-Safe │
411 └─────────────────────────────┴─────────────────────────────┘
412
414 ctype(3C), mktime(3C), setlocale(3C), strftime(3C), strptime(3C),
415 attributes(5), environ(5), standards(5)
416
417
418
419SunOS 5.11 1 Nov 2003 getdate(3C)