merge with 3.8.4c
[coreutils.git] / lib / posixtm.y
blobbb5b40e753d619aea209f8aa84b00a4e263e1e0a
1 /* Parse dates for touch.
2 Copyright (C) 1989, 1990, 1991 Free Software Foundation Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Written by Jim Kingdon and David MacKenzie. */
20 #ifdef __GNUC__
21 #define alloca __builtin_alloca
22 #else
23 #ifdef sparc
24 #include <alloca.h>
25 #else
26 #ifdef _AIX
27 #pragma alloca
28 #else
29 char *alloca ();
30 #endif
31 #endif
32 #endif
34 #include <stdio.h>
35 #include <sys/types.h>
36 #include <time.h>
38 #define YYDEBUG 1
40 /* Lexical analyzer's current scan position in the input string. */
41 static char *curpos;
43 /* The return value. */
44 static struct tm t;
46 time_t mktime ();
48 #define yyparse posixtime_yyparse
49 static int yylex ();
50 static int yyerror ();
53 %token DIGIT
56 date :
57 digitpair /* month */
58 digitpair /* day */
59 digitpair /* hours */
60 digitpair /* minutes */
61 year
62 seconds {
63 if ($1 >= 1 && $1 <= 12)
64 t.tm_mon = $1 - 1;
65 else {
66 YYABORT;
68 if ($2 >= 1 && $2 <= 31)
69 t.tm_mday = $2;
70 else {
71 YYABORT;
73 if ($3 >= 0 && $3 <= 23)
74 t.tm_hour = $3;
75 else {
76 YYABORT;
78 if ($4 >= 0 && $4 <= 59)
79 t.tm_min = $4;
80 else {
81 YYABORT;
85 year : digitpair {
86 t.tm_year = $1;
87 /* Deduce the century based on the year.
88 See POSIX.2 section 4.63.3. */
89 if ($1 <= 68)
90 t.tm_year += 100;
92 | digitpair digitpair {
93 t.tm_year = $1 * 100 + $2;
94 if (t.tm_year < 1900) {
95 YYABORT;
96 } else
97 t.tm_year -= 1900;
99 | /* empty */ {
100 time_t now;
101 struct tm *tmp;
103 /* Use current year. */
104 time (&now);
105 tmp = localtime (&now);
106 t.tm_year = tmp->tm_year;
110 seconds : /* empty */ {
111 t.tm_sec = 0;
113 | '.' digitpair {
114 if ($2 >= 0 && $2 <= 61)
115 t.tm_sec = $2;
116 else {
117 YYABORT;
122 digitpair : DIGIT DIGIT {
123 $$ = $1 * 10 + $2;
127 static int
128 yylex ()
130 char ch = *curpos++;
132 if (ch >= '0' && ch <= '9')
134 yylval = ch - '0';
135 return DIGIT;
137 else if (ch == '.' || ch == 0)
138 return ch;
139 else
140 return '?'; /* Cause an error. */
143 static int
144 yyerror ()
146 return 0;
149 /* Parse a POSIX-style date and return it, or (time_t)-1 for an error. */
151 time_t
152 posixtime (s)
153 char *s;
155 curpos = s;
156 /* Let mktime decide whether it is daylight savings time. */
157 t.tm_isdst = -1;
158 if (yyparse ())
159 return (time_t)-1;
160 else
161 return mktime (&t);
164 /* Parse a POSIX-style date and return it, or NULL for an error. */
166 struct tm *
167 posixtm (s)
168 char *s;
170 if (posixtime (s) == -1)
171 return NULL;
172 return &t;