OSDN Git Service

bd48d7edb117bccace2a9db381a981d2d78e4231
[linuxjm/LDP_man-pages.git] / original / man3 / getdate.3
1 .\" Copyright 2001 walter harms (walter.harms@informatik.uni-oldenburg.de)
2 .\" and Copyright 2008, Linux Foundation, written by Michael Kerrisk
3 .\"     <mtk.manpages@gmail.com>
4 .\"
5 .\" %%%LICENSE_START(VERBATIM)
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
9 .\"
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
14 .\"
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein.  The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" %%%LICENSE_END
26 .\"
27 .\" Modified, 2001-12-26, aeb
28 .\" 2008-09-07, mtk, Various rewrites; added an example program.
29 .\"
30 .TH GETDATE 3 2010-09-20 "" "Linux Programmer's Manual"
31 .SH NAME
32 getdate, getdate_r \- convert a date-plus-time string to broken-down time
33 .SH SYNOPSIS
34 .B "#include <time.h>"
35 .sp
36 .BI "struct tm *getdate(const char *" string );
37 .sp
38 .B "extern int getdate_err;"
39 .sp
40 .B "#include <time.h>"
41 .sp
42 .BI "int getdate_r(const char *" string ", struct tm *" res );
43 .sp
44 .in -4n
45 Feature Test Macro Requirements for glibc (see
46 .BR feature_test_macros (7)):
47 .in
48 .sp
49 .BR getdate ():
50 .ad l
51 .RS 4
52 _XOPEN_SOURCE\ >=\ 500 ||
53 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
54 .RE
55 .br
56 .BR getdate_r ():
57 .ad l
58 .RS 4
59 _GNU_SOURCE
60 .RE
61 .ad
62 .SH DESCRIPTION
63 The function
64 .BR getdate ()
65 converts a string representation of a date and time,
66 contained in the buffer pointed to by
67 .IR string ,
68 into a broken-down time.
69 The broken-down time is stored in a
70 .I tm
71 structure, and a pointer to this
72 structure is returned as the function result.
73 This
74 .I tm
75 structure is allocated in static storage,
76 and consequently it will be overwritten by further calls to
77 .BR getdate ().
78
79 In contrast to
80 .BR strptime (3),
81 (which has a
82 .I format
83 argument),
84 .BR getdate ()
85 uses the formats found in the file
86 whose full pathname is given in the environment variable
87 .BR DATEMSK .
88 The first line in the file that matches the given input string
89 is used for the conversion.
90
91 The matching is done case insensitively.
92 Superfluous whitespace, either in the pattern or in the string to
93 be converted, is ignored.
94
95 The conversion specifications that a pattern can contain are those given for
96 .BR strptime (3).
97 One more conversion specification is specified in POSIX.1-2001:
98 .TP
99 .B %Z
100 Timezone name.
101 This is not implemented in glibc.
102 .LP
103 When
104 .B %Z
105 is given, the structure containing the broken-down time
106 is initialized with values corresponding to the current
107 time in the given timezone.
108 Otherwise, the structure is initialized to the broken-down time
109 corresponding to the current local time (as by a call to
110 .BR localtime (3)).
111 .LP
112 When only the weekday is given, the day is taken to be the first such day
113 on or after today.
114 .LP
115 When only the month is given (and no year), the month is taken to
116 be the first such month equal to or after the current month.
117 If no day is given, it is the first day of the month.
118 .LP
119 When no hour, minute and second are given, the current
120 hour, minute and second are taken.
121 .LP
122 If no date is given, but we know the hour, then that hour is taken
123 to be the first such hour equal to or after the current hour.
124
125 .BR getdate_r ()
126 is a GNU extension that provides a reentrant version of
127 .BR getdate ().
128 Rather than using a global variable to report errors and a static buffer
129 to return the broken down time,
130 it returns errors via the function result value,
131 and returns the resulting broken-down time in the
132 caller-allocated buffer pointed to by the argument
133 .IR res .
134 .SH RETURN VALUE
135 When successful,
136 .BR getdate ()
137 returns a pointer to a
138 .IR "struct tm" .
139 Otherwise, it returns NULL and sets the global variable
140 .IR getdate_err
141 to one of the error numbers shown below.
142 Changes to
143 .I errno
144 are unspecified.
145
146 On success
147 .BR getdate_r ()
148 returns 0;
149 on error it returns one of the error numbers shown below.
150 .SH ERRORS
151 The following errors are returned via
152 .IR getdate_err
153 (for
154 .BR getdate ())
155 or as the function result (for
156 .BR getdate_r ()):
157 .TP 4n
158 .B 1
159 The
160 .B DATEMSK
161 environment variable is not defined, or its value is an empty string.
162 .TP
163 .B 2
164 The template file specified by
165 .B DATEMSK
166 cannot be opened for reading.
167 .TP
168 .B 3
169 Failed to get file status information.
170 .\" stat()
171 .TP
172 .B 4
173 The template file is not a regular file.
174 .TP
175 .B 5
176 An error was encountered while reading the template file.
177 .TP
178 .B 6
179 Memory allocation failed (not enough memory available).
180 .\" Error 6 doesn't seem to occur in glibc
181 .TP
182 .B 7
183 There is no line in the file that matches the input.
184 .TP
185 .B 8
186 Invalid input specification.
187 .SH ENVIRONMENT
188 .TP
189 .B DATEMSK
190 File containing format patterns.
191 .TP
192 .BR TZ ", " LC_TIME
193 Variables used by
194 .BR strptime (3).
195 .SH CONFORMING TO
196 POSIX.1-2001.
197 .SH NOTES
198 The POSIX.1-2001 specification for
199 .BR strptime (3)
200 contains conversion specifications using the
201 .B %E
202 or
203 .B %O
204 modifier, while such specifications are not given for
205 .BR getdate ().
206 In glibc,
207 .BR getdate ()
208 is implemented using
209 .BR strptime (3),
210 so that precisely the same conversions are supported by both.
211 .SH EXAMPLE
212 The program below calls
213 .BR getdate ()
214 for each of its command-line arguments,
215 and for each call displays the values in the fields of the returned
216 .I tm
217 structure.
218 The following shell session demonstrates the operation of the program:
219
220 .in +4n
221 .nf
222 .RB "$" " TFILE=$PWD/tfile"
223 .RB "$" " echo \(aq%A\(aq > $TFILE " "      # Full weekday name"
224 .RB "$" " echo \(aq%T\(aq >> $TFILE" "      # ISO date (YYYY-MM-DD)"
225 .RB "$" " echo \(aq%F\(aq >> $TFILE" "      # Time (HH:MM:SS)"
226 .RB "$" " date"
227 .RB "$" " export DATEMSK=$TFILE"
228 .RB "$" " ./a.out Tuesday \(aq2009-12-28\(aq \(aq12:22:33\(aq"
229 Sun Sep  7 06:03:36 CEST 2008
230 Call 1 ("Tuesday") succeeded:
231     tm_sec   = 36
232     tm_min   = 3
233     tm_hour  = 6
234     tm_mday  = 9
235     tm_mon   = 8
236     tm_year  = 108
237     tm_wday  = 2
238     tm_yday  = 252
239     tm_isdst = 1
240 Call 2 ("2009-12-28") succeeded:
241     tm_sec   = 36
242     tm_min   = 3
243     tm_hour  = 6
244     tm_mday  = 28
245     tm_mon   = 11
246     tm_year  = 109
247     tm_wday  = 1
248     tm_yday  = 361
249     tm_isdst = 0
250 Call 3 ("12:22:33") succeeded:
251     tm_sec   = 33
252     tm_min   = 22
253     tm_hour  = 12
254     tm_mday  = 7
255     tm_mon   = 8
256     tm_year  = 108
257     tm_wday  = 0
258     tm_yday  = 250
259     tm_isdst = 1
260 .fi
261 .in
262 .SS Program source
263 \&
264 .nf
265 #define _GNU_SOURCE 500
266 #include <time.h>
267 #include <stdio.h>
268 #include <stdlib.h>
269
270 int
271 main(int argc, char *argv[])
272 {
273     struct tm *tmp;
274     int j;
275
276     for (j = 1; j < argc; j++) {
277         tmp = getdate(argv[j]);
278
279         if (tmp == NULL) {
280             printf("Call %d failed; getdate_err = %d\\n",
281                    j, getdate_err);
282             continue;
283         }
284
285         printf("Call %d (\\"%s\\") succeeded:\\n", j, argv[j]);
286         printf("    tm_sec   = %d\\n", tmp\->tm_sec);
287         printf("    tm_min   = %d\\n", tmp\->tm_min);
288         printf("    tm_hour  = %d\\n", tmp\->tm_hour);
289         printf("    tm_mday  = %d\\n", tmp\->tm_mday);
290         printf("    tm_mon   = %d\\n", tmp\->tm_mon);
291         printf("    tm_year  = %d\\n", tmp\->tm_year);
292         printf("    tm_wday  = %d\\n", tmp\->tm_wday);
293         printf("    tm_yday  = %d\\n", tmp\->tm_yday);
294         printf("    tm_isdst = %d\\n", tmp\->tm_isdst);
295     }
296
297     exit(EXIT_SUCCESS);
298 }
299 .fi
300 .SH SEE ALSO
301 .BR time (2),
302 .BR localtime (3),
303 .BR setlocale (3),
304 .BR strftime (3),
305 .BR strptime (3)