git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Clean up approxidate() in preparation for fixes
@ 2006-09-28 19:12 Linus Torvalds
  2006-09-28 19:14 ` Fix approxidate() to understand more extended numbers Linus Torvalds
  0 siblings, 1 reply; 9+ messages in thread
From: Linus Torvalds @ 2006-09-28 19:12 UTC (permalink / raw)
  To: Junio C Hamano, Git Mailing List


Our approxidate cannot handle simple times like "5 PM yesterday", and to
fix that, we will need to add some logic for number handling.  This just
splits that out into a function of its own (the same way the _real_ date
parsing works).

Signed-off-by: Linus Torvalds <torvalds@osdl.org>
---

This should change no code what-so-ever, just split it up in preparation 
for the next patch..

diff --git a/date.c b/date.c
index e387dcd..4ff6604 100644
--- a/date.c
+++ b/date.c
@@ -712,6 +712,15 @@ static const char *approxidate_alpha(con
 	return end;
 }
 
+static const char *approxidate_digit(const char *date, struct tm *tm, int *num)
+{
+	char *end;
+	unsigned long number = strtoul(date, &end, 10);
+
+	*num = number;
+	return end;
+}
+
 unsigned long approxidate(const char *date)
 {
 	int number = 0;
@@ -731,9 +740,7 @@ unsigned long approxidate(const char *da
 			break;
 		date++;
 		if (isdigit(c)) {
-			char *end;
-			number = strtoul(date-1, &end, 10);
-			date = end;
+			date = approxidate_digit(date-1, &tm, &number);
 			continue;
 		}
 		if (isalpha(c))

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Fix approxidate() to understand more extended numbers
  2006-09-28 19:12 Clean up approxidate() in preparation for fixes Linus Torvalds
@ 2006-09-28 19:14 ` Linus Torvalds
  2006-09-29  0:12   ` Morten Welinder
  0 siblings, 1 reply; 9+ messages in thread
From: Linus Torvalds @ 2006-09-28 19:14 UTC (permalink / raw)
  To: Junio C Hamano, Git Mailing List


You can now say "5:35 PM yesterday", and approxidate() gets the right answer.

Signed-off-by: Linus Torvalds <torvalds@osdl.org>
---

Useful? Of course. In a sick and twisted sort of way.

Try to say "yesterday at 5 PM" before and after this. It _really_ was 
confused before.

diff --git a/date.c b/date.c
index 4ff6604..db4c185 100644
--- a/date.c
+++ b/date.c
@@ -598,6 +598,32 @@ static void date_tea(struct tm *tm, int 
 	date_time(tm, 17);
 }
 
+static void date_pm(struct tm *tm, int *num)
+{
+	int hour = *num;
+	*num = 0;
+
+	if (hour > 0 && hour < 12) {
+		tm->tm_hour = hour;
+		tm->tm_min = 0;
+		tm->tm_sec = 0;
+	}
+	if (tm->tm_hour > 0 && tm->tm_hour < 12)
+		tm->tm_hour += 12;
+}
+
+static void date_am(struct tm *tm, int *num)
+{
+	int hour = *num;
+	*num = 0;
+
+	if (hour > 0 && hour < 12) {
+		tm->tm_hour = hour;
+		tm->tm_min = 0;
+		tm->tm_sec = 0;
+	}
+}
+
 static const struct special {
 	const char *name;
 	void (*fn)(struct tm *, int *);
@@ -606,6 +632,8 @@ static const struct special {
 	{ "noon", date_noon },
 	{ "midnight", date_midnight },
 	{ "tea", date_tea },
+	{ "PM", date_pm },
+	{ "AM", date_am },
 	{ NULL }
 };
 
@@ -717,6 +745,18 @@ static const char *approxidate_digit(con
 	char *end;
 	unsigned long number = strtoul(date, &end, 10);
 
+	switch (*end) {
+	case ':':
+	case '.':
+	case '/':
+	case '-':
+		if (isdigit(end[1])) {
+			int match = match_multi_number(number, *end, date, end, tm);
+			if (match)
+				return date + match;
+		}
+	}
+
 	*num = number;
 	return end;
 }

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: Fix approxidate() to understand more extended numbers
  2006-09-28 19:14 ` Fix approxidate() to understand more extended numbers Linus Torvalds
@ 2006-09-29  0:12   ` Morten Welinder
  2006-09-29  6:03     ` Linus Torvalds
  0 siblings, 1 reply; 9+ messages in thread
From: Morten Welinder @ 2006-09-29  0:12 UTC (permalink / raw)
  To: Linus Torvalds; +Cc: Junio C Hamano, Git Mailing List

Just don't hack at 12am or 12pm.

