Skip to content

Commit e490501

Browse files
rscharfegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent edc74bc commit e490501

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

apply.c

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -820,8 +820,7 @@ static int has_epoch_timestamp(const char *nameline)
820820
const char *timestamp = NULL, *cp, *colon;
821821
static regex_t *stamp;
822822
regmatch_t m[10];
823-
int zoneoffset;
824-
int hourminute;
823+
int zoneoffset, epoch_hour, hour, minute;
825824
int status;
826825

827826
for (cp = nameline; *cp != '\n'; cp++) {
@@ -830,6 +829,18 @@ static int has_epoch_timestamp(const char *nameline)
830829
}
831830
if (!timestamp)
832831
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+
833844
if (!stamp) {
834845
stamp = xmalloc(sizeof(*stamp));
835846
if (regcomp(stamp, stamp_regexp, REG_EXTENDED)) {
@@ -847,6 +858,9 @@ static int has_epoch_timestamp(const char *nameline)
847858
return 0;
848859
}
849860

861+
hour = strtol(timestamp + 11, NULL, 10);
862+
minute = strtol(timestamp + 14, NULL, 10);
863+
850864
zoneoffset = strtol(timestamp + m[3].rm_so + 1, (char **) &colon, 10);
851865
if (*colon == ':')
852866
zoneoffset = zoneoffset * 60 + strtol(colon + 1, NULL, 10);
@@ -855,20 +869,7 @@ static int has_epoch_timestamp(const char *nameline)
855869
if (timestamp[m[3].rm_so] == '-')
856870
zoneoffset = -zoneoffset;
857871

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;
872873
}
873874

874875
/*

0 commit comments

Comments
 (0)