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