M.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Fix approxidate() to understand more extended numbers
  2006-09-29  0:12   ` Morten Welinder
@ 2006-09-29  6:03     ` Linus Torvalds
  2006-09-29  6:14       ` Junio C Hamano
  0 siblings, 1 reply; 9+ messages in thread
From: Linus Torvalds @ 2006-09-29  6:03 UTC (permalink / raw)
  To: Morten Welinder; +Cc: Junio C Hamano, Git Mailing List



On Thu, 28 Sep 2006, Morten Welinder wrote:
>
> Just don't hack at 12am or 12pm.

I think 12pm is correct, but 12am probably isn't (12am should _subtract_ 
12, while 12pm does _not_ add 12).

That said, I have a rice cooker that avoids the problem by saying "0:10 PM" 
for ten minutes past midday ;)

Of course, all sane and civilized countries just use 24-hour format 
anyway. "Military time" my *ss. 

Some day the US will turn metric and 24-hour-format. If the sun doesn't 
turn into a red giant first, that is.

		Linus

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Fix approxidate() to understand more extended numbers
  2006-09-29  6:03     ` Linus Torvalds
@ 2006-09-29  6:14       ` Junio C Hamano
  2006-09-29  6:42         ` Linus Torvalds
  0 siblings, 1 reply; 9+ messages in thread
From: Junio C Hamano @ 2006-09-29  6:14 UTC (permalink / raw)
  To: Linus Torvalds; +Cc: Morten Welinder, Git Mailing List

Linus Torvalds <torvalds@osdl.org> writes:

> On Thu, 28 Sep 2006, Morten Welinder wrote:
>>
>> Just don't hack at 12am or 12pm.
>
> I think 12pm is correct, but 12am probably isn't (12am should _subtract_ 
> 12, while 12pm does _not_ add 12).

But you have "if (hour > 0 && hour < 12)" in both am and pm so
assignment to tm would not trigger...

> That said, I have a rice cooker that avoids the problem by saying "0:10 PM" 
> for ten minutes past midday ;)

You eat rice?

> Of course, all sane and civilized countries just use 24-hour format 
> anyway.

You are referring to the US, but neither is Japan sane nor
civilized ;-).

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Fix approxidate() to understand more extended numbers
  2006-09-29  6:14       ` Junio C Hamano
@ 2006-09-29  6:42         ` Linus Torvalds
  2006-09-29  7:09           ` Junio C Hamano
  0 siblings, 1 reply; 9+ messages in thread
From: Linus Torvalds @ 2006-09-29  6:42 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Morten Welinder, Git Mailing List



On Thu, 28 Sep 2006, Junio C Hamano wrote:

> Linus Torvalds <torvalds@osdl.org> writes:
> 
> > On Thu, 28 Sep 2006, Morten Welinder wrote:
> >>
> >> Just don't hack at 12am or 12pm.
> >
> > I think 12pm is correct, but 12am probably isn't (12am should _subtract_ 
> > 12, while 12pm does _not_ add 12).
> 
> But you have "if (hour > 0 && hour < 12)" in both am and pm so
> assignment to tm would not trigger...

That's not the point.

If you write

	12:30 am

you really _should_ subtract 12, leaving you with 0:30. We don't. So we 
end up with a 24-hour time of 12:30, which is obviously _pm_, and wrong.

And "12 am" or "12 pm" doesn't work at all.

> > That said, I have a rice cooker that avoids the problem by saying "0:10 PM" 
> > for ten minutes past midday ;)
> 
> You eat rice?

Ok, is it just me, or is that just a very odd question?

I can see the question "You eat uni?". That really _does_ take a bit of 
getting used to. And Natto I really _really_ don't see the point of. 

But rice? Afaik, it's the most common food-staple in the world. It's not 
exactly odd and exotic..

> > Of course, all sane and civilized countries just use 24-hour format 
> > anyway.
> 
> You are referring to the US, but neither is Japan sane nor
> civilized ;-).

Yeah, well, they've been learning bad habits. But at least they are 
metric.

		Linus

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Fix approxidate() to understand more extended numbers
  2006-09-29  6:42         ` Linus Torvalds
@ 2006-09-29  7:09           ` Junio C Hamano
  2006-09-29 14:04             ` Johannes Schindelin
  2006-09-29 19:36             ` Linus Torvalds
  0 siblings, 2 replies; 9+ messages in thread
From: Junio C Hamano @ 2006-09-29  7:09 UTC (permalink / raw)
  To: Linus Torvalds; +Cc: git

Linus Torvalds <torvalds@osdl.org> writes:

>> > I think 12pm is correct, but 12am probably isn't (12am should _subtract_ 
>> > 12, while 12pm does _not_ add 12).
>> 
>> But you have "if (hour > 0 && hour < 12)" in both am and pm so
>> assignment to tm would not trigger...
>
> That's not the point.
>
> If you write
>
> 	12:30 am
>
> you really _should_ subtract 12, leaving you with 0:30. We don't. So we 
> end up with a 24-hour time of 12:30, which is obviously _pm_, and wrong.
>
> And "12 am" or "12 pm" doesn't work at all.

Ah, that's what you meant.  My brain a bit too tired from the
day job tonight X-<.

>> > That said, I have a rice cooker that avoids the problem by saying "0:10 PM" 
>> > for ten minutes past midday ;)
>> 
>> You eat rice?
>
> Ok, is it just me, or is that just a very odd question?

Having rice cooker implied eating the stuff regularly and I just
did not expect that from a north european.  Just showing my
ignorance -- I've never been to Europe.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Fix approxidate() to understand more extended numbers
  2006-09-29  7:09           ` Junio C Hamano
