{"thread":{"id":"46669","subject":"[PATCH 1/2] apply: check date of potential epoch timestamps first","startedAt":"2017-08-25T19:05:05Z","lastAt":"2017-08-25T19:06:38Z","messageCount":2,"participants":["René Scharfe"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"327213","messageId":"205339f5-bad8-62a3-8ccc-d3b0dd1d6736@web.de","threadId":"46669","inReplyTo":null,"subject":"[PATCH 1/2] apply: check date of potential epoch timestamps first","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2017-08-25T19:04:54Z","receivedAt":"2017-08-25T19:05:05Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"has_epoch_timestamp() looks for time stamps that amount to either\n1969-12-31 24:00 or 1970-01-01 00:00 after applying the time zone\noffset.  Move the check for these two dates up, set the expected hour\nbased on which one is found, or exit early if none of them are present,\nthus avoiding to engage the regex machinery for newer dates.\n\nThis also gets rid of two magic string length constants.\n\nSigned-off-by: Rene Scharfe <l.s.r@web.de>\n---\n apply.c | 33 +++++++++++++++++----------------\n 1 file changed, 17 insertions(+), 16 deletions(-)\n\ndiff --git a/apply.c b/apply.c\nindex 956f56a927..e14077eaee 100644\n--- a/apply.c\n+++ b/apply.c\n@@ -819,8 +819,7 @@ static int has_epoch_timestamp(const char *nameline)\n \tconst char *timestamp = NULL, *cp, *colon;\n \tstatic regex_t *stamp;\n \tregmatch_t m[10];\n-\tint zoneoffset;\n-\tint hourminute;\n+\tint zoneoffset, epoch_hour, hour, minute;\n \tint status;\n \n \tfor (cp = nameline; *cp != '\\n'; cp++) {\n@@ -829,6 +828,18 @@ static int has_epoch_timestamp(const char *nameline)\n \t}\n \tif (!timestamp)\n \t\treturn 0;\n+\n+\t/*\n+\t * YYYY-MM-DD hh:mm:ss must be from either 1969-12-31\n+\t * (west of GMT) or 1970-01-01 (east of GMT)\n+\t */\n+\tif (starts_with(timestamp, \"1969-12-31\"))\n+\t\tepoch_hour = 24;\n+\telse if (starts_with(timestamp, \"1970-01-01\"))\n+\t\tepoch_hour = 0;\n+\telse\n+\t\treturn 0;\n+\n \tif (!stamp) {\n \t\tstamp = xmalloc(sizeof(*stamp));\n \t\tif (regcomp(stamp, stamp_regexp, REG_EXTENDED)) {\n@@ -846,6 +857,9 @@ static int has_epoch_timestamp(const char *nameline)\n \t\treturn 0;\n \t}\n \n+\thour = strtol(timestamp + 11, NULL, 10);\n+\tminute = strtol(timestamp + 14, NULL, 10);\n+\n \tzoneoffset = strtol(timestamp + m[3].rm_so + 1, (char **) &colon, 10);\n \tif (*colon == ':')\n \t\tzoneoffset = zoneoffset * 60 + strtol(colon + 1, NULL, 10);\n@@ -854,20 +868,7 @@ static int has_epoch_timestamp(const char *nameline)\n \tif (timestamp[m[3].rm_so] == '-')\n \t\tzoneoffset = -zoneoffset;\n \n-\t/*\n-\t * YYYY-MM-DD hh:mm:ss must be from either 1969-12-31\n-\t * (west of GMT) or 1970-01-01 (east of GMT)\n-\t */\n-\tif ((zoneoffset < 0 && memcmp(timestamp, \"1969-12-31\", 10)) ||\n-\t    (0 <= zoneoffset && memcmp(timestamp, \"1970-01-01\", 10)))\n-\t\treturn 0;\n-\n-\thourminute = (strtol(timestamp + 11, NULL, 10) * 60 +\n-\t\t      strtol(timestamp + 14, NULL, 10) -\n-\t\t      zoneoffset);\n-\n-\treturn ((zoneoffset < 0 && hourminute == 1440) ||\n-\t\t(0 <= zoneoffset && !hourminute));\n+\treturn hour * 60 + minute - zoneoffset == epoch_hour * 60;\n }\n \n /*\n-- \n2.14.1\n"},{"id":"327214","messageId":"5dd40b51-ac5b-6885-b7b0-f2a33045c386@web.de","threadId":"46669","inReplyTo":"205339f5-bad8-62a3-8ccc-d3b0dd1d6736@web.de","subject":"[PATCH 2/2] apply: remove epoch date from regex","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2017-08-25T19:06:28Z","receivedAt":"2017-08-25T19:06:38Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"We check the date of epoch timestamp candidates already with\nstarts_with().  Move beyond that part using skip_prefix() instead of\nchecking it again using a regular expression.  Also group the minutes\npart, so that we can access them using a substring match instead of\nusing a magic number.\n\nSigned-off-by: Rene Scharfe <l.s.r@web.de>\n---\n apply.c | 12 +++++-------\n 1 file changed, 5 insertions(+), 7 deletions(-)\n\ndiff --git a/apply.c b/apply.c\nindex e14077eaee..9c70b4040a 100644\n--- a/apply.c\n+++ b/apply.c\n@@ -811,9 +811,7 @@ static int has_epoch_timestamp(const char *nameline)\n \t * 1970-01-01, and the seconds part must be \"00\".\n \t */\n \tconst char stamp_regexp[] =\n-\t\t\"^(1969-12-31|1970-01-01)\"\n-\t\t\" \"\n-\t\t\"[0-2][0-9]:[0-5][0-9]:00(\\\\.0+)?\"\n+\t\t\"^[0-2][0-9]:([0-5][0-9]):00(\\\\.0+)?\"\n \t\t\" \"\n \t\t\"([-+][0-2][0-9]:?[0-5][0-9])\\n\";\n \tconst char *timestamp = NULL, *cp, *colon;\n@@ -833,9 +831,9 @@ static int has_epoch_timestamp(const char *nameline)\n \t * YYYY-MM-DD hh:mm:ss must be from either 1969-12-31\n \t * (west of GMT) or 1970-01-01 (east of GMT)\n \t */\n-\tif (starts_with(timestamp, \"1969-12-31\"))\n+\tif (skip_prefix(timestamp, \"1969-12-31 \", &timestamp))\n \t\tepoch_hour = 24;\n-\telse if (starts_with(timestamp, \"1970-01-01\"))\n+\telse if (skip_prefix(timestamp, \"1970-01-01 \", &timestamp))\n \t\tepoch_hour = 0;\n \telse\n \t\treturn 0;\n@@ -857,8 +855,8 @@ static int has_epoch_timestamp(const char *nameline)\n \t\treturn 0;\n \t}\n \n-\thour = strtol(timestamp + 11, NULL, 10);\n-\tminute = strtol(timestamp + 14, NULL, 10);\n+\thour = strtol(timestamp, NULL, 10);\n+\tminute = strtol(timestamp + m[1].rm_so, NULL, 10);\n \n \tzoneoffset = strtol(timestamp + m[3].rm_so + 1, (char **) &colon, 10);\n \tif (*colon == ':')\n-- \n2.14.1\n\n"}]}