@matheusbsbs / matheus-bagliano / commits / 4f89f4fc9a
Fork 0

date.c: validate and set time in a helper function

In a later patch, we will reuse this logic, move it to a helper, now. While we're at it, explicit states that we intentionally ignore old-and-defective 2nd leap second. Signed-off-by: Đoàn Trần Công Danh <congdanhqx@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Đoàn Trần Công Danh committed Apr 24, 2020 at 22:07 UTC 4f89f4fc9ac494fde3f3bede19a7599f77afe8dc
1 file changed +15 -4
date.c
+15 -4
@@ -539,6 +539,20 @@ static int set_date(int year, int month, int day, struct tm *now_tm, time_t now,
539 return -1;
540 }
541
542 +static int set_time(long hour, long minute, long second, struct tm *tm)
543 +{
544 + /* We accept 61st second because of leap second */
545 + if (0 <= hour && hour <= 24 &&
546 + 0 <= minute && minute < 60 &&
547 + 0 <= second && second <= 60) {
548 + tm->tm_hour = hour;
549 + tm->tm_min = minute;
550 + tm->tm_sec = second;
551 + return 0;
552 + }
553 + return -1;
554 +}
555 +
556 static int match_multi_number(timestamp_t num, char c, const char *date,
557 char *end, struct tm *tm, time_t now)
558 {
@@ -556,10 +570,7 @@ static int match_multi_number(timestamp_t num, char c, const char *date,
570 case ':':
571 if (num3 < 0)
572 num3 = 0;
559 - if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) {
560 - tm->tm_hour = num;
561 - tm->tm_min = num2;
562 - tm->tm_sec = num3;
573 + if (set_time(num, num2, num3, tm) == 0) {
574 break;
575 }
576 return 0;