1 /* Parse dates for touch and date.
2 Copyright (C) 1989, 1990, 1991, 1998, 2000, 2001 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)
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 Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Yacc-based version written by Jim Kingdon and David MacKenzie.
19 Rewritten by Jim Meyering. */
29 #include <sys/types.h>
37 # include <sys/time.h>
43 #include "unlocked-io.h"
45 /* ISDIGIT differs from isdigit, as follows:
46 - Its arg may be any int or unsigned int; it need not be an unsigned char.
47 - It's guaranteed to evaluate its argument exactly once.
48 - It's typically faster.
49 Posix 1003.2-1992 section 2.5.2.1 page 50 lines 1556-1558 says that
50 only '0' through '9' are digits. Prefer ISDIGIT to isdigit unless
51 it's important to use the locale's definition of `digit' even when the
52 host does not conform to Posix. */
53 #define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
55 /* The return value. */
63 touch -t [[CC]YY]mmddhhmm[.ss] FILE...
64 8, 10, or 12 digits, followed by optional .ss
65 (PDS_LEADING_YEAR | PDS_CENTURY | PDS_SECONDS)
67 touch mmddhhmm[YY] FILE... (obsolescent)
73 (PDS_TRAILING_YEAR | PDS_CENTURY)
78 year (const int *digit_pair, size_t n, int allow_century)
83 t.tm_year = *digit_pair;
84 /* Deduce the century based on the year.
85 See POSIX.2 section 4.63.3. */
86 if (digit_pair[0] <= 68)
93 t.tm_year = digit_pair[0] * 100 + digit_pair[1];
104 /* Use current year. */
106 tmp = localtime (&now);
107 t.tm_year = tmp->tm_year;
119 posix_time_parse (const char *s, unsigned int syntax_bits)
121 const char *dot = NULL;
126 size_t s_len = strlen (s);
127 size_t len = (((syntax_bits & PDS_SECONDS) && (dot = strchr (s, '.')))
131 if (len != 8 && len != 10 && len != 12)
136 if (!(syntax_bits & PDS_SECONDS))
139 if (s_len - len != 3)
143 for (i = 0; i < len; i++)
148 for (i = 0; i < len; i++)
149 pair[i] = 10 * (s[2*i] - '0') + s[2*i + 1] - '0';
152 if (syntax_bits & PDS_LEADING_YEAR)
154 if (year (p, len - 4, syntax_bits & PDS_CENTURY))
160 /* Handle 8 digits worth of `MMDDhhmm'. */
161 if (*p < 1 || *p > 12)
166 if (*p < 1 || *p > 31)
171 if (*p < 0 || *p > 23)
176 if (*p < 0 || *p > 59)
181 /* Handle any trailing year. */
182 if (syntax_bits & PDS_TRAILING_YEAR)
184 if (year (p, len, syntax_bits & PDS_CENTURY))
188 /* Handle seconds. */
198 if (!ISDIGIT (dot[0]) || !ISDIGIT (dot[1]))
200 seconds = 10 * (dot[0] - '0') + dot[1] - '0';
202 if (seconds < 0 || seconds > 61)
210 /* Parse a POSIX-style date and return it, or (time_t)-1 for an error. */
213 posixtime (const char *s, unsigned int syntax_bits)
216 if (posix_time_parse (s, syntax_bits))
222 /* Parse a POSIX-style date and return it, or NULL for an error. */
225 posixtm (const char *s, unsigned int syntax_bits)
227 if (posixtime (s, syntax_bits) == -1)
232 #ifdef TEST_POSIXTIME
234 Test mainly with syntax_bits == 13
235 (aka: (PDS_LEADING_YEAR | PDS_CENTURY | PDS_SECONDS))
238 1112131415 13 1323807300 Tue Dec 13 14:15:00 2011
239 1112131415.16 13 1323807316 Tue Dec 13 14:15:16 2011
240 201112131415.16 13 1323807316 Tue Dec 13 14:15:16 2011
241 191112131415.16 13 -1 ***
242 203712131415.16 13 2144348116 Sun Dec 13 14:15:16 2037
243 3712131415.16 13 2144348116 Sun Dec 13 14:15:16 2037
244 6812131415.16 13 -1 ***
245 6912131415.16 13 -1 ***
246 7012131415.16 13 29967316 Sun Dec 13 14:15:16 1970
247 12131415.16 13 913580116 Sun Dec 13 14:15:16 1998
249 1213141599 2 945116100 Mon Dec 13 14:15:00 1999
250 1213141500 2 976738500 Wed Dec 13 14:15:00 2000
255 # define MAX_BUFF_LEN 1024
260 char buff[MAX_BUFF_LEN + 1];
262 buff[MAX_BUFF_LEN] = 0;
263 while (fgets (buff, MAX_BUFF_LEN, stdin) && buff[0])
265 char time_str[MAX_BUFF_LEN];
266 unsigned int syntax_bits;
269 sscanf (buff, "%s %u", time_str, &syntax_bits);
270 t = posixtime (time_str, syntax_bits);
271 result = (t == (time_t) -1 ? "***" : ctime (&t));
272 printf ("%d %s\n", (int) t, result);