1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
/**
* Copyright © 2015 Mattias Andrée <maandree@member.fsf.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#define _DEFAULT_SOURCE
#include "parse_time.h"
#include "common.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <limits.h>
/**
* The number of seconds in a day.
*/
#define ONE_DAY (time_t)(24 * 60 * 60)
/**
* Set errno and return.
*
* @return e:int The error number.
*/
#define FAIL(e) return errno = (e), -1
/**
* `a *= b` with overflow check.
*/
#define MUL(a, b) if (a > timemax / (b)) FAIL(ERANGE); else a *= (b)
/**
* `a += b` with overflow check.
*/
#define ADD(a, b) if (a > timemax - (b)) FAIL(ERANGE); else a += (b)
/**
* The name of the process.
*/
extern char *argv0;
/**
* The highest value that can be stored in `time_t`.
*/
const time_t timemax = (sizeof(time_t) == sizeof(long long int)) ? LLONG_MAX : LONG_MAX;
/**
* Wrapper for `strtoll` and `strtol` that selects
* the appropriate function for `time_t`, does not
* allow whitespace or signs. It also forces the
* base to be 10, and will only return 0 on error,
* and will set `errno` on success.
*
* @param str Please see strtol(3), first parameter.
* @param end Please see strtol(3), second parameter.
* @return Please see strtol(3), only 0 may be
* returned at error.
*
* @throws 0 On success.
* @throws Please see strtol(3).
*/
static time_t
strtotime(const char *str, const char **end)
{
time_t rc;
long long int rcll;
long int rcl;
char **end_ = (char **)end;
if (!isdigit(*str))
FAIL(EINVAL);
/* The outer if-statement is for when `time_t` is update to `long long int`.
* which is need to avoid the year 2038 problem. Be we have to use `strtol`
* if `time_t` is `long int`, otherwise we will not detect overflow. */
errno = 0;
if (sizeof(time_t) == sizeof(long long int)) {
rcll = strtoll(str, end_, 10);
rc = (time_t)rcll;
} else {
rcl = strtol(str, end_, 10);
rc = (time_t)rcl;
}
return errno ? 0 : rc;
}
/**
* Parse a time on the format HH:MM[:SS].
*
* @param str The time string.
* @param ts Output parameter for the POSIX time the string
* represents.
* @param end Output parameter for the end of the parsing of `str`.
* @return 0 on success, -1 on error.
*
* @throws EINVAL `str` could not be parsed.
* @throws ERANGE `str` specifies a time beyond what can be stored.
*/
static int
parse_time_time(const char *str, struct timespec *ts, const char **end)
{
time_t tm;
memset(ts, 0, sizeof(*ts));
ts->tv_sec = strtotime(str, end), str = *end;
t (errno);
/* Must not be restricted to 23, beyond 24 is legal. */
MUL(ts->tv_sec, (time_t)(60 * 60));
if (*str++ != ':')
FAIL(EINVAL);
tm = strtotime(str, end), str = *end;
t (errno);
if (tm >= 60)
FAIL(EINVAL);
MUL(tm, (time_t)60);
ADD(ts->tv_sec, tm);
switch (*str++) {
case 0: return 0;
case ':': break;
default: FAIL(EINVAL);
}
tm = strtotime(str, end), str = *end;
t (errno);
/* Do not restrict to 59, or 60, there can be more than +1 leap second. */
ADD(ts->tv_sec, tm);
return 0;
fail:
return -1;
}
/**
* Parse a time with only a second-count.
*
* @param str The time string.
* @param ts Output parameter for the POSIX time the string
* represents.
* @param end Output parameter for the end of the parsing of `str`.
* @return 0 on success, -1 on error.
*
* @throws EINVAL `str` could not be parsed.
* @throws ERANGE `str` specifies a time beyond what can be stored.
*/
static int
parse_time_seconds(const char *str, struct timespec *ts, const char **end)
{
memset(ts, 0, sizeof(*ts));
ts->tv_sec = strtotime(str, end);
return errno ? -1 : 0;
}
/**
* Trivial time-parsing.
*
* @param str The time string.
* @param ts Output parameter for the POSIX time the string
* represents. If the time is in the pasted, but
* within a day, one day is added. This is in case
* you are using a external parser that did not
* release they you wanted a future time but
* thought you wanted the time to be the closed or
* in the same day.
* @param clk Output parameter for the clock in which the
* returned time is specified.
* @return 0 on success, -1 on error.
*
* @throws EINVAL `str` could not be parsed.
* @throws ERANGE `str` specifies a time beyond what can be stored.
* @throws EDOM The specified the was over a day ago.
*
* @futuredirection Add environment variable that allows
* parsing through another program.
*/
int
parse_time(const char *str, struct timespec *ts, clockid_t *clk)
{
struct timespec now;
int points, plus = *str == '+';
const char *start = str;
const char *end;
time_t adj;
/* Get current time and clock. */
clock_gettime(CLOCK_REALTIME, &now);
*clk = plus ? CLOCK_BOOTTIME : CLOCK_REALTIME;
/* Mañana? */
if (!strcmp(str, "mañana")) { /* Do not documented. */
ts->tv_sec = now.tv_sec + ONE_DAY;
ts->tv_nsec = now.tv_nsec;
return 0;
}
/* HH:MM[:SS[.NNNNNNNNN]] or seconds? */
if (strchr(str, ':')) {
t (parse_time_time(str, ts, &end));
adj = now.tv_sec - (now.tv_sec % ONE_DAY);
ADD(ts->tv_sec, adj); /* In case the HH is really large. */
} else {
t (parse_time_seconds(str + plus, ts, &end));
}
str = end;
/* Any fractions of a second? */
switch (*str) {
case 0: return 0;
case '.': break;
default: FAIL(EINVAL);
}
/* Parse up to nanosecond resolution. */
for (points = 0; isdigit(*str); points++) {
if (points < 9) {
ts->tv_nsec *= 10;
ts->tv_nsec += *str++ & 15;
} else if ((points == 10) && (*str >= '5')) {
ts->tv_nsec += 1;
}
}
if (ts->tv_nsec > 999999999L) {
ts->tv_sec += 1;
ts->tv_nsec = 0;
}
/* Check for error at end, and missing explicit UTC. */
if (*str) {
if (*clk == CLOCK_BOOTTIME)
FAIL(EINVAL);
while (*str == ' ') str++;
if (!strcasecmp(str, "Z") && !strcasecmp(str, "UTC"))
FAIL(EINVAL);
} else if (*clk == CLOCK_REALTIME) {
fprintf(stderr,
"%s: warning: parsing as UTC, you can avoid this warning "
"by adding a 'Z' at the end of the time argument.\n", argv0);
}
/* Adjust the day? */
if (*clk == CLOCK_BOOTTIME)
return 0;
if (ts->tv_sec < now.tv_sec) { /* Ignore partial second. */
ts->tv_sec += ONE_DAY;
if (ts->tv_sec < now.tv_sec)
FAIL(EDOM);
if (!strchr(start, ':'))
fprintf(stderr,
"%s: warning: the specified time is in the past, "
"it is being adjust to be tomorrow instead.\n", argv0);
}
return 0;
fail:
return -1;
}
|