Skip to content

Commit 4f89f4f

Browse files
sgngitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c933b28 commit 4f89f4f

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

date.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,20 @@ static int set_date(int year, int month, int day, struct tm *now_tm, time_t now,
539539
return -1;
540540
}
541541

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+
542556
static int match_multi_number(timestamp_t num, char c, const char *date,
543557
char *end, struct tm *tm, time_t now)
544558
{
@@ -556,10 +570,7 @@ static int match_multi_number(timestamp_t num, char c, const char *date,
556570
case ':':
557571
if (num3 < 0)
558572
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) {
563574
break;
564575
}
565576
return 0;

0 commit comments

Comments
 (0)