apply: check date of potential epoch timestamps first

has_epoch_timestamp() looks for time stamps that amount to either 1969-12-31 24:00 or 1970-01-01 00:00 after applying the time zone offset. Move the check for these two dates up, set the expected hour based on which one is found, or exit early if none of them are present, thus avoiding to engage the regex machinery for newer dates. This also gets rid of two magic string length constants. Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Aug 25, 2017 at 21:04 UTC e4905019df568762d5f9ab66227eefcb32915b40
1 file changed +17 -16
apply.c
+17 -16
@@ -820,8 +820,7 @@ static int has_epoch_timestamp(const char *nameline)
820 const char *timestamp = NULL, *cp, *colon;
821 static regex_t *stamp;
822 regmatch_t m[10];
823 - int zoneoffset;
824 - int hourminute;
823 + int zoneoffset, epoch_hour, hour, minute;
824 int status;
825
826 for (cp = nameline; *cp != '\n'; cp++) {
@@ -830,6 +829,18 @@ static int has_epoch_timestamp(const char *nameline)
829 }
830 if (!timestamp)
831 return 0;
832 +
833 + /*
834 + * YYYY-MM-DD hh:mm:ss must be from either 1969-12-31
835 + * (west of GMT) or 1970-01-01 (east of GMT)
836 + */
837 + if (starts_with(timestamp, "1969-12-31"))
838 + epoch_hour = 24;
839 + else if (starts_with(timestamp, "1970-01-01"))
840 + epoch_hour = 0;
841 + else
842 + return 0;
843 +
844 if (!stamp) {
845 stamp = xmalloc(sizeof(*stamp));
846 if (regcomp(stamp, stamp_regexp, REG_EXTENDED)) {
@@ -847,6 +858,9 @@ static int has_epoch_timestamp(const char *nameline)
858 return 0;
859 }
860
861 + hour = strtol(timestamp + 11, NULL, 10);
862 + minute = strtol(timestamp + 14, NULL, 10);
863 +
864 zoneoffset = strtol(timestamp + m[3].rm_so + 1, (char **) &colon, 10);
865 if (*colon == ':')
866 zoneoffset = zoneoffset * 60 + strtol(colon + 1, NULL, 10);
@@ -855,20 +869,7 @@ static int has_epoch_timestamp(const char *nameline)
869 if (timestamp[m[3].rm_so] == '-')
870 zoneoffset = -zoneoffset;
871
858 - /*
859 - * YYYY-MM-DD hh:mm:ss must be from either 1969-12-31
860 - * (west of GMT) or 1970-01-01 (east of GMT)
861 - */
862 - if ((zoneoffset < 0 && memcmp(timestamp, "1969-12-31", 10)) ||
863 - (0 <= zoneoffset && memcmp(timestamp, "1970-01-01", 10)))
864 - return 0;
865 -
866 - hourminute = (strtol(timestamp + 11, NULL, 10) * 60 +
867 - strtol(timestamp + 14, NULL, 10) -
868 - zoneoffset);
869 -
870 - return ((zoneoffset < 0 && hourminute == 1440) ||
871 - (0 <= zoneoffset && !hourminute));
872 + return hour * 60 + minute - zoneoffset == epoch_hour * 60;
873 }
874
875 /*