Fix approxidate() to understand 12:34 AM/PM are 00:34 and 12:34
It just simplifies the whole thing to say "hour = (hour % 12) + X" where X is 12 for PM and 0 for AM. It also fixes the "exact date" parsing, which didn't parse AM at all, and as such would do the same "12:30 AM" means "12:30 24-hour-format" bug. Of course, I hope that no exact dates use AM/PM anyway, but since we support the PM format, let's just get it right. Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Linus Torvalds committed
Sep 29, 2006 at 12:36 UTC
18b633cafcd43b83d752738fea62f185d4de1b91
1 file changed
+16
-10
date.c
+16
-10
@@ -256,8 +256,12 @@ static int match_alpha(const char *date, struct tm *tm, int *offset)
256
}
257
258
if (match_string(date, "PM") == 2) {
259
- if (tm->tm_hour > 0 && tm->tm_hour < 12)
260
- tm->tm_hour += 12;
259
+ tm->tm_hour = (tm->tm_hour % 12) + 12;
260
+ return 2;
261
+ }
262
+
263
+ if (match_string(date, "AM") == 2) {
264
+ tm->tm_hour = (tm->tm_hour % 12) + 0;
265
return 2;
266
}
267
@@ -600,28 +604,30 @@ static void date_tea(struct tm *tm, int *num)
604
605
static void date_pm(struct tm *tm, int *num)
606
{
603
- int hour = *num;
607
+ int hour, n = *num;
608
*num = 0;
609
606
- if (hour > 0 && hour < 12) {
607
- tm->tm_hour = hour;
610
+ hour = tm->tm_hour;
611
+ if (n) {
612
+ hour = n;
613
tm->tm_min = 0;
614
tm->tm_sec = 0;
615
}
611
- if (tm->tm_hour > 0 && tm->tm_hour < 12)
612
- tm->tm_hour += 12;
616
+ tm->tm_hour = (hour % 12) + 12;
617
}
618
619
static void date_am(struct tm *tm, int *num)
620
{
617
- int hour = *num;
621
+ int hour, n = *num;
622
*num = 0;
623
620
- if (hour > 0 && hour < 12) {
621
- tm->tm_hour = hour;
624
+ hour = tm->tm_hour;
625
+ if (n) {
626
+ hour = n;
627
tm->tm_min = 0;
628
tm->tm_sec = 0;
629
}
630
+ tm->tm_hour = (hour % 12);
631
}
632
633
static const struct special {