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