1 /* Parse dates for touch and date.
3 Copyright (C) 1989-1991, 1998, 2000-2025 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Yacc-based version written by Jim Kingdon and David MacKenzie.
19 Rewritten by Jim Meyering. */
29 #include <stdckdint.h>
35 touch -t [[CC]YY]mmddhhmm[.ss] FILE...
36 8, 10, or 12 digits, followed by optional .ss
37 (PDS_CENTURY | PDS_SECONDS)
39 touch mmddhhmm[YY] FILE... (obsoleted by POSIX 1003.1-2001)
40 8 or 10 digits, YY (if present) must be in the range 69-99
41 (PDS_TRAILING_YEAR | PDS_PRE_2000)
45 (PDS_TRAILING_YEAR | PDS_CENTURY)
50 year (struct tm
*tm
, const int *digit_pair
, idx_t n
, unsigned int syntax_bits
)
55 tm
->tm_year
= *digit_pair
;
56 /* Deduce the century based on the year.
57 POSIX requires that 00-68 be interpreted as 2000-2068,
58 and that 69-99 be interpreted as 1969-1999. */
59 if (digit_pair
[0] <= 68)
61 if (syntax_bits
& PDS_PRE_2000
)
68 if (! (syntax_bits
& PDS_CENTURY
))
70 tm
->tm_year
= digit_pair
[0] * 100 + digit_pair
[1] - 1900;
75 /* Use current year. */
76 time_t now
= time (NULL
);
77 struct tm
*tmp
= localtime (&now
);
80 tm
->tm_year
= tmp
->tm_year
;
92 posix_time_parse (struct tm
*tm
, const char *s
, unsigned int syntax_bits
)
94 const char *dot
= NULL
;
97 idx_t s_len
= strlen (s
);
100 if (syntax_bits
& PDS_SECONDS
)
102 dot
= strchr (s
, '.');
106 if (s_len
- len
!= 3)
111 if (! (8 <= len
&& len
<= 12 && len
% 2 == 0))
114 for (idx_t i
= 0; i
< len
; i
++)
115 if (!c_isdigit (s
[i
]))
119 for (idx_t i
= 0; i
< len
; i
++)
120 pair
[i
] = 10 * (s
[2*i
] - '0') + s
[2*i
+ 1] - '0';
123 if (! (syntax_bits
& PDS_TRAILING_YEAR
))
125 if (! year (tm
, p
, len
- 4, syntax_bits
))
131 /* Handle 8 digits worth of 'MMDDhhmm'. */
132 tm
->tm_mon
= *p
++ - 1;
138 /* Handle any trailing year. */
139 if (syntax_bits
& PDS_TRAILING_YEAR
)
141 if (! year (tm
, p
, len
, syntax_bits
))
145 /* Handle seconds. */
148 else if (c_isdigit (dot
[1]) && c_isdigit (dot
[2]))
149 tm
->tm_sec
= 10 * (dot
[1] - '0') + dot
[2] - '0';
156 /* Parse a POSIX-style date, returning true if successful. */
159 posixtime (time_t *p
, const char *s
, unsigned int syntax_bits
)
162 bool leapsec
= false;
164 if (! posix_time_parse (&tm0
, s
, syntax_bits
))
170 tm1
.tm_sec
= tm0
.tm_sec
;
171 tm1
.tm_min
= tm0
.tm_min
;
172 tm1
.tm_hour
= tm0
.tm_hour
;
173 tm1
.tm_mday
= tm0
.tm_mday
;
174 tm1
.tm_mon
= tm0
.tm_mon
;
175 tm1
.tm_year
= tm0
.tm_year
;
178 time_t t
= mktime (&tm1
);
183 /* Reject dates like "September 31" and times like "25:61".
184 However, allow a seconds count of 60 even in time zones that do
185 not support leap seconds, treating it as the following second;
186 POSIX requires this. */
187 if (! ((tm0
.tm_year
^ tm1
.tm_year
)
188 | (tm0
.tm_mon
^ tm1
.tm_mon
)
189 | (tm0
.tm_mday
^ tm1
.tm_mday
)
190 | (tm0
.tm_hour
^ tm1
.tm_hour
)
191 | (tm0
.tm_min
^ tm1
.tm_min
)
192 | (tm0
.tm_sec
^ tm1
.tm_sec
)))
194 if (ckd_add (&t
, t
, +leapsec
))
200 /* Any mismatch without 60 in the tm_sec field is invalid. */
201 if (tm0
.tm_sec
!= 60)
204 /* Allow times like 01:35:60 or 23:59:60. */