{"thread":{"id":"5743","subject":"Clean up approxidate() in preparation for fixes","startedAt":"2006-09-28T19:12:28Z","lastAt":"2006-09-29T19:36:13Z","messageCount":9,"participants":["Linus Torvalds","Morten Welinder","Junio C Hamano","Johannes Schindelin"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"27919","messageId":"Pine.LNX.4.64.0609281211260.3952@g5.osdl.org","threadId":"5743","inReplyTo":null,"subject":"Clean up approxidate() in preparation for fixes","fromName":"Linus Torvalds","fromEmail":"torvalds@osdl.org","sentAt":"2006-09-28T19:12:28Z","receivedAt":"2006-09-28T19:12:28Z","isPatch":false,"sender":{"key":"torvalds@linux-foundation.org","avatar":"https://avatars.githubusercontent.com/u/1024025?v=4"},"body":"\nOur approxidate cannot handle simple times like \"5 PM yesterday\", and to\nfix that, we will need to add some logic for number handling.  This just\nsplits that out into a function of its own (the same way the _real_ date\nparsing works).\n\nSigned-off-by: Linus Torvalds <torvalds@osdl.org>\n---\n\nThis should change no code what-so-ever, just split it up in preparation \nfor the next patch..\n\ndiff --git a/date.c b/date.c\nindex e387dcd..4ff6604 100644\n--- a/date.c\n+++ b/date.c\n@@ -712,6 +712,15 @@ static const char *approxidate_alpha(con\n \treturn end;\n }\n \n+static const char *approxidate_digit(const char *date, struct tm *tm, int *num)\n+{\n+\tchar *end;\n+\tunsigned long number = strtoul(date, &end, 10);\n+\n+\t*num = number;\n+\treturn end;\n+}\n+\n unsigned long approxidate(const char *date)\n {\n \tint number = 0;\n@@ -731,9 +740,7 @@ unsigned long approxidate(const char *da\n \t\t\tbreak;\n \t\tdate++;\n \t\tif (isdigit(c)) {\n-\t\t\tchar *end;\n-\t\t\tnumber = strtoul(date-1, &end, 10);\n-\t\t\tdate = end;\n+\t\t\tdate = approxidate_digit(date-1, &tm, &number);\n \t\t\tcontinue;\n \t\t}\n \t\tif (isalpha(c))\n"},{"id":"27922","messageId":"Pine.LNX.4.64.0609281212380.3952@g5.osdl.org","threadId":"5743","inReplyTo":"Pine.LNX.4.64.0609281211260.3952@g5.osdl.org","subject":"Fix approxidate() to understand more extended numbers","fromName":"Linus Torvalds","fromEmail":"torvalds@osdl.org","sentAt":"2006-09-28T19:14:27Z","receivedAt":"2006-09-28T19:14:27Z","isPatch":false,"sender":{"key":"torvalds@linux-foundation.org","avatar":"https://avatars.githubusercontent.com/u/1024025?v=4"},"body":"\nYou can now say \"5:35 PM yesterday\", and approxidate() gets the right answer.\n\nSigned-off-by: Linus Torvalds <torvalds@osdl.org>\n---\n\nUseful? Of course. In a sick and twisted sort of way.\n\nTry to say \"yesterday at 5 PM\" before and after this. It _really_ was \nconfused before.\n\ndiff --git a/date.c b/date.c\nindex 4ff6604..db4c185 100644\n--- a/date.c\n+++ b/date.c\n@@ -598,6 +598,32 @@ static void date_tea(struct tm *tm, int \n \tdate_time(tm, 17);\n }\n \n+static void date_pm(struct tm *tm, int *num)\n+{\n+\tint hour = *num;\n+\t*num = 0;\n+\n+\tif (hour > 0 && hour < 12) {\n+\t\ttm->tm_hour = hour;\n+\t\ttm->tm_min = 0;\n+\t\ttm->tm_sec = 0;\n+\t}\n+\tif (tm->tm_hour > 0 && tm->tm_hour < 12)\n+\t\ttm->tm_hour += 12;\n+}\n+\n+static void date_am(struct tm *tm, int *num)\n+{\n+\tint hour = *num;\n+\t*num = 0;\n+\n+\tif (hour > 0 && hour < 12) {\n+\t\ttm->tm_hour = hour;\n+\t\ttm->tm_min = 0;\n+\t\ttm->tm_sec = 0;\n+\t}\n+}\n+\n static const struct special {\n \tconst char *name;\n \tvoid (*fn)(struct tm *, int *);\n@@ -606,6 +632,8 @@ static const struct special {\n \t{ \"noon\", date_noon },\n \t{ \"midnight\", date_midnight },\n \t{ \"tea\", date_tea },\n+\t{ \"PM\", date_pm },\n+\t{ \"AM\", date_am },\n \t{ NULL }\n };\n \n@@ -717,6 +745,18 @@ static const char *approxidate_digit(con\n \tchar *end;\n \tunsigned long number = strtoul(date, &end, 10);\n \n+\tswitch (*end) {\n+\tcase ':':\n+\tcase '.':\n+\tcase '/':\n+\tcase '-':\n+\t\tif (isdigit(end[1])) {\n+\t\t\tint match = match_multi_number(number, *end, date, end, tm);\n+\t\t\tif (match)\n+\t\t\t\treturn date + match;\n+\t\t}\n+\t}\n+\n \t*num = number;\n \treturn end;\n }\n"},{"id":"27960","messageId":"118833cc0609281712u2ce01bc5r8f3e97ae9c9a749a@mail.gmail.com","threadId":"5743","inReplyTo":"Pine.LNX.4.64.0609281212380.3952@g5.osdl.org","subject":"Re: Fix approxidate() to understand more extended numbers","fromName":"Morten Welinder","fromEmail":"mwelinder@gmail.com","sentAt":"2006-09-29T00:12:23Z","receivedAt":"2006-09-29T00:12:23Z","isPatch":false,"sender":{"key":"mwelinder@gmail.com","avatar":null},"body":"Just don't hack at 12am or 12pm.\n\nM.\n"},{"id":"27974","messageId":"Pine.LNX.4.64.0609282300190.3952@g5.osdl.org","threadId":"5743","inReplyTo":"118833cc0609281712u2ce01bc5r8f3e97ae9c9a749a@mail.gmail.com","subject":"Re: Fix approxidate() to understand more extended numbers","fromName":"Linus Torvalds","fromEmail":"torvalds@osdl.org","sentAt":"2006-09-29T06:03:11Z","receivedAt":"2006-09-29T06:03:11Z","isPatch":false,"sender":{"key":"torvalds@linux-foundation.org","avatar":"https://avatars.githubusercontent.com/u/1024025?v=4"},"body":"\n\nOn Thu, 28 Sep 2006, Morten Welinder wrote:\n>\n> Just don't hack at 12am or 12pm.\n\nI think 12pm is correct, but 12am probably isn't (12am should _subtract_ \n12, while 12pm does _not_ add 12).\n\nThat said, I have a rice cooker that avoids the problem by saying \"0:10 PM\" \nfor ten minutes past midday ;)\n\nOf course, all sane and civilized countries just use 24-hour format \nanyway. \"Military time\" my *ss. \n\nSome day the US will turn metric and 24-hour-format. If the sun doesn't \nturn into a red giant first, that is.\n\n\t\tLinus\n"},{"id":"27977","messageId":"7vd59fp5b9.fsf@assigned-by-dhcp.cox.net","threadId":"5743","inReplyTo":"Pine.LNX.4.64.0609282300190.3952@g5.osdl.org","subject":"Re: Fix approxidate() to understand more extended numbers","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2006-09-29T06:14:50Z","receivedAt":"2006-09-29T06:14:50Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Linus Torvalds <torvalds@osdl.org> writes:\n\n> On Thu, 28 Sep 2006, Morten Welinder wrote:\n>>\n>> Just don't hack at 12am or 12pm.\n>\n> I think 12pm is correct, but 12am probably isn't (12am should _subtract_ \n> 12, while 12pm does _not_ add 12).\n\nBut you have \"if (hour > 0 && hour < 12)\" in both am and pm so\nassignment to tm would not trigger...\n\n> That said, I have a rice cooker that avoids the problem by saying \"0:10 PM\" \n> for ten minutes past midday ;)\n\nYou eat rice?\n\n> Of course, all sane and civilized countries just use 24-hour format \n> anyway.\n\nYou are referring to the US, but neither is Japan sane nor\ncivilized ;-).\n"},{"id":"27980","messageId":"Pine.LNX.4.64.0609282330440.3952@g5.osdl.org","threadId":"5743","inReplyTo":"7vd59fp5b9.fsf@assigned-by-dhcp.cox.net","subject":"Re: Fix approxidate() to understand more extended numbers","fromName":"Linus Torvalds","fromEmail":"torvalds@osdl.org","sentAt":"2006-09-29T06:42:19Z","receivedAt":"2006-09-29T06:42:19Z","isPatch":false,"sender":{"key":"torvalds@linux-foundation.org","avatar":"https://avatars.githubusercontent.com/u/1024025?v=4"},"body":"\n\nOn Thu, 28 Sep 2006, Junio C Hamano wrote:\n\n> Linus Torvalds <torvalds@osdl.org> writes:\n> \n> > On Thu, 28 Sep 2006, Morten Welinder wrote:\n> >>\n> >> Just don't hack at 12am or 12pm.\n> >\n> > I think 12pm is correct, but 12am probably isn't (12am should _subtract_ \n> > 12, while 12pm does _not_ add 12).\n> \n> But you have \"if (hour > 0 && hour < 12)\" in both am and pm so\n> assignment to tm would not trigger...\n\nThat's not the point.\n\nIf you write\n\n\t12:30 am\n\nyou really _should_ subtract 12, leaving you with 0:30. We don't. So we \nend up with a 24-hour time of 12:30, which is obviously _pm_, and wrong.\n\nAnd \"12 am\" or \"12 pm\" doesn't work at all.\n\n> > That said, I have a rice cooker that avoids the problem by saying \"0:10 PM\" \n> > for ten minutes past midday ;)\n> \n> You eat rice?\n\nOk, is it just me, or is that just a very odd question?\n\nI can see the question \"You eat uni?\". That really _does_ take a bit of \ngetting used to. And Natto I really _really_ don't see the point of. \n\nBut rice? Afaik, it's the most common food-staple in the world. It's not \nexactly odd and exotic..\n\n> > Of course, all sane and civilized countries just use 24-hour format \n> > anyway.\n> \n> You are referring to the US, but neither is Japan sane nor\n> civilized ;-).\n\nYeah, well, they've been learning bad habits. But at least they are \nmetric.\n\n\t\tLinus\n"},{"id":"27982","messageId":"7vslibno88.fsf@assigned-by-dhcp.cox.net","threadId":"5743","inReplyTo":"Pine.LNX.4.64.0609282330440.3952@g5.osdl.org","subject":"Re: Fix approxidate() to understand more extended numbers","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2006-09-29T07:09:11Z","receivedAt":"2006-09-29T07:09:11Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Linus Torvalds <torvalds@osdl.org> writes:\n\n>> > I think 12pm is correct, but 12am probably isn't (12am should _subtract_ \n>> > 12, while 12pm does _not_ add 12).\n>> \n>> But you have \"if (hour > 0 && hour < 12)\" in both am and pm so\n>> assignment to tm would not trigger...\n>\n> That's not the point.\n>\n> If you write\n>\n> \t12:30 am\n>\n> you really _should_ subtract 12, leaving you with 0:30. We don't. So we \n> end up with a 24-hour time of 12:30, which is obviously _pm_, and wrong.\n>\n> And \"12 am\" or \"12 pm\" doesn't work at all.\n\nAh, that's what you meant.  My brain a bit too tired from the\nday job tonight X-<.\n\n>> > That said, I have a rice cooker that avoids the problem by saying \"0:10 PM\" \n>> > for ten minutes past midday ;)\n>> \n>> You eat rice?\n>\n> Ok, is it just me, or is that just a very odd question?\n\nHaving rice cooker implied eating the stuff regularly and I just\ndid not expect that from a north european.  Just showing my\nignorance -- I've never been to Europe.\n"},{"id":"27993","messageId":"Pine.LNX.4.63.0609291602200.14200@wbgn013.biozentrum.uni-wuerzburg.de","threadId":"5743","inReplyTo":"7vslibno88.fsf@assigned-by-dhcp.cox.net","subject":"Re: Fix approxidate() to understand more extended numbers","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2006-09-29T14:04:38Z","receivedAt":"2006-09-29T14:04:38Z","isPatch":false,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Fri, 29 Sep 2006, Junio C Hamano wrote:\n\n> Having rice cooker implied eating the stuff regularly and I just\n> did not expect that from a north european.  Just showing my\n> ignorance -- I've never been to Europe.\n\nActually, yesterday I saw a digital wrist watch for the first time in my \nlife! Amazing! ;-)\n\nThere is even a running joke about Great Britain: \"Oh yes, I _love_ \nEnglish dishes; especially the Chinese ones!\"\n\nCiao,\nDscho\n"},{"id":"28009","messageId":"Pine.LNX.4.64.0609291231560.3952@g5.osdl.org","threadId":"5743","inReplyTo":"7vslibno88.fsf@assigned-by-dhcp.cox.net","subject":"Re: Fix approxidate() to understand more extended numbers","fromName":"Linus Torvalds","fromEmail":"torvalds@osdl.org","sentAt":"2006-09-29T19:36:13Z","receivedAt":"2006-09-29T19:36:13Z","isPatch":false,"sender":{"key":"torvalds@linux-foundation.org","avatar":"https://avatars.githubusercontent.com/u/1024025?v=4"},"body":"\n\nOn Fri, 29 Sep 2006, Junio C Hamano wrote:\n> Linus Torvalds <torvalds@osdl.org> writes:\n> >\n> > If you write\n> >\n> > \t12:30 am\n> >\n> > you really _should_ subtract 12, leaving you with 0:30. We don't. So we \n> > end up with a 24-hour time of 12:30, which is obviously _pm_, and wrong.\n> >\n> > And \"12 am\" or \"12 pm\" doesn't work at all.\n> \n> Ah, that's what you meant.  My brain a bit too tired from the\n> day job tonight X-<.\n\nHere's a patch that should work. It just simplifies the whole thing to say \n\n\t\"hour = (hour % 12) + X\"\n\nwhere X is 12 for PM and 0 for AM.\n\nIt also fixes the \"exact date\" parsing, which didn't parse AM at all, and \nas such would do the same \"12:30 AM\" means \"12:30 24-hour-format\" bug. Of \ncourse, I hope that no exact dates use AM/PM anyway, but since we support \nthe PM format, let's just get it right.\n\nNot hugely tested, but I did test some of it, and it all _looks_ sane.\n\n\t\tLinus\n---\ndiff --git a/date.c b/date.c\nindex db4c185..1825922 100644\n--- a/date.c\n+++ b/date.c\n@@ -256,8 +256,12 @@ static int match_alpha(const char *date,\n \t}\n \n \tif (match_string(date, \"PM\") == 2) {\n-\t\tif (tm->tm_hour > 0 && tm->tm_hour < 12)\n-\t\t\ttm->tm_hour += 12;\n+\t\ttm->tm_hour = (tm->tm_hour % 12) + 12;\n+\t\treturn 2;\n+\t}\n+\n+\tif (match_string(date, \"AM\") == 2) {\n+\t\ttm->tm_hour = (tm->tm_hour % 12) + 0;\n \t\treturn 2;\n \t}\n \n@@ -600,28 +604,30 @@ static void date_tea(struct tm *tm, int \n \n static void date_pm(struct tm *tm, int *num)\n {\n-\tint hour = *num;\n+\tint hour, n = *num;\n \t*num = 0;\n \n-\tif (hour > 0 && hour < 12) {\n-\t\ttm->tm_hour = hour;\n+\thour = tm->tm_hour;\n+\tif (n) {\n+\t\thour = n;\n \t\ttm->tm_min = 0;\n \t\ttm->tm_sec = 0;\n \t}\n-\tif (tm->tm_hour > 0 && tm->tm_hour < 12)\n-\t\ttm->tm_hour += 12;\n+\ttm->tm_hour = (hour % 12) + 12;\n }\n \n static void date_am(struct tm *tm, int *num)\n {\n-\tint hour = *num;\n+\tint hour, n = *num;\n \t*num = 0;\n \n-\tif (hour > 0 && hour < 12) {\n-\t\ttm->tm_hour = hour;\n+\thour = tm->tm_hour;\n+\tif (n) {\n+\t\thour = n;\n \t\ttm->tm_min = 0;\n \t\ttm->tm_sec = 0;\n \t}\n+\ttm->tm_hour = (hour % 12);\n }\n \n static const struct special {\n"}]}