]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - lib/parse-date.y
rev: be careful with close()
[thirdparty/util-linux.git] / lib / parse-date.y
index b79b333e3f296375b9f63031ae58b239c59cc218..bb5abb5d4cf468959be8e567c21bbc91bb3de3c8 100644 (file)
@@ -206,10 +206,6 @@ typedef struct {
        size_t dsts_seen;
        size_t times_seen;
        size_t zones_seen;
-       size_t year_seen;
-
-       /* 1 if the user specified explicit ordinal day value, */
-       int ordinal_day_seen;
 
        /* Table of local time zone abbreviations, null terminated. */
        table local_time_zone_table[3];
@@ -218,7 +214,7 @@ typedef struct {
 union YYSTYPE;
 static int yylex (union YYSTYPE *, parser_control *);
 static int yyerror (parser_control const *, char const *);
-static long int time_zone_hhmm (parser_control *, textint, intmax_t);
+static int time_zone_hhmm (parser_control *, textint, textint);
 
 /**
  * Extract into *PC any date and time info from a string of digits
@@ -229,7 +225,6 @@ static void digits_to_date_time(parser_control *pc, textint text_int)
 {
        if (pc->dates_seen && ! pc->year.digits
            && ! pc->rels_seen && (pc->times_seen || 2 < text_int.digits)) {
-               pc->year_seen++;
                pc->year = text_int;
        } else {
                if (4 < text_int.digits) {
@@ -312,7 +307,7 @@ set_hhmmss(parser_control *pc, intmax_t hour, intmax_t minutes,
 %token <textintval> tSNUMBER tUNUMBER
 %token <timespec> tSDECIMAL_NUMBER tUDECIMAL_NUMBER
 
-%type <intval> o_colon_minutes
+%type <textintval> o_colon_minutes
 %type <timespec> seconds signed_seconds unsigned_seconds
 
 %type <rel> relunit relunit_snumber dayshift
@@ -407,7 +402,7 @@ o_zone_offset:
 zone_offset:
          tSNUMBER o_colon_minutes {
                pc->zones_seen++;
-               pc->time_zone = time_zone_hhmm (pc, $1, $2);
+               if (! time_zone_hhmm (pc, $1, $2)) YYABORT;
          }
 ;
 
@@ -460,7 +455,8 @@ zone:
                apply_relative_time (pc, $2, 1);
          }
        | tZONE tSNUMBER o_colon_minutes {
-               pc->time_zone = $1 + time_zone_hhmm (pc, $2, $3);
+               if (! time_zone_hhmm (pc, $2, $3)) YYABORT;
+               pc->time_zone += $1;
          }
        | tDAYZONE {
                pc->time_zone = $1 + 60;
@@ -482,12 +478,10 @@ day:
        | tORDINAL tDAY {
                pc->day_ordinal = $1;
                pc->day_number = $2;
-               pc->ordinal_day_seen = 1;
          }
        | tUNUMBER tDAY {
                pc->day_ordinal = $1.value;
                pc->day_number = $2;
-               pc->ordinal_day_seen = 1;
          }
 ;
 
@@ -669,9 +663,10 @@ hybrid:
 
 o_colon_minutes:
          /* empty */
-               { $$ = -1; }
-       | ':' tUNUMBER
-               { $$ = $2.value; }
+               { $$.value = $$.digits = 0; }
+       | ':' tUNUMBER {
+               $$ = $2;
+         }
 ;
 
 %%
@@ -862,39 +857,35 @@ static table const military_table[] = {
 };
 
 /**
- * Convert a time zone expressed as HH:MM into an integer count of
- * minutes.  If MM is negative, then S is of the form HHMM and needs
- * to be picked apart; otherwise, S is of the form HH.  As specified in
- * http://www.opengroup.org/susv3xbd/xbd_chap08.html#tag_08_03, allow
- * only valid TZ range, and consider first two digits as hours, if no
- * minutes specified.
+ * Convert a time offset expressed as HH:MM or HHMM into an integer count of
+ * minutes.  If hh is more than 2 digits then it is of the form HHMM and must be
+ * delimited; in that case 'mm' is required to be absent.  Otherwise, hh and mm
+ * are used ('mm' contains digits that were prefixed with a colon).
+ *
+ * POSIX TZ and ISO 8601 both define the maximum offset as 24:59. POSIX also
+ * allows seconds, but currently the parser rejects them. Both require minutes
+ * to be zero padded (2 digits). ISO requires hours to be zero padded, POSIX
+ * does not, either is accepted; which means an invalid ISO offset could pass.
  */
 
-static long int time_zone_hhmm(parser_control *pc, textint s, intmax_t mm)
+static int time_zone_hhmm(parser_control *pc, textint hh, textint mm)
 {
-       intmax_t n_minutes;
-
-       /**
-        * If the length of S is 1 or 2 and no minutes are specified,
-        * interpret it as a number of hours.
-        */
-       if (s.digits <= 2 && mm < 0)
-               s.value *= 100;
-
-       if (mm < 0)
-               n_minutes = (s.value / 100) * 60 + s.value % 100;
-       else
-               n_minutes = s.value * 60 + (s.negative ? -mm : mm);
+       int h, m;
+
+       if (hh.digits > 2 && hh.digits < 5 && mm.digits == 0) {
+               h = hh.value / 100;
+               m = hh.value % 100;
+       } else if (hh.digits < 3 && (mm.digits == 0 || mm.digits == 2)) {
+               h = hh.value;
+               m = hh.negative ? -mm.value : mm.value;
+       } else
+               return 0;
 
-       /**
-        * If the absolute number of minutes is larger than 24 hours,
-        * arrange to reject it by incrementing pc->zones_seen. Thus,
-        * we allow only values in the range UTC-24:00 to UTC+24:00.
-        */
-       if (24 * 60 < abs (n_minutes))
-               pc->zones_seen++;
+       if (abs(h) > 24 || abs(m) > 59)
+               return 0;
 
-       return n_minutes;
+       pc->time_zone =  h * 60 + m;
+       return 1;
 }
 
 static int to_hour(intmax_t hours, int meridian)
@@ -957,7 +948,7 @@ static table const * lookup_zone(parser_control const *pc, char const *name)
  * The body of this function is taken directly from the GNU C Library;
  * see src/strftime.c.
  */
-static long int tm_diff(struct tm const *a, struct tm const *b)
+static int tm_diff(struct tm const *a, struct tm const *b)
 {
        /**
         * Compute intervening leap days correctly even if year is negative.
@@ -970,8 +961,7 @@ static long int tm_diff(struct tm const *a, struct tm const *b)
        int a400 = SHR (a100, 2);
        int b400 = SHR (b100, 2);
        int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400);
-       int ayear = a->tm_year;
-       int years = ayear - b->tm_year;
+       int years = a->tm_year - b->tm_year;
        int days = (365 * years + intervening_leap_days
                         + (a->tm_yday - b->tm_yday));
        return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour))
@@ -990,10 +980,8 @@ static table const * lookup_word(parser_control const *pc, char *word)
        int abbrev;
 
        /* Make it uppercase. */
-       for (p = word; *p; p++) {
-               unsigned char ch = *p;
-               *p = c_toupper (ch);
-       }
+       for (p = word; *p; p++)
+               *p = c_toupper (to_uchar (*p));
 
        for (tp = meridian_table; tp->name; tp++)
                if (strcmp (word, tp->name) == 0)
@@ -1061,7 +1049,7 @@ static int yylex (union YYSTYPE *lvalp, parser_control *pc)
                if (c_isdigit (c) || c == '-' || c == '+') {
                        char const *p;
                        int sign;
-                       unsigned long int value;
+                       uintmax_t value;
                        if (c == '-' || c == '+') {
                                sign = c == '-' ? -1 : 1;
                                while (c = *++pc->input, c_isspace (c))
@@ -1073,21 +1061,21 @@ static int yylex (union YYSTYPE *lvalp, parser_control *pc)
                                sign = 0;
                        p = pc->input;
                        for (value = 0; ; value *= 10) {
-                               unsigned long int value1 = value + (c - '0');
+                               uintmax_t value1 = value + (c - '0');
                                if (value1 < value)
                                        return '?';
                                value = value1;
                                c = *++p;
                                if (! c_isdigit (c))
                                        break;
-                               if (ULONG_MAX / 10 < value)
+                               if (UINTMAX_MAX / 10 < value)
                                        return '?';
                        }
                        if ((c == '.' || c == ',') && c_isdigit (p[1])) {
                                time_t s;
                                int ns;
                                int digits;
-                               unsigned long int value1;
+                               uintmax_t value1;
 
                                /* Check for overflow when converting value to
                                 * time_t.
@@ -1371,8 +1359,6 @@ int parse_date(struct timespec *result, char const *p,
        pc.local_zones_seen = 0;
        pc.dsts_seen = 0;
        pc.zones_seen = 0;
-       pc.year_seen = 0;
-       pc.ordinal_day_seen = 0;
 
 #if HAVE_STRUCT_TM_TM_ZONE
        pc.local_time_zone_table[0].name = tmp->tm_zone;
@@ -1517,7 +1503,7 @@ int parse_date(struct timespec *result, char const *p,
 
                                if (!tz_was_altered)
                                        tz0 = get_tz (tz0buf);
-                               sprintf (tz1buf, "XXX%s%ld:%02d",
+                               sprintf (tz1buf, "XXX%s%jd:%02d",
                                         &"-"[time_zone < 0],
                                         abs_time_zone_hour,
                                         abs_time_zone_min);