@ 2006-09-29 14:04             ` Johannes Schindelin
  2006-09-29 19:36             ` Linus Torvalds
  1 sibling, 0 replies; 9+ messages in thread
From: Johannes Schindelin @ 2006-09-29 14:04 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Linus Torvalds, git

Hi,

On Fri, 29 Sep 2006, Junio C Hamano wrote:

> Having rice cooker implied eating the stuff regularly and I just
> did not expect that from a north european.  Just showing my
> ignorance -- I've never been to Europe.

Actually, yesterday I saw a digital wrist watch for the first time in my 
life! Amazing! ;-)

There is even a running joke about Great Britain: "Oh yes, I _love_ 
English dishes; especially the Chinese ones!"

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Fix approxidate() to understand more extended numbers
  2006-09-29  7:09           ` Junio C Hamano
  2006-09-29 14:04             ` Johannes Schindelin
@ 2006-09-29 19:36             ` Linus Torvalds
  1 sibling, 0 replies; 9+ messages in thread
From: Linus Torvalds @ 2006-09-29 19:36 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git



On Fri, 29 Sep 2006, Junio C Hamano wrote:
> Linus Torvalds <torvalds@osdl.org> writes:
> >
> > If you write
> >
> > 	12:30 am
> >
> > you really _should_ subtract 12, leaving you with 0:30. We don't. So we 
> > end up with a 24-hour time of 12:30, which is obviously _pm_, and wrong.
> >
> > And "12 am" or "12 pm" doesn't work at all.
> 
> Ah, that's what you meant.  My brain a bit too tired from the
> day job tonight X-<.

Here's a patch that should work. 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.

Not hugely tested, but I did test some of it, and it all _looks_ sane.

		Linus
---
diff --git a/date.c b/date.c
index db4c185..1825922 100644
--- a/date.c
+++ b/date.c
@@ -256,8 +256,12 @@ static int match_alpha(const char *date,
 	}
 
 	if (match_string(date, "PM") == 2) {
-		if (tm->tm_hour > 0 && tm->tm_hour < 12)
-			tm->tm_hour += 12;
+		tm->tm_hour = (tm->tm_hour % 12) + 12;
+		return 2;
+	}
+
+	if (match_string(date, "AM") == 2) {
+		tm->tm_hour = (tm->tm_hour % 12) + 0;
 		return 2;
 	}
 
@@ -600,28 +604,30 @@ static void date_tea(struct tm *tm, int 
 
 static void date_pm(struct tm *tm, int *num)
 {
-	int hour = *num;
+	int hour, n = *num;
 	*num = 0;
 
-	if (hour > 0 && hour < 12) {
-		tm->tm_hour = hour;
+	hour = tm->tm_hour;
+	if (n) {
+		hour = n;
 		tm->tm_min = 0;
 		tm->tm_sec = 0;
 	}
-	if (tm->tm_hour > 0 && tm->tm_hour < 12)
-		tm->tm_hour += 12;
+	tm->tm_hour = (hour % 12) + 12;
 }
 
 static void date_am(struct tm *tm, int *num)
 {
-	int hour = *num;
+	int hour, n = *num;
 	*num = 0;
 
-	if (hour > 0 && hour < 12) {
-		tm->tm_hour = hour;
+	hour = tm->tm_hour;
+	if (n) {
+		hour = n;
 		tm->tm_min = 0;
 		tm->tm_sec = 0;
 	}
+	tm->tm_hour = (hour % 12);
 }
 
 static const struct special {

^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2006-09-29 19:36 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-09-28 19:12 Clean up approxidate() in preparation for fixes Linus Torvalds
2006-09-28 19:14 ` Fix approxidate() to understand more extended numbers Linus Torvalds
2006-09-29  0:12   ` Morten Welinder
2006-09-29  6:03     ` Linus Torvalds
2006-09-29  6:14       ` Junio C Hamano
2006-09-29  6:42         ` Linus Torvalds
2006-09-29  7:09           ` Junio C Hamano
2006-09-29 14:04             ` Johannes Schindelin
2006-09-29 19:36             ` Linus Torvalds

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).