| 1 |
%{ |
| 2 |
/* Parse a string into an internal timestamp. |
| 3 |
|
| 4 |
Copyright (C) 1999-2000, 2002-2023 Free Software Foundation, Inc. |
| 5 |
|
| 6 |
This program is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, either version 3 of the License, or |
| 9 |
(at your option) any later version. |
| 10 |
|
| 11 |
This program is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 18 |
|
| 19 |
/* Originally written by Steven M. Bellovin <[email protected]> while |
| 20 |
at the University of North Carolina at Chapel Hill. Later tweaked by |
| 21 |
a couple of people on Usenet. Completely overhauled by Rich $alz |
| 22 |
<[email protected]> and Jim Berets <[email protected]> in August, 1990. |
| 23 |
|
| 24 |
Modified by Assaf Gordon <[email protected]> in 2016 to add |
| 25 |
debug output. |
| 26 |
|
| 27 |
Modified by Paul Eggert <[email protected]> in 1999 to do the |
| 28 |
right thing about local DST. Also modified by Paul Eggert |
| 29 |
<[email protected]> in 2004 to support nanosecond-resolution |
| 30 |
timestamps, in 2004 to support TZ strings in dates, and in 2017 and 2020 to |
| 31 |
check for integer overflow and to support longer-than-'long' |
| 32 |
'time_t' and 'tv_nsec'. */ |
| 33 |
|
| 34 |
#include <config.h> |
| 35 |
|
| 36 |
#include "parse-datetime.h" |
| 37 |
|
| 38 |
#include "idx.h" |
| 39 |
#include "intprops.h" |
| 40 |
#include "timespec.h" |
| 41 |
#include "strftime.h" |
| 42 |
|
| 43 |
/* There's no need to extend the stack, so there's no need to involve |
| 44 |
alloca. */ |
| 45 |
#define YYSTACK_USE_ALLOCA 0 |
| 46 |
|
| 47 |
/* Tell Bison how much stack space is needed. 20 should be plenty for |
| 48 |
this grammar, which is not right recursive. Beware setting it too |
| 49 |
high, since that might cause problems on machines whose |
| 50 |
implementations have lame stack-overflow checking. */ |
| 51 |
#define YYMAXDEPTH 20 |
| 52 |
#define YYINITDEPTH YYMAXDEPTH |
| 53 |
|
| 54 |
#include <inttypes.h> |
| 55 |
#include <c-ctype.h> |
| 56 |
#include <stdarg.h> |
| 57 |
#include <stdckdint.h> |
| 58 |
#include <stdio.h> |
| 59 |
#include <stdlib.h> |
| 60 |
#include <string.h> |
| 61 |
|
| 62 |
#include "gettext.h" |
| 63 |
|
| 64 |
#define _(str) gettext (str) |
| 65 |
|
| 66 |
/* Bison's skeleton tests _STDLIB_H, while some stdlib.h headers |
| 67 |
use _STDLIB_H_ as witness. Map the latter to the one bison uses. */ |
| 68 |
/* FIXME: this is temporary. Remove when we have a mechanism to ensure |
| 69 |
that the version we're using is fixed, too. */ |
| 70 |
#ifdef _STDLIB_H_ |
| 71 |
# undef _STDLIB_H |
| 72 |
# define _STDLIB_H 1 |
| 73 |
#endif |
| 74 |
|
| 75 |
/* Shift A right by B bits portably, by dividing A by 2**B and |
| 76 |
truncating towards minus infinity. A and B should be free of side |
| 77 |
effects, and B should be in the range 0 <= B <= INT_BITS - 2, where |
| 78 |
INT_BITS is the number of useful bits in an int. GNU code can |
| 79 |
assume that INT_BITS is at least 32. |
| 80 |
|
| 81 |
ISO C99 says that A >> B is implementation-defined if A < 0. Some |
| 82 |
implementations (e.g., UNICOS 9.0 on a Cray Y-MP EL) don't shift |
| 83 |
right in the usual way when A < 0, so SHR falls back on division if |
| 84 |
ordinary A >> B doesn't seem to be the usual signed shift. */ |
| 85 |
#define SHR(a, b) \ |
| 86 |
(-1 >> 1 == -1 \ |
| 87 |
? (a) >> (b) \ |
| 88 |
: (a) / (1 << (b)) - ((a) % (1 << (b)) < 0)) |
| 89 |
|
| 90 |
#define HOUR(x) (60 * 60 * (x)) |
| 91 |
|
| 92 |
#define STREQ(a, b) (strcmp (a, b) == 0) |
| 93 |
|
| 94 |
/* Verify that time_t is an integer as POSIX requires, and that every |
| 95 |
time_t value fits in intmax_t. Please file a bug report if these |
| 96 |
assumptions are false on your platform. */ |
| 97 |
static_assert (TYPE_IS_INTEGER (time_t)); |
| 98 |
static_assert (!TYPE_SIGNED (time_t) || INTMAX_MIN <= TYPE_MINIMUM (time_t)); |
| 99 |
static_assert (TYPE_MAXIMUM (time_t) <= INTMAX_MAX); |
| 100 |
|
| 101 |
/* True if N is out of range for time_t. */ |
| 102 |
static bool |
| 103 |
time_overflow (intmax_t n) |
| 104 |
{ |
| 105 |
return ! ((TYPE_SIGNED (time_t) ? TYPE_MINIMUM (time_t) <= n : 0 <= n) |
| 106 |
&& n <= TYPE_MAXIMUM (time_t)); |
| 107 |
} |
| 108 |
|
| 109 |
/* Convert a possibly-signed character to an unsigned character. This is |
| 110 |
a bit safer than casting to unsigned char, since it catches some type |
| 111 |
errors that the cast doesn't. */ |
| 112 |
static unsigned char to_uchar (char ch) { return ch; } |
| 113 |
|
| 114 |
static void _GL_ATTRIBUTE_FORMAT ((__printf__, 1, 2)) |
| 115 |
dbg_printf (char const *msg, ...) |
| 116 |
{ |
| 117 |
va_list args; |
| 118 |
/* TODO: use gnulib's 'program_name' instead? */ |
| 119 |
fputs ("date: ", stderr); |
| 120 |
|
| 121 |
va_start (args, msg); |
| 122 |
vfprintf (stderr, msg, args); |
| 123 |
va_end (args); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
/* An integer value, and the number of digits in its textual |
| 128 |
representation. */ |
| 129 |
typedef struct |
| 130 |
{ |
| 131 |
bool negative; |
| 132 |
intmax_t value; |
| 133 |
idx_t digits; |
| 134 |
} textint; |
| 135 |
|
| 136 |
/* An entry in the lexical lookup table. */ |
| 137 |
typedef struct |
| 138 |
{ |
| 139 |
char const *name; |
| 140 |
int type; |
| 141 |
int value; |
| 142 |
} table; |
| 143 |
|
| 144 |
/* Meridian: am, pm, or 24-hour style. */ |
| 145 |
enum { MERam, MERpm, MER24 }; |
| 146 |
|
| 147 |
/* A reasonable upper bound for the buffer used in debug output. */ |
| 148 |
enum { DBGBUFSIZE = 100 }; |
| 149 |
|
| 150 |
enum { BILLION = 1000000000, LOG10_BILLION = 9 }; |
| 151 |
|
| 152 |
/* Relative times. */ |
| 153 |
typedef struct |
| 154 |
{ |
| 155 |
/* Relative year, month, day, hour, minutes, seconds, and nanoseconds. */ |
| 156 |
intmax_t year; |
| 157 |
intmax_t month; |
| 158 |
intmax_t day; |
| 159 |
intmax_t hour; |
| 160 |
intmax_t minutes; |
| 161 |
intmax_t seconds; |
| 162 |
int ns; |
| 163 |
} relative_time; |
| 164 |
|
| 165 |
#if HAVE_COMPOUND_LITERALS |
| 166 |
# define RELATIVE_TIME_0 ((relative_time) { 0, 0, 0, 0, 0, 0, 0 }) |
| 167 |
#else |
| 168 |
static relative_time const RELATIVE_TIME_0; |
| 169 |
#endif |
| 170 |
|
| 171 |
/* Information passed to and from the parser. */ |
| 172 |
typedef struct |
| 173 |
{ |
| 174 |
/* The input string remaining to be parsed. */ |
| 175 |
const char *input; |
| 176 |
|
| 177 |
/* N, if this is the Nth Tuesday. */ |
| 178 |
intmax_t day_ordinal; |
| 179 |
|
| 180 |
/* Day of week; Sunday is 0. */ |
| 181 |
int day_number; |
| 182 |
|
| 183 |
/* tm_isdst flag for the local zone. */ |
| 184 |
int local_isdst; |
| 185 |
|
| 186 |
/* Time zone, in seconds east of UT. */ |
| 187 |
int time_zone; |
| 188 |
|
| 189 |
/* Style used for time. */ |
| 190 |
int meridian; |
| 191 |
|
| 192 |
/* Gregorian year, month, day, hour, minutes, seconds, and nanoseconds. */ |
| 193 |
textint year; |
| 194 |
intmax_t month; |
| 195 |
intmax_t day; |
| 196 |
intmax_t hour; |
| 197 |
intmax_t minutes; |
| 198 |
struct timespec seconds; /* includes nanoseconds */ |
| 199 |
|
| 200 |
/* Relative year, month, day, hour, minutes, seconds, and nanoseconds. */ |
| 201 |
relative_time rel; |
| 202 |
|
| 203 |
/* Presence or counts of nonterminals of various flavors parsed so far. */ |
| 204 |
bool timespec_seen; |
| 205 |
bool rels_seen; |
| 206 |
idx_t dates_seen; |
| 207 |
idx_t days_seen; |
| 208 |
idx_t J_zones_seen; |
| 209 |
idx_t local_zones_seen; |
| 210 |
idx_t dsts_seen; |
| 211 |
idx_t times_seen; |
| 212 |
idx_t zones_seen; |
| 213 |
bool year_seen; |
| 214 |
|
| 215 |
#ifdef GNULIB_PARSE_DATETIME2 |
| 216 |
/* Print debugging output to stderr. */ |
| 217 |
bool parse_datetime_debug; |
| 218 |
#endif |
| 219 |
|
| 220 |
/* Which of the 'seen' parts have been printed when debugging. */ |
| 221 |
bool debug_dates_seen; |
| 222 |
bool debug_days_seen; |
| 223 |
bool debug_local_zones_seen; |
| 224 |
bool debug_times_seen; |
| 225 |
bool debug_zones_seen; |
| 226 |
bool debug_year_seen; |
| 227 |
|
| 228 |
/* The user specified explicit ordinal day value. */ |
| 229 |
bool debug_ordinal_day_seen; |
| 230 |
|
| 231 |
/* Table of local time zone abbreviations, terminated by a null entry. */ |
| 232 |
table local_time_zone_table[3]; |
| 233 |
} parser_control; |
| 234 |
|
| 235 |
static bool |
| 236 |
debugging (parser_control const *pc) |
| 237 |
{ |
| 238 |
#ifdef GNULIB_PARSE_DATETIME2 |
| 239 |
return pc->parse_datetime_debug; |
| 240 |
#else |
| 241 |
return false; |
| 242 |
#endif |
| 243 |
} |
| 244 |
|
| 245 |
union YYSTYPE; |
| 246 |
static int yylex (union YYSTYPE *, parser_control *); |
| 247 |
static int yyerror (parser_control const *, char const *); |
| 248 |
static bool time_zone_hhmm (parser_control *, textint, intmax_t); |
| 249 |
|
| 250 |
/* Extract into *PC any date and time info from a string of digits |
| 251 |
of the form e.g., YYYYMMDD, YYMMDD, HHMM, HH (and sometimes YYY, |
| 252 |
YYYY, ...). */ |
| 253 |
static void |
| 254 |
digits_to_date_time (parser_control *pc, textint text_int) |
| 255 |
{ |
| 256 |
if (pc->dates_seen && ! pc->year.digits |
| 257 |
&& ! pc->rels_seen && (pc->times_seen || 2 < text_int.digits)) |
| 258 |
{ |
| 259 |
pc->year_seen = true; |
| 260 |
pc->year = text_int; |
| 261 |
} |
| 262 |
else |
| 263 |
{ |
| 264 |
if (4 < text_int.digits) |
| 265 |
{ |
| 266 |
pc->dates_seen++; |
| 267 |
pc->day = text_int.value % 100; |
| 268 |
pc->month = (text_int.value / 100) % 100; |
| 269 |
pc->year.value = text_int.value / 10000; |
| 270 |
pc->year.digits = text_int.digits - 4; |
| 271 |
} |
| 272 |
else |
| 273 |
{ |
| 274 |
pc->times_seen++; |
| 275 |
if (text_int.digits <= 2) |
| 276 |
{ |
| 277 |
pc->hour = text_int.value; |
| 278 |
pc->minutes = 0; |
| 279 |
} |
| 280 |
else |
| 281 |
{ |
| 282 |
pc->hour = text_int.value / 100; |
| 283 |
pc->minutes = text_int.value % 100; |
| 284 |
} |
| 285 |
pc->seconds = (struct timespec) {0}; |
| 286 |
pc->meridian = MER24; |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
/* Increment PC->rel by FACTOR * REL (FACTOR is 1 or -1). Return true |
| 292 |
if successful, false if an overflow occurred. */ |
| 293 |
static bool |
| 294 |
apply_relative_time (parser_control *pc, relative_time rel, int factor) |
| 295 |
{ |
| 296 |
if (factor < 0 |
| 297 |
? (ckd_sub (&pc->rel.ns, pc->rel.ns, rel.ns) |
| 298 |
| ckd_sub (&pc->rel.seconds, pc->rel.seconds, rel.seconds) |
| 299 |
| ckd_sub (&pc->rel.minutes, pc->rel.minutes, rel.minutes) |
| 300 |
| ckd_sub (&pc->rel.hour, pc->rel.hour, rel.hour) |
| 301 |
| ckd_sub (&pc->rel.day, pc->rel.day, rel.day) |
| 302 |
| ckd_sub (&pc->rel.month, pc->rel.month, rel.month) |
| 303 |
| ckd_sub (&pc->rel.year, pc->rel.year, rel.year)) |
| 304 |
: (ckd_add (&pc->rel.ns, pc->rel.ns, rel.ns) |
| 305 |
| ckd_add (&pc->rel.seconds, pc->rel.seconds, rel.seconds) |
| 306 |
| ckd_add (&pc->rel.minutes, pc->rel.minutes, rel.minutes) |
| 307 |
| ckd_add (&pc->rel.hour, pc->rel.hour, rel.hour) |
| 308 |
| ckd_add (&pc->rel.day, pc->rel.day, rel.day) |
| 309 |
| ckd_add (&pc->rel.month, pc->rel.month, rel.month) |
| 310 |
| ckd_add (&pc->rel.year, pc->rel.year, rel.year))) |
| 311 |
return false; |
| 312 |
pc->rels_seen = true; |
| 313 |
return true; |
| 314 |
} |
| 315 |
|
| 316 |
/* Set PC-> hour, minutes, seconds and nanoseconds members from arguments. */ |
| 317 |
static void |
| 318 |
set_hhmmss (parser_control *pc, intmax_t hour, intmax_t minutes, |
| 319 |
time_t sec, int nsec) |
| 320 |
{ |
| 321 |
pc->hour = hour; |
| 322 |
pc->minutes = minutes; |
| 323 |
pc->seconds = (struct timespec) { .tv_sec = sec, .tv_nsec = nsec }; |
| 324 |
} |
| 325 |
|
| 326 |
/* Return a textual representation of the day ordinal/number values |
| 327 |
in the parser_control struct (e.g., "last wed", "this tues", "thu"). */ |
| 328 |
static const char * |
| 329 |
str_days (parser_control *pc, char *buffer, int n) |
| 330 |
{ |
| 331 |
/* TODO: use relative_time_table for reverse lookup. */ |
| 332 |
static char const ordinal_values[][11] = { |
| 333 |
"last", |
| 334 |
"this", |
| 335 |
"next/first", |
| 336 |
"(SECOND)", /* SECOND is commented out in relative_time_table. */ |
| 337 |
"third", |
| 338 |
"fourth", |
| 339 |
"fifth", |
| 340 |
"sixth", |
| 341 |
"seventh", |
| 342 |
"eight", |
| 343 |
"ninth", |
| 344 |
"tenth", |
| 345 |
"eleventh", |
| 346 |
"twelfth" |
| 347 |
}; |
| 348 |
|
| 349 |
static char const days_values[][4] = { |
| 350 |
"Sun", |
| 351 |
"Mon", |
| 352 |
"Tue", |
| 353 |
"Wed", |
| 354 |
"Thu", |
| 355 |
"Fri", |
| 356 |
"Sat" |
| 357 |
}; |
| 358 |
|
| 359 |
int len; |
| 360 |
|
| 361 |
/* Don't add an ordinal prefix if the user didn't specify it |
| 362 |
(e.g., "this wed" vs "wed"). */ |
| 363 |
if (pc->debug_ordinal_day_seen) |
| 364 |
{ |
| 365 |
/* Use word description if possible (e.g., -1 = last, 3 = third). */ |
| 366 |
len = (-1 <= pc->day_ordinal && pc->day_ordinal <= 12 |
| 367 |
? snprintf (buffer, n, "%s", ordinal_values[pc->day_ordinal + 1]) |
| 368 |
: snprintf (buffer, n, "%"PRIdMAX, pc->day_ordinal)); |
| 369 |
} |
| 370 |
else |
| 371 |
{ |
| 372 |
buffer[0] = '\0'; |
| 373 |
len = 0; |
| 374 |
} |
| 375 |
|
| 376 |
/* Add the day name */ |
| 377 |
if (0 <= pc->day_number && pc->day_number <= 6 && 0 <= len && len < n) |
| 378 |
snprintf (buffer + len, n - len, &" %s"[len == 0], |
| 379 |
days_values[pc->day_number]); |
| 380 |
else |
| 381 |
{ |
| 382 |
/* invalid day_number value - should never happen */ |
| 383 |
} |
| 384 |
return buffer; |
| 385 |
} |
| 386 |
|
| 387 |
/* Convert a time zone to its string representation. */ |
| 388 |
|
| 389 |
enum { TIME_ZONE_BUFSIZE = INT_STRLEN_BOUND (intmax_t) + sizeof ":MM:SS" } ; |
| 390 |
|
| 391 |
static char const * |
| 392 |
time_zone_str (int time_zone, char time_zone_buf[TIME_ZONE_BUFSIZE]) |
| 393 |
{ |
| 394 |
char *p = time_zone_buf; |
| 395 |
char sign = time_zone < 0 ? '-' : '+'; |
| 396 |
int hour = abs (time_zone / (60 * 60)); |
| 397 |
p += sprintf (time_zone_buf, "%c%02d", sign, hour); |
| 398 |
int offset_from_hour = abs (time_zone % (60 * 60)); |
| 399 |
if (offset_from_hour != 0) |
| 400 |
{ |
| 401 |
int mm = offset_from_hour / 60; |
| 402 |
int ss = offset_from_hour % 60; |
| 403 |
*p++ = ':'; |
| 404 |
*p++ = '0' + mm / 10; |
| 405 |
*p++ = '0' + mm % 10; |
| 406 |
if (ss) |
| 407 |
{ |
| 408 |
*p++ = ':'; |
| 409 |
*p++ = '0' + ss / 10; |
| 410 |
*p++ = '0' + ss % 10; |
| 411 |
} |
| 412 |
*p = '\0'; |
| 413 |
} |
| 414 |
return time_zone_buf; |
| 415 |
} |
| 416 |
|
| 417 |
/* debugging: print the current time in the parser_control structure. |
| 418 |
The parser will increment "*_seen" members for those which were parsed. |
| 419 |
This function will print only newly seen parts. */ |
| 420 |
static void |
| 421 |
debug_print_current_time (char const *item, parser_control *pc) |
| 422 |
{ |
| 423 |
bool space = false; |
| 424 |
|
| 425 |
if (!debugging (pc)) |
| 426 |
return; |
| 427 |
|
| 428 |
/* no newline, more items printed below */ |
| 429 |
dbg_printf (_("parsed %s part: "), item); |
| 430 |
|
| 431 |
if (pc->dates_seen && !pc->debug_dates_seen) |
| 432 |
{ |
| 433 |
/*TODO: use pc->year.negative? */ |
| 434 |
fprintf (stderr, "(Y-M-D) %04"PRIdMAX"-%02"PRIdMAX"-%02"PRIdMAX, |
| 435 |
pc->year.value, pc->month, pc->day); |
| 436 |
pc->debug_dates_seen = true; |
| 437 |
space = true; |
| 438 |
} |
| 439 |
|
| 440 |
if (pc->year_seen != pc->debug_year_seen) |
| 441 |
{ |
| 442 |
if (space) |
| 443 |
fputc (' ', stderr); |
| 444 |
fprintf (stderr, _("year: %04"PRIdMAX), pc->year.value); |
| 445 |
|
| 446 |
pc->debug_year_seen = pc->year_seen; |
| 447 |
space = true; |
| 448 |
} |
| 449 |
|
| 450 |
if (pc->times_seen && !pc->debug_times_seen) |
| 451 |
{ |
| 452 |
intmax_t sec = pc->seconds.tv_sec; |
| 453 |
fprintf (stderr, &" %02"PRIdMAX":%02"PRIdMAX":%02"PRIdMAX[!space], |
| 454 |
pc->hour, pc->minutes, sec); |
| 455 |
if (pc->seconds.tv_nsec != 0) |
| 456 |
{ |
| 457 |
int nsec = pc->seconds.tv_nsec; |
| 458 |
fprintf (stderr, ".%09d", nsec); |
| 459 |
} |
| 460 |
if (pc->meridian == MERpm) |
| 461 |
fputs ("pm", stderr); |
| 462 |
|
| 463 |
pc->debug_times_seen = true; |
| 464 |
space = true; |
| 465 |
} |
| 466 |
|
| 467 |
if (pc->days_seen && !pc->debug_days_seen) |
| 468 |
{ |
| 469 |
if (space) |
| 470 |
fputc (' ', stderr); |
| 471 |
char tmp[DBGBUFSIZE]; |
| 472 |
fprintf (stderr, _("%s (day ordinal=%"PRIdMAX" number=%d)"), |
| 473 |
str_days (pc, tmp, sizeof tmp), |
| 474 |
pc->day_ordinal, pc->day_number); |
| 475 |
pc->debug_days_seen = true; |
| 476 |
space = true; |
| 477 |
} |
| 478 |
|
| 479 |
/* local zone strings only change the DST settings, |
| 480 |
not the timezone value. If seen, inform about the DST. */ |
| 481 |
if (pc->local_zones_seen && !pc->debug_local_zones_seen) |
| 482 |
{ |
| 483 |
fprintf (stderr, &" isdst=%d%s"[!space], |
| 484 |
pc->local_isdst, pc->dsts_seen ? " DST" : ""); |
| 485 |
pc->debug_local_zones_seen = true; |
| 486 |
space = true; |
| 487 |
} |
| 488 |
|
| 489 |
if (pc->zones_seen && !pc->debug_zones_seen) |
| 490 |
{ |
| 491 |
char time_zone_buf[TIME_ZONE_BUFSIZE]; |
| 492 |
fprintf (stderr, &" UTC%s"[!space], |
| 493 |
time_zone_str (pc->time_zone, time_zone_buf)); |
| 494 |
pc->debug_zones_seen = true; |
| 495 |
space = true; |
| 496 |
} |
| 497 |
|
| 498 |
if (pc->timespec_seen) |
| 499 |
{ |
| 500 |
intmax_t sec = pc->seconds.tv_sec; |
| 501 |
if (space) |
| 502 |
fputc (' ', stderr); |
| 503 |
fprintf (stderr, _("number of seconds: %"PRIdMAX), sec); |
| 504 |
} |
| 505 |
|
| 506 |
fputc ('\n', stderr); |
| 507 |
} |
| 508 |
|
| 509 |
/* Debugging: print the current relative values. */ |
| 510 |
|
| 511 |
static bool |
| 512 |
print_rel_part (bool space, intmax_t val, char const *name) |
| 513 |
{ |
| 514 |
if (val == 0) |
| 515 |
return space; |
| 516 |
fprintf (stderr, &" %+"PRIdMAX" %s"[!space], val, name); |
| 517 |
return true; |
| 518 |
} |
| 519 |
|
| 520 |
static void |
| 521 |
debug_print_relative_time (char const *item, parser_control const *pc) |
| 522 |
{ |
| 523 |
bool space = false; |
| 524 |
|
| 525 |
if (!debugging (pc)) |
| 526 |
return; |
| 527 |
|
| 528 |
/* no newline, more items printed below */ |
| 529 |
dbg_printf (_("parsed %s part: "), item); |
| 530 |
|
| 531 |
if (pc->rel.year == 0 && pc->rel.month == 0 && pc->rel.day == 0 |
| 532 |
&& pc->rel.hour == 0 && pc->rel.minutes == 0 && pc->rel.seconds == 0 |
| 533 |
&& pc->rel.ns == 0) |
| 534 |
{ |
| 535 |
/* Special case: relative time of this/today/now */ |
| 536 |
fputs (_("today/this/now\n"), stderr); |
| 537 |
return; |
| 538 |
} |
| 539 |
|
| 540 |
space = print_rel_part (space, pc->rel.year, "year(s)"); |
| 541 |
space = print_rel_part (space, pc->rel.month, "month(s)"); |
| 542 |
space = print_rel_part (space, pc->rel.day, "day(s)"); |
| 543 |
space = print_rel_part (space, pc->rel.hour, "hour(s)"); |
| 544 |
space = print_rel_part (space, pc->rel.minutes, "minutes"); |
| 545 |
space = print_rel_part (space, pc->rel.seconds, "seconds"); |
| 546 |
print_rel_part (space, pc->rel.ns, "nanoseconds"); |
| 547 |
|
| 548 |
fputc ('\n', stderr); |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
%} |
| 554 |
|
| 555 |
/* We want a reentrant parser, even if the TZ manipulation and the calls to |
| 556 |
localtime and gmtime are not reentrant. */ |
| 557 |
%define api.pure |
| 558 |
%parse-param { parser_control *pc } |
| 559 |
%lex-param { parser_control *pc } |
| 560 |
|
| 561 |
/* This grammar has 31 shift/reduce conflicts. */ |
| 562 |
%expect 31 |
| 563 |
|
| 564 |
%union |
| 565 |
{ |
| 566 |
intmax_t intval; |
| 567 |
textint textintval; |
| 568 |
struct timespec timespec; |
| 569 |
relative_time rel; |
| 570 |
} |
| 571 |
|
| 572 |
%token <intval> tAGO |
| 573 |
%token tDST |
| 574 |
|
| 575 |
%token tYEAR_UNIT tMONTH_UNIT tHOUR_UNIT tMINUTE_UNIT tSEC_UNIT |
| 576 |
%token <intval> tDAY_UNIT tDAY_SHIFT |
| 577 |
|
| 578 |
%token <intval> tDAY tDAYZONE tLOCAL_ZONE tMERIDIAN |
| 579 |
%token <intval> tMONTH tORDINAL tZONE |
| 580 |
|
| 581 |
%token <textintval> tSNUMBER tUNUMBER |
| 582 |
%token <timespec> tSDECIMAL_NUMBER tUDECIMAL_NUMBER |
| 583 |
|
| 584 |
%type <intval> o_colon_minutes |
| 585 |
%type <timespec> seconds signed_seconds unsigned_seconds |
| 586 |
|
| 587 |
%type <rel> relunit relunit_snumber dayshift |
| 588 |
|
| 589 |
%% |
| 590 |
|
| 591 |
spec: |
| 592 |
timespec |
| 593 |
| items |
| 594 |
; |
| 595 |
|
| 596 |
timespec: |
| 597 |
'@' seconds |
| 598 |
{ |
| 599 |
pc->seconds = $2; |
| 600 |
pc->timespec_seen = true; |
| 601 |
debug_print_current_time (_("number of seconds"), pc); |
| 602 |
} |
| 603 |
; |
| 604 |
|
| 605 |
items: |
| 606 |
/* empty */ |
| 607 |
| items item |
| 608 |
; |
| 609 |
|
| 610 |
item: |
| 611 |
datetime |
| 612 |
{ |
| 613 |
pc->times_seen++; pc->dates_seen++; |
| 614 |
debug_print_current_time (_("datetime"), pc); |
| 615 |
} |
| 616 |
| time |
| 617 |
{ |
| 618 |
pc->times_seen++; |
| 619 |
debug_print_current_time (_("time"), pc); |
| 620 |
} |
| 621 |
| local_zone |
| 622 |
{ |
| 623 |
pc->local_zones_seen++; |
| 624 |
debug_print_current_time (_("local_zone"), pc); |
| 625 |
} |
| 626 |
| 'J' |
| 627 |
{ |
| 628 |
pc->J_zones_seen++; |
| 629 |
debug_print_current_time ("J", pc); |
| 630 |
} |
| 631 |
| zone |
| 632 |
{ |
| 633 |
pc->zones_seen++; |
| 634 |
debug_print_current_time (_("zone"), pc); |
| 635 |
} |
| 636 |
| date |
| 637 |
{ |
| 638 |
pc->dates_seen++; |
| 639 |
debug_print_current_time (_("date"), pc); |
| 640 |
} |
| 641 |
| day |
| 642 |
{ |
| 643 |
pc->days_seen++; |
| 644 |
debug_print_current_time (_("day"), pc); |
| 645 |
} |
| 646 |
| rel |
| 647 |
{ |
| 648 |
debug_print_relative_time (_("relative"), pc); |
| 649 |
} |
| 650 |
| number |
| 651 |
{ |
| 652 |
debug_print_current_time (_("number"), pc); |
| 653 |
} |
| 654 |
| hybrid |
| 655 |
{ |
| 656 |
debug_print_relative_time (_("hybrid"), pc); |
| 657 |
} |
| 658 |
; |
| 659 |
|
| 660 |
datetime: |
| 661 |
iso_8601_datetime |
| 662 |
; |
| 663 |
|
| 664 |
iso_8601_datetime: |
| 665 |
iso_8601_date 'T' iso_8601_time |
| 666 |
; |
| 667 |
|
| 668 |
time: |
| 669 |
tUNUMBER tMERIDIAN |
| 670 |
{ |
| 671 |
set_hhmmss (pc, $1.value, 0, 0, 0); |
| 672 |
pc->meridian = $2; |
| 673 |
} |
| 674 |
| tUNUMBER ':' tUNUMBER tMERIDIAN |
| 675 |
{ |
| 676 |
set_hhmmss (pc, $1.value, $3.value, 0, 0); |
| 677 |
pc->meridian = $4; |
| 678 |
} |
| 679 |
| tUNUMBER ':' tUNUMBER ':' unsigned_seconds tMERIDIAN |
| 680 |
{ |
| 681 |
set_hhmmss (pc, $1.value, $3.value, $5.tv_sec, $5.tv_nsec); |
| 682 |
pc->meridian = $6; |
| 683 |
} |
| 684 |
| iso_8601_time |
| 685 |
; |
| 686 |
|
| 687 |
iso_8601_time: |
| 688 |
tUNUMBER zone_offset |
| 689 |
{ |
| 690 |
set_hhmmss (pc, $1.value, 0, 0, 0); |
| 691 |
pc->meridian = MER24; |
| 692 |
} |
| 693 |
| tUNUMBER ':' tUNUMBER o_zone_offset |
| 694 |
{ |
| 695 |
set_hhmmss (pc, $1.value, $3.value, 0, 0); |
| 696 |
pc->meridian = MER24; |
| 697 |
} |
| 698 |
| tUNUMBER ':' tUNUMBER ':' unsigned_seconds o_zone_offset |
| 699 |
{ |
| 700 |
set_hhmmss (pc, $1.value, $3.value, $5.tv_sec, $5.tv_nsec); |
| 701 |
pc->meridian = MER24; |
| 702 |
} |
| 703 |
; |
| 704 |
|
| 705 |
o_zone_offset: |
| 706 |
/* empty */ |
| 707 |
| zone_offset |
| 708 |
; |
| 709 |
|
| 710 |
zone_offset: |
| 711 |
tSNUMBER o_colon_minutes |
| 712 |
{ |
| 713 |
pc->zones_seen++; |
| 714 |
if (! time_zone_hhmm (pc, $1, $2)) YYABORT; |
| 715 |
} |
| 716 |
; |
| 717 |
|
| 718 |
/* Local zone strings affect only the DST setting, and take effect |
| 719 |
only if the current TZ setting is relevant. |
| 720 |
|
| 721 |
Example 1: |
| 722 |
'EEST' is parsed as tLOCAL_ZONE, as it relates to the effective TZ: |
| 723 |
TZ='Europe/Helsinki' date -d '2016-06-30 EEST' |
| 724 |
|
| 725 |
Example 2: |
| 726 |
'EEST' is parsed as tDAYZONE: |
| 727 |
TZ='Asia/Tokyo' date -d '2016-06-30 EEST' |
| 728 |
|
| 729 |
This is implemented by probing the next three calendar quarters |
| 730 |
of the effective timezone and looking for DST changes - |
| 731 |
if found, the timezone name (EEST) is inserted into |
| 732 |
the lexical lookup table with type tLOCAL_ZONE. |
| 733 |
(Search for 'quarter' comment in 'parse_datetime2'.) |
| 734 |
*/ |
| 735 |
local_zone: |
| 736 |
tLOCAL_ZONE |
| 737 |
{ pc->local_isdst = $1; } |
| 738 |
| tLOCAL_ZONE tDST |
| 739 |
{ |
| 740 |
pc->local_isdst = 1; |
| 741 |
pc->dsts_seen++; |
| 742 |
} |
| 743 |
; |
| 744 |
|
| 745 |
/* Note 'T' is a special case, as it is used as the separator in ISO |
| 746 |
8601 date and time of day representation. */ |
| 747 |
zone: |
| 748 |
tZONE |
| 749 |
{ pc->time_zone = $1; } |
| 750 |
| 'T' |
| 751 |
{ pc->time_zone = -HOUR (7); } |
| 752 |
| tZONE relunit_snumber |
| 753 |
{ pc->time_zone = $1; |
| 754 |
if (! apply_relative_time (pc, $2, 1)) YYABORT; |
| 755 |
debug_print_relative_time (_("relative"), pc); |
| 756 |
} |
| 757 |
| 'T' relunit_snumber |
| 758 |
{ pc->time_zone = -HOUR (7); |
| 759 |
if (! apply_relative_time (pc, $2, 1)) YYABORT; |
| 760 |
debug_print_relative_time (_("relative"), pc); |
| 761 |
} |
| 762 |
| tZONE tSNUMBER o_colon_minutes |
| 763 |
{ if (! time_zone_hhmm (pc, $2, $3)) YYABORT; |
| 764 |
if (ckd_add (&pc->time_zone, pc->time_zone, $1)) YYABORT; } |
| 765 |
| tDAYZONE |
| 766 |
{ pc->time_zone = $1 + 60 * 60; } |
| 767 |
| tZONE tDST |
| 768 |
{ pc->time_zone = $1 + 60 * 60; } |
| 769 |
; |
| 770 |
|
| 771 |
day: |
| 772 |
tDAY |
| 773 |
{ |
| 774 |
pc->day_ordinal = 0; |
| 775 |
pc->day_number = $1; |
| 776 |
} |
| 777 |
| tDAY ',' |
| 778 |
{ |
| 779 |
pc->day_ordinal = 0; |
| 780 |
pc->day_number = $1; |
| 781 |
} |
| 782 |
| tORDINAL tDAY |
| 783 |
{ |
| 784 |
pc->day_ordinal = $1; |
| 785 |
pc->day_number = $2; |
| 786 |
pc->debug_ordinal_day_seen = true; |
| 787 |
} |
| 788 |
| tUNUMBER tDAY |
| 789 |
{ |
| 790 |
pc->day_ordinal = $1.value; |
| 791 |
pc->day_number = $2; |
| 792 |
pc->debug_ordinal_day_seen = true; |
| 793 |
} |
| 794 |
; |
| 795 |
|
| 796 |
date: |
| 797 |
tUNUMBER '/' tUNUMBER |
| 798 |
{ |
| 799 |
pc->month = $1.value; |
| 800 |
pc->day = $3.value; |
| 801 |
} |
| 802 |
| tUNUMBER '/' tUNUMBER '/' tUNUMBER |
| 803 |
{ |
| 804 |
/* Interpret as YYYY/MM/DD if the first value has 4 or more digits, |
| 805 |
otherwise as MM/DD/YY. |
| 806 |
The goal in recognizing YYYY/MM/DD is solely to support legacy |
| 807 |
machine-generated dates like those in an RCS log listing. If |
| 808 |
you want portability, use the ISO 8601 format. */ |
| 809 |
if (4 <= $1.digits) |
| 810 |
{ |
| 811 |
if (debugging (pc)) |
| 812 |
{ |
| 813 |
intmax_t digits = $1.digits; |
| 814 |
dbg_printf (_("warning: value %"PRIdMAX" has %"PRIdMAX" digits. " |
| 815 |
"Assuming YYYY/MM/DD\n"), |
| 816 |
$1.value, digits); |
| 817 |
} |
| 818 |
|
| 819 |
pc->year = $1; |
| 820 |
pc->month = $3.value; |
| 821 |
pc->day = $5.value; |
| 822 |
} |
| 823 |
else |
| 824 |
{ |
| 825 |
if (debugging (pc)) |
| 826 |
dbg_printf (_("warning: value %"PRIdMAX" has less than 4 digits. " |
| 827 |
"Assuming MM/DD/YY[YY]\n"), |
| 828 |
$1.value); |
| 829 |
|
| 830 |
pc->month = $1.value; |
| 831 |
pc->day = $3.value; |
| 832 |
pc->year = $5; |
| 833 |
} |
| 834 |
} |
| 835 |
| tUNUMBER tMONTH tSNUMBER |
| 836 |
{ |
| 837 |
/* E.g., 17-JUN-1992. */ |
| 838 |
pc->day = $1.value; |
| 839 |
pc->month = $2; |
| 840 |
if (ckd_sub (&pc->year.value, 0, $3.value)) YYABORT; |
| 841 |
pc->year.digits = $3.digits; |
| 842 |
} |
| 843 |
| tMONTH tSNUMBER tSNUMBER |
| 844 |
{ |
| 845 |
/* E.g., JUN-17-1992. */ |
| 846 |
pc->month = $1; |
| 847 |
if (ckd_sub (&pc->day, 0, $2.value)) YYABORT; |
| 848 |
if (ckd_sub (&pc->year.value, 0, $3.value)) YYABORT; |
| 849 |
pc->year.digits = $3.digits; |
| 850 |
} |
| 851 |
| tMONTH tUNUMBER |
| 852 |
{ |
| 853 |
pc->month = $1; |
| 854 |
pc->day = $2.value; |
| 855 |
} |
| 856 |
| tMONTH tUNUMBER ',' tUNUMBER |
| 857 |
{ |
| 858 |
pc->month = $1; |
| 859 |
pc->day = $2.value; |
| 860 |
pc->year = $4; |
| 861 |
} |
| 862 |
| tUNUMBER tMONTH |
| 863 |
{ |
| 864 |
pc->day = $1.value; |
| 865 |
pc->month = $2; |
| 866 |
} |
| 867 |
| tUNUMBER tMONTH tUNUMBER |
| 868 |
{ |
| 869 |
pc->day = $1.value; |
| 870 |
pc->month = $2; |
| 871 |
pc->year = $3; |
| 872 |
} |
| 873 |
| iso_8601_date |
| 874 |
; |
| 875 |
|
| 876 |
iso_8601_date: |
| 877 |
tUNUMBER tSNUMBER tSNUMBER |
| 878 |
{ |
| 879 |
/* ISO 8601 format. YYYY-MM-DD. */ |
| 880 |
pc->year = $1; |
| 881 |
if (ckd_sub (&pc->month, 0, $2.value)) YYABORT; |
| 882 |
if (ckd_sub (&pc->day, 0, $3.value)) YYABORT; |
| 883 |
} |
| 884 |
; |
| 885 |
|
| 886 |
rel: |
| 887 |
relunit tAGO |
| 888 |
{ if (! apply_relative_time (pc, $1, $2)) YYABORT; } |
| 889 |
| relunit |
| 890 |
{ if (! apply_relative_time (pc, $1, 1)) YYABORT; } |
| 891 |
| dayshift |
| 892 |
{ if (! apply_relative_time (pc, $1, 1)) YYABORT; } |
| 893 |
; |
| 894 |
|
| 895 |
relunit: |
| 896 |
tORDINAL tYEAR_UNIT |
| 897 |
{ $$ = RELATIVE_TIME_0; $$.year = $1; } |
| 898 |
| tUNUMBER tYEAR_UNIT |
| 899 |
{ $$ = RELATIVE_TIME_0; $$.year = $1.value; } |
| 900 |
| tYEAR_UNIT |
| 901 |
{ $$ = RELATIVE_TIME_0; $$.year = 1; } |
| 902 |
| tORDINAL tMONTH_UNIT |
| 903 |
{ $$ = RELATIVE_TIME_0; $$.month = $1; } |
| 904 |
| tUNUMBER tMONTH_UNIT |
| 905 |
{ $$ = RELATIVE_TIME_0; $$.month = $1.value; } |
| 906 |
| tMONTH_UNIT |
| 907 |
{ $$ = RELATIVE_TIME_0; $$.month = 1; } |
| 908 |
| tORDINAL tDAY_UNIT |
| 909 |
{ $$ = RELATIVE_TIME_0; |
| 910 |
if (ckd_mul (&$$.day, $1, $2)) YYABORT; } |
| 911 |
| tUNUMBER tDAY_UNIT |
| 912 |
{ $$ = RELATIVE_TIME_0; |
| 913 |
if (ckd_mul (&$$.day, $1.value, $2)) YYABORT; } |
| 914 |
| tDAY_UNIT |
| 915 |
{ $$ = RELATIVE_TIME_0; $$.day = $1; } |
| 916 |
| tORDINAL tHOUR_UNIT |
| 917 |
{ $$ = RELATIVE_TIME_0; $$.hour = $1; } |
| 918 |
| tUNUMBER tHOUR_UNIT |
| 919 |
{ $$ = RELATIVE_TIME_0; $$.hour = $1.value; } |
| 920 |
| tHOUR_UNIT |
| 921 |
{ $$ = RELATIVE_TIME_0; $$.hour = 1; } |
| 922 |
| tORDINAL tMINUTE_UNIT |
| 923 |
{ $$ = RELATIVE_TIME_0; $$.minutes = $1; } |
| 924 |
| tUNUMBER tMINUTE_UNIT |
| 925 |
{ $$ = RELATIVE_TIME_0; $$.minutes = $1.value; } |
| 926 |
| tMINUTE_UNIT |
| 927 |
{ $$ = RELATIVE_TIME_0; $$.minutes = 1; } |
| 928 |
| tORDINAL tSEC_UNIT |
| 929 |
{ $$ = RELATIVE_TIME_0; $$.seconds = $1; } |
| 930 |
| tUNUMBER tSEC_UNIT |
| 931 |
{ $$ = RELATIVE_TIME_0; $$.seconds = $1.value; } |
| 932 |
| tSDECIMAL_NUMBER tSEC_UNIT |
| 933 |
{ $$ = RELATIVE_TIME_0; $$.seconds = $1.tv_sec; $$.ns = $1.tv_nsec; } |
| 934 |
| tUDECIMAL_NUMBER tSEC_UNIT |
| 935 |
{ $$ = RELATIVE_TIME_0; $$.seconds = $1.tv_sec; $$.ns = $1.tv_nsec; } |
| 936 |
| tSEC_UNIT |
| 937 |
{ $$ = RELATIVE_TIME_0; $$.seconds = 1; } |
| 938 |
| relunit_snumber |
| 939 |
; |
| 940 |
|
| 941 |
relunit_snumber: |
| 942 |
tSNUMBER tYEAR_UNIT |
| 943 |
{ $$ = RELATIVE_TIME_0; $$.year = $1.value; } |
| 944 |
| tSNUMBER tMONTH_UNIT |
| 945 |
{ $$ = RELATIVE_TIME_0; $$.month = $1.value; } |
| 946 |
| tSNUMBER tDAY_UNIT |
| 947 |
{ $$ = RELATIVE_TIME_0; |
| 948 |
if (ckd_mul (&$$.day, $1.value, $2)) YYABORT; } |
| 949 |
| tSNUMBER tHOUR_UNIT |
| 950 |
{ $$ = RELATIVE_TIME_0; $$.hour = $1.value; } |
| 951 |
| tSNUMBER tMINUTE_UNIT |
| 952 |
{ $$ = RELATIVE_TIME_0; $$.minutes = $1.value; } |
| 953 |
| tSNUMBER tSEC_UNIT |
| 954 |
{ $$ = RELATIVE_TIME_0; $$.seconds = $1.value; } |
| 955 |
; |
| 956 |
|
| 957 |
dayshift: |
| 958 |
tDAY_SHIFT |
| 959 |
{ $$ = RELATIVE_TIME_0; $$.day = $1; } |
| 960 |
; |
| 961 |
|
| 962 |
seconds: signed_seconds | unsigned_seconds; |
| 963 |
|
| 964 |
signed_seconds: |
| 965 |
tSDECIMAL_NUMBER |
| 966 |
| tSNUMBER |
| 967 |
{ if (time_overflow ($1.value)) YYABORT; |
| 968 |
$$ = (struct timespec) { .tv_sec = $1.value }; } |
| 969 |
; |
| 970 |
|
| 971 |
unsigned_seconds: |
| 972 |
tUDECIMAL_NUMBER |
| 973 |
| tUNUMBER |
| 974 |
{ if (time_overflow ($1.value)) YYABORT; |
| 975 |
$$ = (struct timespec) { .tv_sec = $1.value }; } |
| 976 |
; |
| 977 |
|
| 978 |
number: |
| 979 |
tUNUMBER |
| 980 |
{ digits_to_date_time (pc, $1); } |
| 981 |
; |
| 982 |
|
| 983 |
hybrid: |
| 984 |
tUNUMBER relunit_snumber |
| 985 |
{ |
| 986 |
/* Hybrid all-digit and relative offset, so that we accept e.g., |
| 987 |
"YYYYMMDD +N days" as well as "YYYYMMDD N days". */ |
| 988 |
digits_to_date_time (pc, $1); |
| 989 |
if (! apply_relative_time (pc, $2, 1)) YYABORT; |
| 990 |
} |
| 991 |
; |
| 992 |
|
| 993 |
o_colon_minutes: |
| 994 |
/* empty */ |
| 995 |
{ $$ = -1; } |
| 996 |
| ':' tUNUMBER |
| 997 |
{ $$ = $2.value; } |
| 998 |
; |
| 999 |
|
| 1000 |
%% |
| 1001 |
|
| 1002 |
static table const meridian_table[] = |
| 1003 |
{ |
| 1004 |
{ "AM", tMERIDIAN, MERam }, |
| 1005 |
{ "A.M.", tMERIDIAN, MERam }, |
| 1006 |
{ "PM", tMERIDIAN, MERpm }, |
| 1007 |
{ "P.M.", tMERIDIAN, MERpm }, |
| 1008 |
{ NULL, 0, 0 } |
| 1009 |
}; |
| 1010 |
|
| 1011 |
static table const dst_table[] = |
| 1012 |
{ |
| 1013 |
{ "DST", tDST, 0 } |
| 1014 |
}; |
| 1015 |
|
| 1016 |
static table const month_and_day_table[] = |
| 1017 |
{ |
| 1018 |
{ "JANUARY", tMONTH, 1 }, |
| 1019 |
{ "FEBRUARY", tMONTH, 2 }, |
| 1020 |
{ "MARCH", tMONTH, 3 }, |
| 1021 |
{ "APRIL", tMONTH, 4 }, |
| 1022 |
{ "MAY", tMONTH, 5 }, |
| 1023 |
{ "JUNE", tMONTH, 6 }, |
| 1024 |
{ "JULY", tMONTH, 7 }, |
| 1025 |
{ "AUGUST", tMONTH, 8 }, |
| 1026 |
{ "SEPTEMBER",tMONTH, 9 }, |
| 1027 |
{ "SEPT", tMONTH, 9 }, |
| 1028 |
{ "OCTOBER", tMONTH, 10 }, |
| 1029 |
{ "NOVEMBER", tMONTH, 11 }, |
| 1030 |
{ "DECEMBER", tMONTH, 12 }, |
| 1031 |
{ "SUNDAY", tDAY, 0 }, |
| 1032 |
{ "MONDAY", tDAY, 1 }, |
| 1033 |
{ "TUESDAY", tDAY, 2 }, |
| 1034 |
{ "TUES", tDAY, 2 }, |
| 1035 |
{ "WEDNESDAY",tDAY, 3 }, |
| 1036 |
{ "WEDNES", tDAY, 3 }, |
| 1037 |
{ "THURSDAY", tDAY, 4 }, |
| 1038 |
{ "THUR", tDAY, 4 }, |
| 1039 |
{ "THURS", tDAY, 4 }, |
| 1040 |
{ "FRIDAY", tDAY, 5 }, |
| 1041 |
{ "SATURDAY", tDAY, 6 }, |
| 1042 |
{ NULL, 0, 0 } |
| 1043 |
}; |
| 1044 |
|
| 1045 |
static table const time_units_table[] = |
| 1046 |
{ |
| 1047 |
{ "YEAR", tYEAR_UNIT, 1 }, |
| 1048 |
{ "MONTH", tMONTH_UNIT, 1 }, |
| 1049 |
{ "FORTNIGHT",tDAY_UNIT, 14 }, |
| 1050 |
{ "WEEK", tDAY_UNIT, 7 }, |
| 1051 |
{ "DAY", tDAY_UNIT, 1 }, |
| 1052 |
{ "HOUR", tHOUR_UNIT, 1 }, |
| 1053 |
{ "MINUTE", tMINUTE_UNIT, 1 }, |
| 1054 |
{ "MIN", tMINUTE_UNIT, 1 }, |
| 1055 |
{ "SECOND", tSEC_UNIT, 1 }, |
| 1056 |
{ "SEC", tSEC_UNIT, 1 }, |
| 1057 |
{ NULL, 0, 0 } |
| 1058 |
}; |
| 1059 |
|
| 1060 |
/* Assorted relative-time words. */ |
| 1061 |
static table const relative_time_table[] = |
| 1062 |
{ |
| 1063 |
{ "TOMORROW", tDAY_SHIFT, 1 }, |
| 1064 |
{ "YESTERDAY",tDAY_SHIFT, -1 }, |
| 1065 |
{ "TODAY", tDAY_SHIFT, 0 }, |
| 1066 |
{ "NOW", tDAY_SHIFT, 0 }, |
| 1067 |
{ "LAST", tORDINAL, -1 }, |
| 1068 |
{ "THIS", tORDINAL, 0 }, |
| 1069 |
{ "NEXT", tORDINAL, 1 }, |
| 1070 |
{ "FIRST", tORDINAL, 1 }, |
| 1071 |
/*{ "SECOND", tORDINAL, 2 }, */ |
| 1072 |
{ "THIRD", tORDINAL, 3 }, |
| 1073 |
{ "FOURTH", tORDINAL, 4 }, |
| 1074 |
{ "FIFTH", tORDINAL, 5 }, |
| 1075 |
{ "SIXTH", tORDINAL, 6 }, |
| 1076 |
{ "SEVENTH", tORDINAL, 7 }, |
| 1077 |
{ "EIGHTH", tORDINAL, 8 }, |
| 1078 |
{ "NINTH", tORDINAL, 9 }, |
| 1079 |
{ "TENTH", tORDINAL, 10 }, |
| 1080 |
{ "ELEVENTH", tORDINAL, 11 }, |
| 1081 |
{ "TWELFTH", tORDINAL, 12 }, |
| 1082 |
{ "AGO", tAGO, -1 }, |
| 1083 |
{ "HENCE", tAGO, 1 }, |
| 1084 |
{ NULL, 0, 0 } |
| 1085 |
}; |
| 1086 |
|
| 1087 |
/* The universal time zone table. These labels can be used even for |
| 1088 |
timestamps that would not otherwise be valid, e.g., GMT timestamps |
| 1089 |
oin London during summer. */ |
| 1090 |
static table const universal_time_zone_table[] = |
| 1091 |
{ |
| 1092 |
{ "GMT", tZONE, HOUR ( 0) }, /* Greenwich Mean */ |
| 1093 |
{ "UT", tZONE, HOUR ( 0) }, /* Universal (Coordinated) */ |
| 1094 |
{ "UTC", tZONE, HOUR ( 0) }, |
| 1095 |
{ NULL, 0, 0 } |
| 1096 |
}; |
| 1097 |
|
| 1098 |
/* The time zone table. This table is necessarily incomplete, as time |
| 1099 |
zone abbreviations are ambiguous; e.g., Australians interpret "EST" |
| 1100 |
as Eastern time in Australia, not as US Eastern Standard Time. |
| 1101 |
You cannot rely on parse_datetime to handle arbitrary time zone |
| 1102 |
abbreviations; use numeric abbreviations like "-0500" instead. */ |
| 1103 |
static table const time_zone_table[] = |
| 1104 |
{ |
| 1105 |
{ "WET", tZONE, HOUR ( 0) }, /* Western European */ |
| 1106 |
{ "WEST", tDAYZONE, HOUR ( 0) }, /* Western European Summer */ |
| 1107 |
{ "BST", tDAYZONE, HOUR ( 0) }, /* British Summer */ |
| 1108 |
{ "ART", tZONE, -HOUR ( 3) }, /* Argentina */ |
| 1109 |
{ "BRT", tZONE, -HOUR ( 3) }, /* Brazil */ |
| 1110 |
{ "BRST", tDAYZONE, -HOUR ( 3) }, /* Brazil Summer */ |
| 1111 |
{ "NST", tZONE, -(HOUR ( 3) + 30 * 60) }, /* Newfoundland Standard */ |
| 1112 |
{ "NDT", tDAYZONE,-(HOUR ( 3) + 30 * 60) }, /* Newfoundland Daylight */ |
| 1113 |
{ "AST", tZONE, -HOUR ( 4) }, /* Atlantic Standard */ |
| 1114 |
{ "ADT", tDAYZONE, -HOUR ( 4) }, /* Atlantic Daylight */ |
| 1115 |
{ "CLT", tZONE, -HOUR ( 4) }, /* Chile */ |
| 1116 |
{ "CLST", tDAYZONE, -HOUR ( 4) }, /* Chile Summer */ |
| 1117 |
{ "EST", tZONE, -HOUR ( 5) }, /* Eastern Standard */ |
| 1118 |
{ "EDT", tDAYZONE, -HOUR ( 5) }, /* Eastern Daylight */ |
| 1119 |
{ "CST", tZONE, -HOUR ( 6) }, /* Central Standard */ |
| 1120 |
{ "CDT", tDAYZONE, -HOUR ( 6) }, /* Central Daylight */ |
| 1121 |
{ "MST", tZONE, -HOUR ( 7) }, /* Mountain Standard */ |
| 1122 |
{ "MDT", tDAYZONE, -HOUR ( 7) }, /* Mountain Daylight */ |
| 1123 |
{ "PST", tZONE, -HOUR ( 8) }, /* Pacific Standard */ |
| 1124 |
{ "PDT", tDAYZONE, -HOUR ( 8) }, /* Pacific Daylight */ |
| 1125 |
{ "AKST", tZONE, -HOUR ( 9) }, /* Alaska Standard */ |
| 1126 |
{ "AKDT", tDAYZONE, -HOUR ( 9) }, /* Alaska Daylight */ |
| 1127 |
{ "HST", tZONE, -HOUR (10) }, /* Hawaii Standard */ |
| 1128 |
{ "HAST", tZONE, -HOUR (10) }, /* Hawaii-Aleutian Standard */ |
| 1129 |
{ "HADT", tDAYZONE, -HOUR (10) }, /* Hawaii-Aleutian Daylight */ |
| 1130 |
{ "SST", tZONE, -HOUR (12) }, /* Samoa Standard */ |
| 1131 |
{ "WAT", tZONE, HOUR ( 1) }, /* West Africa */ |
| 1132 |
{ "CET", tZONE, HOUR ( 1) }, /* Central European */ |
| 1133 |
{ "CEST", tDAYZONE, HOUR ( 1) }, /* Central European Summer */ |
| 1134 |
{ "MET", tZONE, HOUR ( 1) }, /* Middle European */ |
| 1135 |
{ "MEZ", tZONE, HOUR ( 1) }, /* Middle European */ |
| 1136 |
{ "MEST", tDAYZONE, HOUR ( 1) }, /* Middle European Summer */ |
| 1137 |
{ "MESZ", tDAYZONE, HOUR ( 1) }, /* Middle European Summer */ |
| 1138 |
{ "EET", tZONE, HOUR ( 2) }, /* Eastern European */ |
| 1139 |
{ "EEST", tDAYZONE, HOUR ( 2) }, /* Eastern European Summer */ |
| 1140 |
{ "CAT", tZONE, HOUR ( 2) }, /* Central Africa */ |
| 1141 |
{ "SAST", tZONE, HOUR ( 2) }, /* South Africa Standard */ |
| 1142 |
{ "EAT", tZONE, HOUR ( 3) }, /* East Africa */ |
| 1143 |
{ "MSK", tZONE, HOUR ( 3) }, /* Moscow */ |
| 1144 |
{ "MSD", tDAYZONE, HOUR ( 3) }, /* Moscow Daylight */ |
| 1145 |
{ "IST", tZONE, (HOUR ( 5) + 30 * 60) }, /* India Standard */ |
| 1146 |
{ "SGT", tZONE, HOUR ( 8) }, /* Singapore */ |
| 1147 |
{ "KST", tZONE, HOUR ( 9) }, /* Korea Standard */ |
| 1148 |
{ "JST", tZONE, HOUR ( 9) }, /* Japan Standard */ |
| 1149 |
{ "GST", tZONE, HOUR (10) }, /* Guam Standard */ |
| 1150 |
{ "NZST", tZONE, HOUR (12) }, /* New Zealand Standard */ |
| 1151 |
{ "NZDT", tDAYZONE, HOUR (12) }, /* New Zealand Daylight */ |
| 1152 |
{ NULL, 0, 0 } |
| 1153 |
}; |
| 1154 |
|
| 1155 |
/* Military time zone table. |
| 1156 |
|
| 1157 |
RFC 822 got these backwards, but RFC 5322 makes the incorrect |
| 1158 |
treatment optional, so do them the right way here. |
| 1159 |
|
| 1160 |
'J' is special, as it is local time. |
| 1161 |
'T' is also special, as it is the separator in ISO |
| 1162 |
8601 date and time of day representation. */ |
| 1163 |
static table const military_table[] = |
| 1164 |
{ |
| 1165 |
{ "A", tZONE, HOUR ( 1) }, |
| 1166 |
{ "B", tZONE, HOUR ( 2) }, |
| 1167 |
{ "C", tZONE, HOUR ( 3) }, |
| 1168 |
{ "D", tZONE, HOUR ( 4) }, |
| 1169 |
{ "E", tZONE, HOUR ( 5) }, |
| 1170 |
{ "F", tZONE, HOUR ( 6) }, |
| 1171 |
{ "G", tZONE, HOUR ( 7) }, |
| 1172 |
{ "H", tZONE, HOUR ( 8) }, |
| 1173 |
{ "I", tZONE, HOUR ( 9) }, |
| 1174 |
{ "J", 'J', 0 }, |
| 1175 |
{ "K", tZONE, HOUR (10) }, |
| 1176 |
{ "L", tZONE, HOUR (11) }, |
| 1177 |
{ "M", tZONE, HOUR (12) }, |
| 1178 |
{ "N", tZONE, -HOUR ( 1) }, |
| 1179 |
{ "O", tZONE, -HOUR ( 2) }, |
| 1180 |
{ "P", tZONE, -HOUR ( 3) }, |
| 1181 |
{ "Q", tZONE, -HOUR ( 4) }, |
| 1182 |
{ "R", tZONE, -HOUR ( 5) }, |
| 1183 |
{ "S", tZONE, -HOUR ( 6) }, |
| 1184 |
{ "T", 'T', 0 }, |
| 1185 |
{ "U", tZONE, -HOUR ( 8) }, |
| 1186 |
{ "V", tZONE, -HOUR ( 9) }, |
| 1187 |
{ "W", tZONE, -HOUR (10) }, |
| 1188 |
{ "X", tZONE, -HOUR (11) }, |
| 1189 |
{ "Y", tZONE, -HOUR (12) }, |
| 1190 |
{ "Z", tZONE, HOUR ( 0) }, |
| 1191 |
{ NULL, 0, 0 } |
| 1192 |
}; |
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
/* Convert a time zone expressed as HH:MM into an integer count of |
| 1197 |
seconds. If MM is negative, then S is of the form HHMM and needs |
| 1198 |
to be picked apart; otherwise, S is of the form HH. As specified in |
| 1199 |
https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_03, allow |
| 1200 |
only valid TZ range, and consider first two digits as hours, if no |
| 1201 |
minutes specified. Return true if successful. */ |
| 1202 |
|
| 1203 |
static bool |
| 1204 |
time_zone_hhmm (parser_control *pc, textint s, intmax_t mm) |
| 1205 |
{ |
| 1206 |
intmax_t n_minutes; |
| 1207 |
bool overflow = false; |
| 1208 |
|
| 1209 |
/* If the length of S is 1 or 2 and no minutes are specified, |
| 1210 |
interpret it as a number of hours. */ |
| 1211 |
if (s.digits <= 2 && mm < 0) |
| 1212 |
s.value *= 100; |
| 1213 |
|
| 1214 |
if (mm < 0) |
| 1215 |
n_minutes = (s.value / 100) * 60 + s.value % 100; |
| 1216 |
else |
| 1217 |
{ |
| 1218 |
overflow |= ckd_mul (&n_minutes, s.value, 60); |
| 1219 |
overflow |= (s.negative |
| 1220 |
? ckd_sub (&n_minutes, n_minutes, mm) |
| 1221 |
: ckd_add (&n_minutes, n_minutes, mm)); |
| 1222 |
} |
| 1223 |
|
| 1224 |
if (overflow || ! (-24 * 60 <= n_minutes && n_minutes <= 24 * 60)) |
| 1225 |
return false; |
| 1226 |
pc->time_zone = n_minutes * 60; |
| 1227 |
return true; |
| 1228 |
} |
| 1229 |
|
| 1230 |
static int |
| 1231 |
to_hour (intmax_t hours, int meridian) |
| 1232 |
{ |
| 1233 |
switch (meridian) |
| 1234 |
{ |
| 1235 |
default: /* Pacify GCC. */ |
| 1236 |
case MER24: |
| 1237 |
return 0 <= hours && hours < 24 ? hours : -1; |
| 1238 |
case MERam: |
| 1239 |
return 0 < hours && hours < 12 ? hours : hours == 12 ? 0 : -1; |
| 1240 |
case MERpm: |
| 1241 |
return 0 < hours && hours < 12 ? hours + 12 : hours == 12 ? 12 : -1; |
| 1242 |
} |
| 1243 |
} |
| 1244 |
|
| 1245 |
enum { TM_YEAR_BASE = 1900 }; |
| 1246 |
enum { TM_YEAR_BUFSIZE = INT_BUFSIZE_BOUND (int) + 1 }; |
| 1247 |
|
| 1248 |
/* Convert TM_YEAR, a year minus 1900, to a string that is numerically |
| 1249 |
correct even if subtracting 1900 would overflow. */ |
| 1250 |
|
| 1251 |
static char const * |
| 1252 |
tm_year_str (int tm_year, char buf[TM_YEAR_BUFSIZE]) |
| 1253 |
{ |
| 1254 |
static_assert (TM_YEAR_BASE % 100 == 0); |
| 1255 |
sprintf (buf, &"-%02d%02d"[-TM_YEAR_BASE <= tm_year], |
| 1256 |
abs (tm_year / 100 + TM_YEAR_BASE / 100), |
| 1257 |
abs (tm_year % 100)); |
| 1258 |
return buf; |
| 1259 |
} |
| 1260 |
|
| 1261 |
/* Convert a text year number to a year minus 1900, working correctly |
| 1262 |
even if the input is in the range INT_MAX .. INT_MAX + 1900 - 1. */ |
| 1263 |
|
| 1264 |
static bool |
| 1265 |
to_tm_year (textint textyear, bool debug, int *tm_year) |
| 1266 |
{ |
| 1267 |
intmax_t year = textyear.value; |
| 1268 |
|
| 1269 |
/* XPG4 suggests that years 00-68 map to 2000-2068, and |
| 1270 |
years 69-99 map to 1969-1999. */ |
| 1271 |
if (0 <= year && textyear.digits == 2) |
| 1272 |
{ |
| 1273 |
year += year < 69 ? 2000 : 1900; |
| 1274 |
if (debug) |
| 1275 |
dbg_printf (_("warning: adjusting year value %"PRIdMAX |
| 1276 |
" to %"PRIdMAX"\n"), |
| 1277 |
textyear.value, year); |
| 1278 |
} |
| 1279 |
|
| 1280 |
if (year < 0 |
| 1281 |
? ckd_sub (tm_year, -TM_YEAR_BASE, year) |
| 1282 |
: ckd_sub (tm_year, year, TM_YEAR_BASE)) |
| 1283 |
{ |
| 1284 |
if (debug) |
| 1285 |
dbg_printf (_("error: out-of-range year %"PRIdMAX"\n"), year); |
| 1286 |
return false; |
| 1287 |
} |
| 1288 |
|
| 1289 |
return true; |
| 1290 |
} |
| 1291 |
|
| 1292 |
static table const * _GL_ATTRIBUTE_PURE |
| 1293 |
lookup_zone (parser_control const *pc, char const *name) |
| 1294 |
{ |
| 1295 |
table const *tp; |
| 1296 |
|
| 1297 |
for (tp = universal_time_zone_table; tp->name; tp++) |
| 1298 |
if (strcmp (name, tp->name) == 0) |
| 1299 |
return tp; |
| 1300 |
|
| 1301 |
/* Try local zone abbreviations before those in time_zone_table, as |
| 1302 |
the local ones are more likely to be right. */ |
| 1303 |
for (tp = pc->local_time_zone_table; tp->name; tp++) |
| 1304 |
if (strcmp (name, tp->name) == 0) |
| 1305 |
return tp; |
| 1306 |
|
| 1307 |
for (tp = time_zone_table; tp->name; tp++) |
| 1308 |
if (strcmp (name, tp->name) == 0) |
| 1309 |
return tp; |
| 1310 |
|
| 1311 |
return NULL; |
| 1312 |
} |
| 1313 |
|
| 1314 |
#if ! HAVE_TM_GMTOFF |
| 1315 |
/* Yield the difference between *A and *B, |
| 1316 |
measured in seconds, ignoring leap seconds. |
| 1317 |
The body of this function is taken directly from the GNU C Library; |
| 1318 |
see strftime.c. */ |
| 1319 |
static int |
| 1320 |
tm_diff (const struct tm *a, const struct tm *b) |
| 1321 |
{ |
| 1322 |
/* Compute intervening leap days correctly even if year is negative. |
| 1323 |
Take care to avoid int overflow in leap day calculations, |
| 1324 |
but it's OK to assume that A and B are close to each other. */ |
| 1325 |
int a4 = SHR (a->tm_year, 2) + SHR (TM_YEAR_BASE, 2) - ! (a->tm_year & 3); |
| 1326 |
int b4 = SHR (b->tm_year, 2) + SHR (TM_YEAR_BASE, 2) - ! (b->tm_year & 3); |
| 1327 |
int a100 = a4 / 25 - (a4 % 25 < 0); |
| 1328 |
int b100 = b4 / 25 - (b4 % 25 < 0); |
| 1329 |
int a400 = SHR (a100, 2); |
| 1330 |
int b400 = SHR (b100, 2); |
| 1331 |
int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400); |
| 1332 |
int years = a->tm_year - b->tm_year; |
| 1333 |
int days = (365 * years + intervening_leap_days |
| 1334 |
+ (a->tm_yday - b->tm_yday)); |
| 1335 |
return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour)) |
| 1336 |
+ (a->tm_min - b->tm_min)) |
| 1337 |
+ (a->tm_sec - b->tm_sec)); |
| 1338 |
} |
| 1339 |
#endif /* ! HAVE_TM_GMTOFF */ |
| 1340 |
|
| 1341 |
static table const * |
| 1342 |
lookup_word (parser_control const *pc, char *word) |
| 1343 |
{ |
| 1344 |
char *p; |
| 1345 |
char *q; |
| 1346 |
idx_t wordlen; |
| 1347 |
table const *tp; |
| 1348 |
bool period_found; |
| 1349 |
bool abbrev; |
| 1350 |
|
| 1351 |
/* Make it uppercase. */ |
| 1352 |
for (p = word; *p; p++) |
| 1353 |
*p = c_toupper (to_uchar (*p)); |
| 1354 |
|
| 1355 |
for (tp = meridian_table; tp->name; tp++) |
| 1356 |
if (strcmp (word, tp->name) == 0) |
| 1357 |
return tp; |
| 1358 |
|
| 1359 |
/* See if we have an abbreviation for a month. */ |
| 1360 |
wordlen = strlen (word); |
| 1361 |
abbrev = wordlen == 3 || (wordlen == 4 && word[3] == '.'); |
| 1362 |
|
| 1363 |
for (tp = month_and_day_table; tp->name; tp++) |
| 1364 |
if ((abbrev ? strncmp (word, tp->name, 3) : strcmp (word, tp->name)) == 0) |
| 1365 |
return tp; |
| 1366 |
|
| 1367 |
if ((tp = lookup_zone (pc, word))) |
| 1368 |
return tp; |
| 1369 |
|
| 1370 |
if (strcmp (word, dst_table[0].name) == 0) |
| 1371 |
return dst_table; |
| 1372 |
|
| 1373 |
for (tp = time_units_table; tp->name; tp++) |
| 1374 |
if (strcmp (word, tp->name) == 0) |
| 1375 |
return tp; |
| 1376 |
|
| 1377 |
/* Strip off any plural and try the units table again. */ |
| 1378 |
if (word[wordlen - 1] == 'S') |
| 1379 |
{ |
| 1380 |
word[wordlen - 1] = '\0'; |
| 1381 |
for (tp = time_units_table; tp->name; tp++) |
| 1382 |
if (strcmp (word, tp->name) == 0) |
| 1383 |
return tp; |
| 1384 |
word[wordlen - 1] = 'S'; /* For "this" in relative_time_table. */ |
| 1385 |
} |
| 1386 |
|
| 1387 |
for (tp = relative_time_table; tp->name; tp++) |
| 1388 |
if (strcmp (word, tp->name) == 0) |
| 1389 |
return tp; |
| 1390 |
|
| 1391 |
/* Military time zones. */ |
| 1392 |
if (wordlen == 1) |
| 1393 |
for (tp = military_table; tp->name; tp++) |
| 1394 |
if (word[0] == tp->name[0]) |
| 1395 |
return tp; |
| 1396 |
|
| 1397 |
/* Drop out any periods and try the time zone table again. */ |
| 1398 |
for (period_found = false, p = q = word; (*p = *q); q++) |
| 1399 |
if (*q == '.') |
| 1400 |
period_found = true; |
| 1401 |
else |
| 1402 |
p++; |
| 1403 |
if (period_found && (tp = lookup_zone (pc, word))) |
| 1404 |
return tp; |
| 1405 |
|
| 1406 |
return NULL; |
| 1407 |
} |
| 1408 |
|
| 1409 |
static int |
| 1410 |
yylex (union YYSTYPE *lvalp, parser_control *pc) |
| 1411 |
{ |
| 1412 |
unsigned char c; |
| 1413 |
|
| 1414 |
for (;;) |
| 1415 |
{ |
| 1416 |
while (c = *pc->input, c_isspace (c)) |
| 1417 |
pc->input++; |
| 1418 |
|
| 1419 |
if (c_isdigit (c) || c == '-' || c == '+') |
| 1420 |
{ |
| 1421 |
char const *p = pc->input; |
| 1422 |
int sign; |
| 1423 |
if (c == '-' || c == '+') |
| 1424 |
{ |
| 1425 |
sign = c == '-' ? -1 : 1; |
| 1426 |
while (c = *(pc->input = ++p), c_isspace (c)) |
| 1427 |
continue; |
| 1428 |
if (! c_isdigit (c)) |
| 1429 |
/* skip the '-' sign */ |
| 1430 |
continue; |
| 1431 |
} |
| 1432 |
else |
| 1433 |
sign = 0; |
| 1434 |
|
| 1435 |
time_t value = 0; |
| 1436 |
do |
| 1437 |
{ |
| 1438 |
if (ckd_mul (&value, value, 10)) |
| 1439 |
return '?'; |
| 1440 |
if (ckd_add (&value, value, sign < 0 ? '0' - c : c - '0')) |
| 1441 |
return '?'; |
| 1442 |
c = *++p; |
| 1443 |
} |
| 1444 |
while (c_isdigit (c)); |
| 1445 |
|
| 1446 |
if ((c == '.' || c == ',') && c_isdigit (p[1])) |
| 1447 |
{ |
| 1448 |
time_t s = value; |
| 1449 |
int digits; |
| 1450 |
|
| 1451 |
/* Accumulate fraction, to ns precision. */ |
| 1452 |
p++; |
| 1453 |
int ns = *p++ - '0'; |
| 1454 |
for (digits = 2; digits <= LOG10_BILLION; digits++) |
| 1455 |
{ |
| 1456 |
ns *= 10; |
| 1457 |
if (c_isdigit (*p)) |
| 1458 |
ns += *p++ - '0'; |
| 1459 |
} |
| 1460 |
|
| 1461 |
/* Skip excess digits, truncating toward -Infinity. */ |
| 1462 |
if (sign < 0) |
| 1463 |
for (; c_isdigit (*p); p++) |
| 1464 |
if (*p != '0') |
| 1465 |
{ |
| 1466 |
ns++; |
| 1467 |
break; |
| 1468 |
} |
| 1469 |
while (c_isdigit (*p)) |
| 1470 |
p++; |
| 1471 |
|
| 1472 |
/* Adjust to the timespec convention, which is that |
| 1473 |
tv_nsec is always a positive offset even if tv_sec is |
| 1474 |
negative. */ |
| 1475 |
if (sign < 0 && ns) |
| 1476 |
{ |
| 1477 |
if (ckd_sub (&s, s, 1)) |
| 1478 |
return '?'; |
| 1479 |
ns = BILLION - ns; |
| 1480 |
} |
| 1481 |
|
| 1482 |
lvalp->timespec = (struct timespec) { .tv_sec = s, |
| 1483 |
.tv_nsec = ns }; |
| 1484 |
pc->input = p; |
| 1485 |
return sign ? tSDECIMAL_NUMBER : tUDECIMAL_NUMBER; |
| 1486 |
} |
| 1487 |
else |
| 1488 |
{ |
| 1489 |
lvalp->textintval.negative = sign < 0; |
| 1490 |
lvalp->textintval.value = value; |
| 1491 |
lvalp->textintval.digits = p - pc->input; |
| 1492 |
pc->input = p; |
| 1493 |
return sign ? tSNUMBER : tUNUMBER; |
| 1494 |
} |
| 1495 |
} |
| 1496 |
|
| 1497 |
if (c_isalpha (c)) |
| 1498 |
{ |
| 1499 |
char buff[20]; |
| 1500 |
char *p = buff; |
| 1501 |
table const *tp; |
| 1502 |
|
| 1503 |
do |
| 1504 |
{ |
| 1505 |
if (p < buff + sizeof buff - 1) |
| 1506 |
*p++ = c; |
| 1507 |
c = *++pc->input; |
| 1508 |
} |
| 1509 |
while (c_isalpha (c) || c == '.'); |
| 1510 |
|
| 1511 |
*p = '\0'; |
| 1512 |
tp = lookup_word (pc, buff); |
| 1513 |
if (! tp) |
| 1514 |
{ |
| 1515 |
if (debugging (pc)) |
| 1516 |
dbg_printf (_("error: unknown word '%s'\n"), buff); |
| 1517 |
return '?'; |
| 1518 |
} |
| 1519 |
lvalp->intval = tp->value; |
| 1520 |
return tp->type; |
| 1521 |
} |
| 1522 |
|
| 1523 |
if (c != '(') |
| 1524 |
return to_uchar (*pc->input++); |
| 1525 |
|
| 1526 |
idx_t count = 0; |
| 1527 |
do |
| 1528 |
{ |
| 1529 |
c = *pc->input++; |
| 1530 |
if (c == '\0') |
| 1531 |
return c; |
| 1532 |
if (c == '(') |
| 1533 |
count++; |
| 1534 |
else if (c == ')') |
| 1535 |
count--; |
| 1536 |
} |
| 1537 |
while (count != 0); |
| 1538 |
} |
| 1539 |
} |
| 1540 |
|
| 1541 |
/* Do nothing if the parser reports an error. */ |
| 1542 |
static int |
| 1543 |
yyerror (_GL_UNUSED parser_control const *pc, |
| 1544 |
_GL_UNUSED char const *s) |
| 1545 |
{ |
| 1546 |
return 0; |
| 1547 |
} |
| 1548 |
|
| 1549 |
/* If *TM0 is the old and *TM1 is the new value of a struct tm after |
| 1550 |
passing it to mktime_z, return true if it's OK. It's not OK if |
| 1551 |
mktime failed or if *TM0 has out-of-range mainline members. |
| 1552 |
The caller should set TM1->tm_wday to -1 before calling mktime, |
| 1553 |
as a negative tm_wday is how mktime failure is inferred. */ |
| 1554 |
|
| 1555 |
static bool |
| 1556 |
mktime_ok (struct tm const *tm0, struct tm const *tm1) |
| 1557 |
{ |
| 1558 |
if (tm1->tm_wday < 0) |
| 1559 |
return false; |
| 1560 |
|
| 1561 |
return ! ((tm0->tm_sec ^ tm1->tm_sec) |
| 1562 |
| (tm0->tm_min ^ tm1->tm_min) |
| 1563 |
| (tm0->tm_hour ^ tm1->tm_hour) |
| 1564 |
| (tm0->tm_mday ^ tm1->tm_mday) |
| 1565 |
| (tm0->tm_mon ^ tm1->tm_mon) |
| 1566 |
| (tm0->tm_year ^ tm1->tm_year)); |
| 1567 |
} |
| 1568 |
|
| 1569 |
/* Debugging: format a 'struct tm' into a buffer, taking the parser's |
| 1570 |
timezone information into account (if pc != NULL). */ |
| 1571 |
static char const * |
| 1572 |
debug_strfdatetime (struct tm const *tm, parser_control const *pc, |
| 1573 |
char *buf, int n) |
| 1574 |
{ |
| 1575 |
/* TODO: |
| 1576 |
1. find an optimal way to print date string in a clear and unambiguous |
| 1577 |
format. Currently, always add '(Y-M-D)' prefix. |
| 1578 |
Consider '2016y01m10d' or 'year(2016) month(01) day(10)'. |
| 1579 |
|
| 1580 |
If the user needs debug printing, it means he/she already having |
| 1581 |
issues with the parsing - better to avoid formats that could |
| 1582 |
be mis-interpreted (e.g., just YYYY-MM-DD). |
| 1583 |
|
| 1584 |
2. Can strftime be used instead? |
| 1585 |
depends if it is portable and can print invalid dates on all systems. |
| 1586 |
|
| 1587 |
3. Print timezone information ? |
| 1588 |
|
| 1589 |
4. Print DST information ? |
| 1590 |
|
| 1591 |
5. Print nanosecond information ? |
| 1592 |
|
| 1593 |
NOTE: |
| 1594 |
Printed date/time values might not be valid, e.g., '2016-02-31' |
| 1595 |
or '2016-19-2016' . These are the values as parsed from the user |
| 1596 |
string, before validation. |
| 1597 |
*/ |
| 1598 |
int m = nstrftime (buf, n, "(Y-M-D) %Y-%m-%d %H:%M:%S", tm, 0, 0); |
| 1599 |
|
| 1600 |
/* If parser_control information was provided (for timezone), |
| 1601 |
and there's enough space in the buffer, add timezone info. */ |
| 1602 |
if (pc && m < n && pc->zones_seen) |
| 1603 |
{ |
| 1604 |
int tz = pc->time_zone; |
| 1605 |
|
| 1606 |
/* Account for DST if tLOCAL_ZONE was seen. */ |
| 1607 |
if (pc->local_zones_seen && !pc->zones_seen && 0 < pc->local_isdst) |
| 1608 |
tz += 60 * 60; |
| 1609 |
|
| 1610 |
char time_zone_buf[TIME_ZONE_BUFSIZE]; |
| 1611 |
snprintf (&buf[m], n - m, " TZ=%s", time_zone_str (tz, time_zone_buf)); |
| 1612 |
} |
| 1613 |
return buf; |
| 1614 |
} |
| 1615 |
|
| 1616 |
static char const * |
| 1617 |
debug_strfdate (struct tm const *tm, char *buf, int n) |
| 1618 |
{ |
| 1619 |
char tm_year_buf[TM_YEAR_BUFSIZE]; |
| 1620 |
snprintf (buf, n, "(Y-M-D) %s-%02d-%02d", |
| 1621 |
tm_year_str (tm->tm_year, tm_year_buf), |
| 1622 |
tm->tm_mon + 1, tm->tm_mday); |
| 1623 |
return buf; |
| 1624 |
} |
| 1625 |
|
| 1626 |
static char const * |
| 1627 |
debug_strftime (struct tm const *tm, char *buf, int n) |
| 1628 |
{ |
| 1629 |
snprintf (buf, n, "%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 1630 |
return buf; |
| 1631 |
} |
| 1632 |
|
| 1633 |
/* If mktime_ok failed, display the failed time values, |
| 1634 |
and provide possible hints. Example output: |
| 1635 |
|
| 1636 |
date: error: invalid date/time value: |
| 1637 |
date: user provided time: '(Y-M-D) 2006-04-02 02:45:00' |
| 1638 |
date: normalized time: '(Y-M-D) 2006-04-02 03:45:00' |
| 1639 |
date: __ |
| 1640 |
date: possible reasons: |
| 1641 |
date: nonexistent due to daylight-saving time; |
| 1642 |
date: numeric values overflow; |
| 1643 |
date: missing timezone; |
| 1644 |
*/ |
| 1645 |
static void |
| 1646 |
debug_mktime_not_ok (struct tm const *tm0, struct tm const *tm1, |
| 1647 |
parser_control const *pc, bool time_zone_seen) |
| 1648 |
{ |
| 1649 |
/* TODO: handle t == -1 (as in 'mktime_ok'). */ |
| 1650 |
char tmp[DBGBUFSIZE]; |
| 1651 |
int i; |
| 1652 |
const bool eq_sec = (tm0->tm_sec == tm1->tm_sec); |
| 1653 |
const bool eq_min = (tm0->tm_min == tm1->tm_min); |
| 1654 |
const bool eq_hour = (tm0->tm_hour == tm1->tm_hour); |
| 1655 |
const bool eq_mday = (tm0->tm_mday == tm1->tm_mday); |
| 1656 |
const bool eq_month = (tm0->tm_mon == tm1->tm_mon); |
| 1657 |
const bool eq_year = (tm0->tm_year == tm1->tm_year); |
| 1658 |
|
| 1659 |
const bool dst_shift = eq_sec && eq_min && !eq_hour |
| 1660 |
&& eq_mday && eq_month && eq_year; |
| 1661 |
|
| 1662 |
if (!debugging (pc)) |
| 1663 |
return; |
| 1664 |
|
| 1665 |
dbg_printf (_("error: invalid date/time value:\n")); |
| 1666 |
dbg_printf (_(" user provided time: '%s'\n"), |
| 1667 |
debug_strfdatetime (tm0, pc, tmp, sizeof tmp)); |
| 1668 |
dbg_printf (_(" normalized time: '%s'\n"), |
| 1669 |
debug_strfdatetime (tm1, pc, tmp, sizeof tmp)); |
| 1670 |
/* The format must be aligned with debug_strfdatetime and the two |
| 1671 |
DEBUG statements above. This string is not translated. */ |
| 1672 |
i = snprintf (tmp, sizeof tmp, |
| 1673 |
" %4s %2s %2s %2s %2s %2s", |
| 1674 |
eq_year ? "" : "----", |
| 1675 |
eq_month ? "" : "--", |
| 1676 |
eq_mday ? "" : "--", |
| 1677 |
eq_hour ? "" : "--", |
| 1678 |
eq_min ? "" : "--", |
| 1679 |
eq_sec ? "" : "--"); |
| 1680 |
/* Trim trailing whitespace. */ |
| 1681 |
if (0 <= i) |
| 1682 |
{ |
| 1683 |
if (sizeof tmp - 1 < i) |
| 1684 |
i = sizeof tmp - 1; |
| 1685 |
while (0 < i && tmp[i - 1] == ' ') |
| 1686 |
--i; |
| 1687 |
tmp[i] = '\0'; |
| 1688 |
} |
| 1689 |
dbg_printf ("%s\n", tmp); |
| 1690 |
|
| 1691 |
dbg_printf (_(" possible reasons:\n")); |
| 1692 |
if (dst_shift) |
| 1693 |
dbg_printf (_(" nonexistent due to daylight-saving time;\n")); |
| 1694 |
if (!eq_mday && !eq_month) |
| 1695 |
dbg_printf (_(" invalid day/month combination;\n")); |
| 1696 |
dbg_printf (_(" numeric values overflow;\n")); |
| 1697 |
dbg_printf (" %s\n", (time_zone_seen ? _("incorrect timezone") |
| 1698 |
: _("missing timezone"))); |
| 1699 |
} |
| 1700 |
|
| 1701 |
/* Parse a date/time string, storing the resulting time value into *RESULT. |
| 1702 |
The string itself is pointed to by P. Return true if successful. |
| 1703 |
P can be an incomplete or relative time specification; if so, use |
| 1704 |
*NOW as the basis for the returned time. Default to timezone |
| 1705 |
TZDEFAULT, which corresponds to tzalloc (TZSTRING). */ |
| 1706 |
static bool |
| 1707 |
parse_datetime_body (struct timespec *result, char const *p, |
| 1708 |
struct timespec const *now, unsigned int flags, |
| 1709 |
timezone_t tzdefault, char const *tzstring) |
| 1710 |
{ |
| 1711 |
struct tm tm; |
| 1712 |
struct tm tm0; |
| 1713 |
char time_zone_buf[TIME_ZONE_BUFSIZE]; |
| 1714 |
char dbg_tm[DBGBUFSIZE]; |
| 1715 |
bool ok = false; |
| 1716 |
char const *input_sentinel = p + strlen (p); |
| 1717 |
char *tz1alloc = NULL; |
| 1718 |
|
| 1719 |
/* A reasonable upper bound for the size of ordinary TZ strings. |
| 1720 |
Use heap allocation if TZ's length exceeds this. */ |
| 1721 |
enum { TZBUFSIZE = 100 }; |
| 1722 |
char tz1buf[TZBUFSIZE]; |
| 1723 |
|
| 1724 |
struct timespec gettime_buffer; |
| 1725 |
if (! now) |
| 1726 |
{ |
| 1727 |
gettime (&gettime_buffer); |
| 1728 |
now = &gettime_buffer; |
| 1729 |
} |
| 1730 |
|
| 1731 |
time_t Start = now->tv_sec; |
| 1732 |
int Start_ns = now->tv_nsec; |
| 1733 |
|
| 1734 |
unsigned char c; |
| 1735 |
while (c = *p, c_isspace (c)) |
| 1736 |
p++; |
| 1737 |
|
| 1738 |
timezone_t tz = tzdefault; |
| 1739 |
|
| 1740 |
/* Store a local copy prior to first "goto". Without this, a prior use |
| 1741 |
below of RELATIVE_TIME_0 on the RHS might translate to an assignment- |
| 1742 |
to-temporary, which would trigger a -Wjump-misses-init warning. */ |
| 1743 |
const relative_time rel_time_0 = RELATIVE_TIME_0; |
| 1744 |
|
| 1745 |
if (strncmp (p, "TZ=\"", 4) == 0) |
| 1746 |
{ |
| 1747 |
char const *tzbase = p + 4; |
| 1748 |
idx_t tzsize = 1; |
| 1749 |
char const *s; |
| 1750 |
|
| 1751 |
for (s = tzbase; *s; s++, tzsize++) |
| 1752 |
if (*s == '\\') |
| 1753 |
{ |
| 1754 |
s++; |
| 1755 |
if (! (*s == '\\' || *s == '"')) |
| 1756 |
break; |
| 1757 |
} |
| 1758 |
else if (*s == '"') |
| 1759 |
{ |
| 1760 |
timezone_t tz1; |
| 1761 |
char *tz1string = tz1buf; |
| 1762 |
char *z; |
| 1763 |
if (TZBUFSIZE < tzsize) |
| 1764 |
{ |
| 1765 |
tz1alloc = malloc (tzsize); |
| 1766 |
if (!tz1alloc) |
| 1767 |
goto fail; |
| 1768 |
tz1string = tz1alloc; |
| 1769 |
} |
| 1770 |
z = tz1string; |
| 1771 |
for (s = tzbase; *s != '"'; s++) |
| 1772 |
*z++ = *(s += *s == '\\'); |
| 1773 |
*z = '\0'; |
| 1774 |
tz1 = tzalloc (tz1string); |
| 1775 |
if (!tz1) |
| 1776 |
goto fail; |
| 1777 |
tz = tz1; |
| 1778 |
tzstring = tz1string; |
| 1779 |
|
| 1780 |
p = s + 1; |
| 1781 |
while (c = *p, c_isspace (c)) |
| 1782 |
p++; |
| 1783 |
|
| 1784 |
break; |
| 1785 |
} |
| 1786 |
} |
| 1787 |
|
| 1788 |
struct tm tmp; |
| 1789 |
if (! localtime_rz (tz, &now->tv_sec, &tmp)) |
| 1790 |
goto fail; |
| 1791 |
|
| 1792 |
/* As documented, be careful to treat the empty string just like |
| 1793 |
a date string of "0". Without this, an empty string would be |
| 1794 |
declared invalid when parsed during a DST transition. */ |
| 1795 |
if (*p == '\0') |
| 1796 |
p = "0"; |
| 1797 |
|
| 1798 |
parser_control pc; |
| 1799 |
pc.input = p; |
| 1800 |
#ifdef GNULIB_PARSE_DATETIME2 |
| 1801 |
pc.parse_datetime_debug = (flags & PARSE_DATETIME_DEBUG) != 0; |
| 1802 |
#endif |
| 1803 |
if (ckd_add (&pc.year.value, tmp.tm_year, TM_YEAR_BASE)) |
| 1804 |
{ |
| 1805 |
if (debugging (&pc)) |
| 1806 |
dbg_printf (_("error: initial year out of range\n")); |
| 1807 |
goto fail; |
| 1808 |
} |
| 1809 |
pc.year.digits = 0; |
| 1810 |
pc.month = tmp.tm_mon + 1; |
| 1811 |
pc.day = tmp.tm_mday; |
| 1812 |
pc.hour = tmp.tm_hour; |
| 1813 |
pc.minutes = tmp.tm_min; |
| 1814 |
pc.seconds = (struct timespec) { .tv_sec = tmp.tm_sec, .tv_nsec = Start_ns }; |
| 1815 |
tm.tm_isdst = tmp.tm_isdst; |
| 1816 |
|
| 1817 |
pc.meridian = MER24; |
| 1818 |
pc.rel = rel_time_0; |
| 1819 |
pc.timespec_seen = false; |
| 1820 |
pc.rels_seen = false; |
| 1821 |
pc.dates_seen = 0; |
| 1822 |
pc.days_seen = 0; |
| 1823 |
pc.times_seen = 0; |
| 1824 |
pc.J_zones_seen = 0; |
| 1825 |
pc.local_zones_seen = 0; |
| 1826 |
pc.dsts_seen = 0; |
| 1827 |
pc.zones_seen = 0; |
| 1828 |
pc.year_seen = false; |
| 1829 |
pc.debug_dates_seen = false; |
| 1830 |
pc.debug_days_seen = false; |
| 1831 |
pc.debug_times_seen = false; |
| 1832 |
pc.debug_local_zones_seen = false; |
| 1833 |
pc.debug_zones_seen = false; |
| 1834 |
pc.debug_year_seen = false; |
| 1835 |
pc.debug_ordinal_day_seen = false; |
| 1836 |
|
| 1837 |
#if HAVE_STRUCT_TM_TM_ZONE |
| 1838 |
pc.local_time_zone_table[0].name = tmp.tm_zone; |
| 1839 |
pc.local_time_zone_table[0].type = tLOCAL_ZONE; |
| 1840 |
pc.local_time_zone_table[0].value = tmp.tm_isdst; |
| 1841 |
pc.local_time_zone_table[1].name = NULL; |
| 1842 |
|
| 1843 |
/* Probe the names used in the next three calendar quarters, looking |
| 1844 |
for a tm_isdst different from the one we already have. */ |
| 1845 |
{ |
| 1846 |
int quarter; |
| 1847 |
for (quarter = 1; quarter <= 3; quarter++) |
| 1848 |
{ |
| 1849 |
time_t probe; |
| 1850 |
if (ckd_add (&probe, Start, quarter * (90 * 24 * 60 * 60))) |
| 1851 |
break; |
| 1852 |
struct tm probe_tm; |
| 1853 |
if (localtime_rz (tz, &probe, &probe_tm) && probe_tm.tm_zone |
| 1854 |
&& probe_tm.tm_isdst != pc.local_time_zone_table[0].value) |
| 1855 |
{ |
| 1856 |
{ |
| 1857 |
pc.local_time_zone_table[1].name = probe_tm.tm_zone; |
| 1858 |
pc.local_time_zone_table[1].type = tLOCAL_ZONE; |
| 1859 |
pc.local_time_zone_table[1].value = probe_tm.tm_isdst; |
| 1860 |
pc.local_time_zone_table[2].name = NULL; |
| 1861 |
} |
| 1862 |
break; |
| 1863 |
} |
| 1864 |
} |
| 1865 |
} |
| 1866 |
#else |
| 1867 |
#if HAVE_TZNAME |
| 1868 |
{ |
| 1869 |
# if !HAVE_DECL_TZNAME |
| 1870 |
extern char *tzname[]; |
| 1871 |
# endif |
| 1872 |
int i; |
| 1873 |
for (i = 0; i < 2; i++) |
| 1874 |
{ |
| 1875 |
pc.local_time_zone_table[i].name = tzname[i]; |
| 1876 |
pc.local_time_zone_table[i].type = tLOCAL_ZONE; |
| 1877 |
pc.local_time_zone_table[i].value = i; |
| 1878 |
} |
| 1879 |
pc.local_time_zone_table[i].name = NULL; |
| 1880 |
} |
| 1881 |
#else |
| 1882 |
pc.local_time_zone_table[0].name = NULL; |
| 1883 |
#endif |
| 1884 |
#endif |
| 1885 |
|
| 1886 |
if (pc.local_time_zone_table[0].name && pc.local_time_zone_table[1].name |
| 1887 |
&& ! strcmp (pc.local_time_zone_table[0].name, |
| 1888 |
pc.local_time_zone_table[1].name)) |
| 1889 |
{ |
| 1890 |
/* This locale uses the same abbreviation for standard and |
| 1891 |
daylight times. So if we see that abbreviation, we don't |
| 1892 |
know whether it's daylight time. */ |
| 1893 |
pc.local_time_zone_table[0].value = -1; |
| 1894 |
pc.local_time_zone_table[1].name = NULL; |
| 1895 |
} |
| 1896 |
|
| 1897 |
if (yyparse (&pc) != 0) |
| 1898 |
{ |
| 1899 |
if (debugging (&pc)) |
| 1900 |
dbg_printf ((input_sentinel <= pc.input |
| 1901 |
? _("error: parsing failed\n") |
| 1902 |
: _("error: parsing failed, stopped at '%s'\n")), |
| 1903 |
pc.input); |
| 1904 |
goto fail; |
| 1905 |
} |
| 1906 |
|
| 1907 |
|
| 1908 |
/* Determine effective timezone source. */ |
| 1909 |
|
| 1910 |
if (debugging (&pc)) |
| 1911 |
{ |
| 1912 |
dbg_printf (_("input timezone: ")); |
| 1913 |
|
| 1914 |
if (pc.timespec_seen) |
| 1915 |
fprintf (stderr, _("'@timespec' - always UTC")); |
| 1916 |
else if (pc.zones_seen) |
| 1917 |
fprintf (stderr, _("parsed date/time string")); |
| 1918 |
else if (tzstring) |
| 1919 |
{ |
| 1920 |
if (tz != tzdefault) |
| 1921 |
fprintf (stderr, _("TZ=\"%s\" in date string"), tzstring); |
| 1922 |
else if (STREQ (tzstring, "UTC0")) |
| 1923 |
{ |
| 1924 |
/* Special case: 'date -u' sets TZ="UTC0". */ |
| 1925 |
fprintf (stderr, _("TZ=\"UTC0\" environment value or -u")); |
| 1926 |
} |
| 1927 |
else |
| 1928 |
fprintf (stderr, _("TZ=\"%s\" environment value"), tzstring); |
| 1929 |
} |
| 1930 |
else |
| 1931 |
fprintf (stderr, _("system default")); |
| 1932 |
|
| 1933 |
/* Account for DST changes if tLOCAL_ZONE was seen. |
| 1934 |
local timezone only changes DST and is relative to the |
| 1935 |
default timezone.*/ |
| 1936 |
if (pc.local_zones_seen && !pc.zones_seen && 0 < pc.local_isdst) |
| 1937 |
fprintf (stderr, ", dst"); |
| 1938 |
|
| 1939 |
if (pc.zones_seen) |
| 1940 |
fprintf (stderr, " (%s)", time_zone_str (pc.time_zone, time_zone_buf)); |
| 1941 |
|
| 1942 |
fputc ('\n', stderr); |
| 1943 |
} |
| 1944 |
|
| 1945 |
if (pc.timespec_seen) |
| 1946 |
*result = pc.seconds; |
| 1947 |
else |
| 1948 |
{ |
| 1949 |
if (1 < (pc.times_seen | pc.dates_seen | pc.days_seen | pc.dsts_seen |
| 1950 |
| (pc.J_zones_seen + pc.local_zones_seen + pc.zones_seen))) |
| 1951 |
{ |
| 1952 |
if (debugging (&pc)) |
| 1953 |
{ |
| 1954 |
if (pc.times_seen > 1) |
| 1955 |
dbg_printf ("error: seen multiple time parts\n"); |
| 1956 |
if (pc.dates_seen > 1) |
| 1957 |
dbg_printf ("error: seen multiple date parts\n"); |
| 1958 |
if (pc.days_seen > 1) |
| 1959 |
dbg_printf ("error: seen multiple days parts\n"); |
| 1960 |
if (pc.dsts_seen > 1) |
| 1961 |
dbg_printf ("error: seen multiple daylight-saving parts\n"); |
| 1962 |
if ((pc.J_zones_seen + pc.local_zones_seen + pc.zones_seen) > 1) |
| 1963 |
dbg_printf ("error: seen multiple time-zone parts\n"); |
| 1964 |
} |
| 1965 |
goto fail; |
| 1966 |
} |
| 1967 |
|
| 1968 |
if (! to_tm_year (pc.year, debugging (&pc), &tm.tm_year) |
| 1969 |
|| ckd_add (&tm.tm_mon, pc.month, -1) |
| 1970 |
|| ckd_add (&tm.tm_mday, pc.day, 0)) |
| 1971 |
{ |
| 1972 |
if (debugging (&pc)) |
| 1973 |
dbg_printf (_("error: year, month, or day overflow\n")); |
| 1974 |
goto fail; |
| 1975 |
} |
| 1976 |
if (pc.times_seen || (pc.rels_seen && ! pc.dates_seen && ! pc.days_seen)) |
| 1977 |
{ |
| 1978 |
tm.tm_hour = to_hour (pc.hour, pc.meridian); |
| 1979 |
if (tm.tm_hour < 0) |
| 1980 |
{ |
| 1981 |
char const *mrd = (pc.meridian == MERam ? "am" |
| 1982 |
: pc.meridian == MERpm ?"pm" : ""); |
| 1983 |
if (debugging (&pc)) |
| 1984 |
dbg_printf (_("error: invalid hour %"PRIdMAX"%s\n"), |
| 1985 |
pc.hour, mrd); |
| 1986 |
goto fail; |
| 1987 |
} |
| 1988 |
tm.tm_min = pc.minutes; |
| 1989 |
tm.tm_sec = pc.seconds.tv_sec; |
| 1990 |
if (debugging (&pc)) |
| 1991 |
dbg_printf ((pc.times_seen |
| 1992 |
? _("using specified time as starting value: '%s'\n") |
| 1993 |
: _("using current time as starting value: '%s'\n")), |
| 1994 |
debug_strftime (&tm, dbg_tm, sizeof dbg_tm)); |
| 1995 |
} |
| 1996 |
else |
| 1997 |
{ |
| 1998 |
tm.tm_hour = tm.tm_min = tm.tm_sec = 0; |
| 1999 |
pc.seconds.tv_nsec = 0; |
| 2000 |
if (debugging (&pc)) |
| 2001 |
dbg_printf ("warning: using midnight as starting time: 00:00:00\n"); |
| 2002 |
} |
| 2003 |
|
| 2004 |
/* Let mktime deduce tm_isdst if we have an absolute timestamp. */ |
| 2005 |
if (pc.dates_seen | pc.days_seen | pc.times_seen) |
| 2006 |
tm.tm_isdst = -1; |
| 2007 |
|
| 2008 |
/* But if the input explicitly specifies local time with or without |
| 2009 |
DST, give mktime that information. */ |
| 2010 |
if (pc.local_zones_seen) |
| 2011 |
tm.tm_isdst = pc.local_isdst; |
| 2012 |
|
| 2013 |
tm0.tm_sec = tm.tm_sec; |
| 2014 |
tm0.tm_min = tm.tm_min; |
| 2015 |
tm0.tm_hour = tm.tm_hour; |
| 2016 |
tm0.tm_mday = tm.tm_mday; |
| 2017 |
tm0.tm_mon = tm.tm_mon; |
| 2018 |
tm0.tm_year = tm.tm_year; |
| 2019 |
tm0.tm_isdst = tm.tm_isdst; |
| 2020 |
tm.tm_wday = -1; |
| 2021 |
|
| 2022 |
Start = mktime_z (tz, &tm); |
| 2023 |
|
| 2024 |
if (! mktime_ok (&tm0, &tm)) |
| 2025 |
{ |
| 2026 |
bool repaired = false; |
| 2027 |
bool time_zone_seen = pc.zones_seen != 0; |
| 2028 |
if (time_zone_seen) |
| 2029 |
{ |
| 2030 |
/* Guard against falsely reporting errors near the time_t |
| 2031 |
boundaries when parsing times in other time zones. For |
| 2032 |
example, suppose the input string "1969-12-31 23:00:00 -0100", |
| 2033 |
the current time zone is 8 hours ahead of UTC, and the min |
| 2034 |
time_t value is 1970-01-01 00:00:00 UTC. Then the min |
| 2035 |
localtime value is 1970-01-01 08:00:00, and mktime will |
| 2036 |
therefore fail on 1969-12-31 23:00:00. To work around the |
| 2037 |
problem, set the time zone to 1 hour behind UTC temporarily |
| 2038 |
by setting TZ="XXX1:00" and try mktime again. */ |
| 2039 |
|
| 2040 |
char tz2buf[sizeof "XXX" - 1 + TIME_ZONE_BUFSIZE]; |
| 2041 |
tz2buf[0] = tz2buf[1] = tz2buf[2] = 'X'; |
| 2042 |
time_zone_str (pc.time_zone, &tz2buf[3]); |
| 2043 |
timezone_t tz2 = tzalloc (tz2buf); |
| 2044 |
if (!tz2) |
| 2045 |
{ |
| 2046 |
if (debugging (&pc)) |
| 2047 |
dbg_printf (_("error: tzalloc (\"%s\") failed\n"), tz2buf); |
| 2048 |
goto fail; |
| 2049 |
} |
| 2050 |
tm.tm_sec = tm0.tm_sec; |
| 2051 |
tm.tm_min = tm0.tm_min; |
| 2052 |
tm.tm_hour = tm0.tm_hour; |
| 2053 |
tm.tm_mday = tm0.tm_mday; |
| 2054 |
tm.tm_mon = tm0.tm_mon; |
| 2055 |
tm.tm_year = tm0.tm_year; |
| 2056 |
tm.tm_isdst = tm0.tm_isdst; |
| 2057 |
tm.tm_wday = -1; |
| 2058 |
Start = mktime_z (tz2, &tm); |
| 2059 |
repaired = mktime_ok (&tm0, &tm); |
| 2060 |
tzfree (tz2); |
| 2061 |
} |
| 2062 |
|
| 2063 |
if (! repaired) |
| 2064 |
{ |
| 2065 |
debug_mktime_not_ok (&tm0, &tm, &pc, time_zone_seen); |
| 2066 |
goto fail; |
| 2067 |
} |
| 2068 |
} |
| 2069 |
|
| 2070 |
char dbg_ord[DBGBUFSIZE]; |
| 2071 |
|
| 2072 |
if (pc.days_seen && ! pc.dates_seen) |
| 2073 |
{ |
| 2074 |
intmax_t dayincr; |
| 2075 |
tm.tm_yday = -1; |
| 2076 |
intmax_t day_ordinal = (pc.day_ordinal |
| 2077 |
- (0 < pc.day_ordinal |
| 2078 |
&& tm.tm_wday != pc.day_number)); |
| 2079 |
if (! (ckd_mul (&dayincr, day_ordinal, 7) |
| 2080 |
|| ckd_add (&dayincr, (pc.day_number - tm.tm_wday + 7) % 7, |
| 2081 |
dayincr) |
| 2082 |
|| ckd_add (&tm.tm_mday, dayincr, tm.tm_mday))) |
| 2083 |
{ |
| 2084 |
tm.tm_isdst = -1; |
| 2085 |
Start = mktime_z (tz, &tm); |
| 2086 |
} |
| 2087 |
|
| 2088 |
if (tm.tm_yday < 0) |
| 2089 |
{ |
| 2090 |
if (debugging (&pc)) |
| 2091 |
dbg_printf (_("error: day '%s' " |
| 2092 |
"(day ordinal=%"PRIdMAX" number=%d) " |
| 2093 |
"resulted in an invalid date: '%s'\n"), |
| 2094 |
str_days (&pc, dbg_ord, sizeof dbg_ord), |
| 2095 |
pc.day_ordinal, pc.day_number, |
| 2096 |
debug_strfdatetime (&tm, &pc, dbg_tm, |
| 2097 |
sizeof dbg_tm)); |
| 2098 |
goto fail; |
| 2099 |
} |
| 2100 |
|
| 2101 |
if (debugging (&pc)) |
| 2102 |
dbg_printf (_("new start date: '%s' is '%s'\n"), |
| 2103 |
str_days (&pc, dbg_ord, sizeof dbg_ord), |
| 2104 |
debug_strfdatetime (&tm, &pc, dbg_tm, sizeof dbg_tm)); |
| 2105 |
|
| 2106 |
} |
| 2107 |
|
| 2108 |
if (debugging (&pc)) |
| 2109 |
{ |
| 2110 |
if (!pc.dates_seen && !pc.days_seen) |
| 2111 |
dbg_printf (_("using current date as starting value: '%s'\n"), |
| 2112 |
debug_strfdate (&tm, dbg_tm, sizeof dbg_tm)); |
| 2113 |
|
| 2114 |
if (pc.days_seen && pc.dates_seen) |
| 2115 |
dbg_printf (_("warning: day (%s) ignored when explicit dates " |
| 2116 |
"are given\n"), |
| 2117 |
str_days (&pc, dbg_ord, sizeof dbg_ord)); |
| 2118 |
|
| 2119 |
dbg_printf (_("starting date/time: '%s'\n"), |
| 2120 |
debug_strfdatetime (&tm, &pc, dbg_tm, sizeof dbg_tm)); |
| 2121 |
} |
| 2122 |
|
| 2123 |
/* Add relative date. */ |
| 2124 |
if (pc.rel.year | pc.rel.month | pc.rel.day) |
| 2125 |
{ |
| 2126 |
if (debugging (&pc)) |
| 2127 |
{ |
| 2128 |
if ((pc.rel.year != 0 || pc.rel.month != 0) && tm.tm_mday != 15) |
| 2129 |
dbg_printf (_("warning: when adding relative months/years, " |
| 2130 |
"it is recommended to specify the 15th of the " |
| 2131 |
"months\n")); |
| 2132 |
|
| 2133 |
if (pc.rel.day != 0 && tm.tm_hour != 12) |
| 2134 |
dbg_printf (_("warning: when adding relative days, " |
| 2135 |
"it is recommended to specify noon\n")); |
| 2136 |
} |
| 2137 |
|
| 2138 |
int year, month, day; |
| 2139 |
if (ckd_add (&year, tm.tm_year, pc.rel.year) |
| 2140 |
|| ckd_add (&month, tm.tm_mon, pc.rel.month) |
| 2141 |
|| ckd_add (&day, tm.tm_mday, pc.rel.day)) |
| 2142 |
{ |
| 2143 |
if (debugging (&pc)) |
| 2144 |
dbg_printf (_("error: %s:%d\n"), __FILE__, __LINE__); |
| 2145 |
goto fail; |
| 2146 |
} |
| 2147 |
tm.tm_year = year; |
| 2148 |
tm.tm_mon = month; |
| 2149 |
tm.tm_mday = day; |
| 2150 |
tm.tm_hour = tm0.tm_hour; |
| 2151 |
tm.tm_min = tm0.tm_min; |
| 2152 |
tm.tm_sec = tm0.tm_sec; |
| 2153 |
tm.tm_isdst = tm0.tm_isdst; |
| 2154 |
tm.tm_wday = -1; |
| 2155 |
Start = mktime_z (tz, &tm); |
| 2156 |
if (tm.tm_wday < 0) |
| 2157 |
{ |
| 2158 |
if (debugging (&pc)) |
| 2159 |
dbg_printf (_("error: adding relative date resulted " |
| 2160 |
"in an invalid date: '%s'\n"), |
| 2161 |
debug_strfdatetime (&tm, &pc, dbg_tm, |
| 2162 |
sizeof dbg_tm)); |
| 2163 |
goto fail; |
| 2164 |
} |
| 2165 |
|
| 2166 |
if (debugging (&pc)) |
| 2167 |
{ |
| 2168 |
dbg_printf (_("after date adjustment " |
| 2169 |
"(%+"PRIdMAX" years, %+"PRIdMAX" months, " |
| 2170 |
"%+"PRIdMAX" days),\n"), |
| 2171 |
pc.rel.year, pc.rel.month, pc.rel.day); |
| 2172 |
dbg_printf (_(" new date/time = '%s'\n"), |
| 2173 |
debug_strfdatetime (&tm, &pc, dbg_tm, |
| 2174 |
sizeof dbg_tm)); |
| 2175 |
|
| 2176 |
/* Warn about crossing DST due to time adjustment. |
| 2177 |
Example: https://bugs.gnu.org/8357 |
| 2178 |
env TZ=Europe/Helsinki \ |
| 2179 |
date --debug \ |
| 2180 |
-d 'Mon Mar 28 00:36:07 2011 EEST 1 day ago' |
| 2181 |
|
| 2182 |
This case is different than DST changes due to time adjustment, |
| 2183 |
i.e., "1 day ago" vs "24 hours ago" are calculated in different |
| 2184 |
places. |
| 2185 |
|
| 2186 |
'tm0.tm_isdst' contains the DST of the input date, |
| 2187 |
'tm.tm_isdst' is the normalized result after calling |
| 2188 |
mktime (&tm). |
| 2189 |
*/ |
| 2190 |
if (tm0.tm_isdst != -1 && tm.tm_isdst != tm0.tm_isdst) |
| 2191 |
dbg_printf (_("warning: daylight saving time changed after " |
| 2192 |
"date adjustment\n")); |
| 2193 |
|
| 2194 |
/* Warn if the user did not ask to adjust days but mday changed, |
| 2195 |
or |
| 2196 |
user did not ask to adjust months/days but the month changed. |
| 2197 |
|
| 2198 |
Example for first case: |
| 2199 |
2016-05-31 + 1 month => 2016-06-31 => 2016-07-01. |
| 2200 |
User asked to adjust month, but the day changed from 31 to 01. |
| 2201 |
|
| 2202 |
Example for second case: |
| 2203 |
2016-02-29 + 1 year => 2017-02-29 => 2017-03-01. |
| 2204 |
User asked to adjust year, but the month changed from 02 to 03. |
| 2205 |
*/ |
| 2206 |
if (pc.rel.day == 0 |
| 2207 |
&& (tm.tm_mday != day |
| 2208 |
|| (pc.rel.month == 0 && tm.tm_mon != month))) |
| 2209 |
{ |
| 2210 |
dbg_printf (_("warning: month/year adjustment resulted in " |
| 2211 |
"shifted dates:\n")); |
| 2212 |
char tm_year_buf[TM_YEAR_BUFSIZE]; |
| 2213 |
dbg_printf (_(" adjusted Y M D: %s %02d %02d\n"), |
| 2214 |
tm_year_str (year, tm_year_buf), month + 1, day); |
| 2215 |
dbg_printf (_(" normalized Y M D: %s %02d %02d\n"), |
| 2216 |
tm_year_str (tm.tm_year, tm_year_buf), |
| 2217 |
tm.tm_mon + 1, tm.tm_mday); |
| 2218 |
} |
| 2219 |
} |
| 2220 |
|
| 2221 |
} |
| 2222 |
|
| 2223 |
/* The only "output" of this if-block is an updated Start value, |
| 2224 |
so this block must follow others that clobber Start. */ |
| 2225 |
if (pc.zones_seen) |
| 2226 |
{ |
| 2227 |
bool overflow = false; |
| 2228 |
#ifdef HAVE_TM_GMTOFF |
| 2229 |
long int utcoff = tm.tm_gmtoff; |
| 2230 |
#else |
| 2231 |
time_t t = Start; |
| 2232 |
struct tm gmt; |
| 2233 |
int utcoff = (gmtime_r (&t, &gmt) |
| 2234 |
? tm_diff (&tm, &gmt) |
| 2235 |
: (overflow = true, 0)); |
| 2236 |
#endif |
| 2237 |
intmax_t delta; |
| 2238 |
overflow |= ckd_sub (&delta, pc.time_zone, utcoff); |
| 2239 |
time_t t1; |
| 2240 |
overflow |= ckd_sub (&t1, Start, delta); |
| 2241 |
if (overflow) |
| 2242 |
{ |
| 2243 |
if (debugging (&pc)) |
| 2244 |
dbg_printf (_("error: timezone %d caused time_t overflow\n"), |
| 2245 |
pc.time_zone); |
| 2246 |
goto fail; |
| 2247 |
} |
| 2248 |
Start = t1; |
| 2249 |
} |
| 2250 |
|
| 2251 |
if (debugging (&pc)) |
| 2252 |
{ |
| 2253 |
intmax_t Starti = Start; |
| 2254 |
dbg_printf (_("'%s' = %"PRIdMAX" epoch-seconds\n"), |
| 2255 |
debug_strfdatetime (&tm, &pc, dbg_tm, sizeof dbg_tm), |
| 2256 |
Starti); |
| 2257 |
} |
| 2258 |
|
| 2259 |
|
| 2260 |
/* Add relative hours, minutes, and seconds. On hosts that support |
| 2261 |
leap seconds, ignore the possibility of leap seconds; e.g., |
| 2262 |
"+ 10 minutes" adds 600 seconds, even if one of them is a |
| 2263 |
leap second. Typically this is not what the user wants, but it's |
| 2264 |
too hard to do it the other way, because the time zone indicator |
| 2265 |
must be applied before relative times, and if mktime is applied |
| 2266 |
again the time zone will be lost. */ |
| 2267 |
{ |
| 2268 |
intmax_t orig_ns = pc.seconds.tv_nsec; |
| 2269 |
intmax_t sum_ns = orig_ns + pc.rel.ns; |
| 2270 |
int normalized_ns = (sum_ns % BILLION + BILLION) % BILLION; |
| 2271 |
int d4 = (sum_ns - normalized_ns) / BILLION; |
| 2272 |
intmax_t d1, t1, d2, t2, t3; |
| 2273 |
time_t t4; |
| 2274 |
if (ckd_mul (&d1, pc.rel.hour, 60 * 60) |
| 2275 |
|| ckd_add (&t1, Start, d1) |
| 2276 |
|| ckd_mul (&d2, pc.rel.minutes, 60) |
| 2277 |
|| ckd_add (&t2, t1, d2) |
| 2278 |
|| ckd_add (&t3, t2, pc.rel.seconds) |
| 2279 |
|| ckd_add (&t4, t3, d4)) |
| 2280 |
{ |
| 2281 |
if (debugging (&pc)) |
| 2282 |
dbg_printf (_("error: adding relative time caused an " |
| 2283 |
"overflow\n")); |
| 2284 |
goto fail; |
| 2285 |
} |
| 2286 |
|
| 2287 |
result->tv_sec = t4; |
| 2288 |
result->tv_nsec = normalized_ns; |
| 2289 |
|
| 2290 |
if (debugging (&pc) |
| 2291 |
&& (pc.rel.hour | pc.rel.minutes | pc.rel.seconds | pc.rel.ns)) |
| 2292 |
{ |
| 2293 |
dbg_printf (_("after time adjustment (%+"PRIdMAX" hours, " |
| 2294 |
"%+"PRIdMAX" minutes, " |
| 2295 |
"%+"PRIdMAX" seconds, %+d ns),\n"), |
| 2296 |
pc.rel.hour, pc.rel.minutes, pc.rel.seconds, |
| 2297 |
pc.rel.ns); |
| 2298 |
intmax_t t4i = t4; |
| 2299 |
dbg_printf (_(" new time = %"PRIdMAX" epoch-seconds\n"), t4i); |
| 2300 |
|
| 2301 |
/* Warn about crossing DST due to time adjustment. |
| 2302 |
Example: https://bugs.gnu.org/8357 |
| 2303 |
env TZ=Europe/Helsinki \ |
| 2304 |
date --debug \ |
| 2305 |
-d 'Mon Mar 28 00:36:07 2011 EEST 24 hours ago' |
| 2306 |
|
| 2307 |
This case is different than DST changes due to days adjustment, |
| 2308 |
i.e., "1 day ago" vs "24 hours ago" are calculated in different |
| 2309 |
places. |
| 2310 |
|
| 2311 |
'tm.tm_isdst' contains the date after date adjustment. */ |
| 2312 |
struct tm lmt; |
| 2313 |
if (tm.tm_isdst != -1 && localtime_rz (tz, &result->tv_sec, &lmt) |
| 2314 |
&& tm.tm_isdst != lmt.tm_isdst) |
| 2315 |
dbg_printf (_("warning: daylight saving time changed after " |
| 2316 |
"time adjustment\n")); |
| 2317 |
} |
| 2318 |
} |
| 2319 |
} |
| 2320 |
|
| 2321 |
if (debugging (&pc)) |
| 2322 |
{ |
| 2323 |
/* Special case: using 'date -u' simply set TZ=UTC0 */ |
| 2324 |
if (! tzstring) |
| 2325 |
dbg_printf (_("timezone: system default\n")); |
| 2326 |
else if (STREQ (tzstring, "UTC0")) |
| 2327 |
dbg_printf (_("timezone: Universal Time\n")); |
| 2328 |
else |
| 2329 |
dbg_printf (_("timezone: TZ=\"%s\" environment value\n"), tzstring); |
| 2330 |
|
| 2331 |
intmax_t sec = result->tv_sec; |
| 2332 |
int nsec = result->tv_nsec; |
| 2333 |
dbg_printf (_("final: %"PRIdMAX".%09d (epoch-seconds)\n"), |
| 2334 |
sec, nsec); |
| 2335 |
|
| 2336 |
struct tm gmt, lmt; |
| 2337 |
bool got_utc = !!gmtime_r (&result->tv_sec, &gmt); |
| 2338 |
if (got_utc) |
| 2339 |
dbg_printf (_("final: %s (UTC)\n"), |
| 2340 |
debug_strfdatetime (&gmt, NULL, |
| 2341 |
dbg_tm, sizeof dbg_tm)); |
| 2342 |
if (localtime_rz (tz, &result->tv_sec, &lmt)) |
| 2343 |
{ |
| 2344 |
#ifdef HAVE_TM_GMTOFF |
| 2345 |
bool got_utcoff = true; |
| 2346 |
long int utcoff = lmt.tm_gmtoff; |
| 2347 |
#else |
| 2348 |
bool got_utcoff = got_utc; |
| 2349 |
int utcoff; |
| 2350 |
if (got_utcoff) |
| 2351 |
utcoff = tm_diff (&lmt, &gmt); |
| 2352 |
#endif |
| 2353 |
if (got_utcoff) |
| 2354 |
dbg_printf (_("final: %s (UTC%s)\n"), |
| 2355 |
debug_strfdatetime (&lmt, NULL, dbg_tm, sizeof dbg_tm), |
| 2356 |
time_zone_str (utcoff, time_zone_buf)); |
| 2357 |
else |
| 2358 |
dbg_printf (_("final: %s (unknown time zone offset)\n"), |
| 2359 |
debug_strfdatetime (&lmt, NULL, dbg_tm, sizeof dbg_tm)); |
| 2360 |
} |
| 2361 |
} |
| 2362 |
|
| 2363 |
ok = true; |
| 2364 |
|
| 2365 |
fail: |
| 2366 |
if (tz != tzdefault) |
| 2367 |
tzfree (tz); |
| 2368 |
free (tz1alloc); |
| 2369 |
return ok; |
| 2370 |
} |
| 2371 |
|
| 2372 |
#ifdef GNULIB_PARSE_DATETIME2 |
| 2373 |
/* Parse a date/time string, storing the resulting time value into *RESULT. |
| 2374 |
The string itself is pointed to by P. Return true if successful. |
| 2375 |
P can be an incomplete or relative time specification; if so, use |
| 2376 |
*NOW as the basis for the returned time. Default to timezone |
| 2377 |
TZDEFAULT, which corresponds to tzalloc (TZSTRING). */ |
| 2378 |
bool |
| 2379 |
parse_datetime2 (struct timespec *result, char const *p, |
| 2380 |
struct timespec const *now, unsigned int flags, |
| 2381 |
timezone_t tzdefault, char const *tzstring) |
| 2382 |
{ |
| 2383 |
return parse_datetime_body (result, p, now, flags, tzdefault, tzstring); |
| 2384 |
} |
| 2385 |
#endif |
| 2386 |
|
| 2387 |
|
| 2388 |
/* The plain interface: run with debug=false and the default timezone. */ |
| 2389 |
bool |
| 2390 |
parse_datetime (struct timespec *result, char const *p, |
| 2391 |
struct timespec const *now) |
| 2392 |
{ |
| 2393 |
char const *tzstring = getenv ("TZ"); |
| 2394 |
timezone_t tz = tzalloc (tzstring); |
| 2395 |
if (!tz) |
| 2396 |
return false; |
| 2397 |
bool ok = parse_datetime_body (result, p, now, 0, tz, tzstring); |
| 2398 |
tzfree (tz); |
| 2399 |
return ok; |
| 2400 |
} |
| 2401 |
|
| 2402 |
#if TEST |
| 2403 |
|
| 2404 |
int |
| 2405 |
main (int ac, char **av) |
| 2406 |
{ |
| 2407 |
char buff[BUFSIZ]; |
| 2408 |
|
| 2409 |
printf ("Enter date, or blank line to exit.\n\t> "); |
| 2410 |
fflush (stdout); |
| 2411 |
|
| 2412 |
buff[BUFSIZ - 1] = '\0'; |
| 2413 |
while (fgets (buff, BUFSIZ - 1, stdin) && buff[0]) |
| 2414 |
{ |
| 2415 |
struct timespec d; |
| 2416 |
struct tm const *tm; |
| 2417 |
if (! parse_datetime (&d, buff, NULL)) |
| 2418 |
printf ("Bad format - couldn't convert.\n"); |
| 2419 |
else if (! (tm = localtime (&d.tv_sec))) |
| 2420 |
{ |
| 2421 |
intmax_t sec = d.tv_sec; |
| 2422 |
printf ("localtime (%"PRIdMAX") failed\n", sec); |
| 2423 |
} |
| 2424 |
else |
| 2425 |
{ |
| 2426 |
int ns = d.tv_nsec; |
| 2427 |
char tm_year_buf[TM_YEAR_BUFSIZE]; |
| 2428 |
printf ("%s-%02d-%02d %02d:%02d:%02d.%09d\n", |
| 2429 |
tm_year_str (tm->tm_year, tm_year_buf), |
| 2430 |
tm->tm_mon + 1, tm->tm_mday, |
| 2431 |
tm->tm_hour, tm->tm_min, tm->tm_sec, ns); |
| 2432 |
} |
| 2433 |
printf ("\t> "); |
| 2434 |
fflush (stdout); |
| 2435 |
} |
| 2436 |
return 0; |
| 2437 |
} |
| 2438 |
#endif /* TEST */ |