Skip to content

Commit 393d340

Browse files
Linus TorvaldsJunio C Hamano
authored andcommitted
Fix approxidate() to understand more extended numbers
You can now say "5:35 PM yesterday", and approxidate() gets the right answer. Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent e92a54d commit 393d340

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

date.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,32 @@ static void date_tea(struct tm *tm, int *num)
598598
date_time(tm, 17);
599599
}
600600

601+
static void date_pm(struct tm *tm, int *num)
602+
{
603+
int hour = *num;
604+
*num = 0;
605+
606+
if (hour > 0 && hour < 12) {
607+
tm->tm_hour = hour;
608+
tm->tm_min = 0;
609+
tm->tm_sec = 0;
610+
}
611+
if (tm->tm_hour > 0 && tm->tm_hour < 12)
612+
tm->tm_hour += 12;
613+
}
614+
615+
static void date_am(struct tm *tm, int *num)
616+
{
617+
int hour = *num;
618+
*num = 0;
619+
620+
if (hour > 0 && hour < 12) {
621+
tm->tm_hour = hour;
622+
tm->tm_min = 0;
623+
tm->tm_sec = 0;
624+
}
625+
}
626+
601627
static const struct special {
602628
const char *name;
603629
void (*fn)(struct tm *, int *);
@@ -606,6 +632,8 @@ static const struct special {
606632
{ "noon", date_noon },
607633
{ "midnight", date_midnight },
608634
{ "tea", date_tea },
635+
{ "PM", date_pm },
636+
{ "AM", date_am },
609637
{ NULL }
610638
};
611639

@@ -717,6 +745,18 @@ static const char *approxidate_digit(const char *date, struct tm *tm, int *num)
717745
char *end;
718746
unsigned long number = strtoul(date, &end, 10);
719747

748+
switch (*end) {
749+
case ':':
750+
case '.':
751+
case '/':
752+
case '-':
753+
if (isdigit(end[1])) {
754+
int match = match_multi_number(number, *end, date, end, tm);
755+
if (match)
756+
return date + match;
757+
}
758+
}
759+
720760
*num = number;
721761
return end;
722762
}

0 commit comments

Comments
 (0)