git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Edgar Toernig <froese@gmx.de>
To: Linus Torvalds <torvalds@osdl.org>
Cc: git@vger.kernel.org
Subject: Re: Trying to use AUTHOR_DATE
Date: Sun, 1 May 2005 00:54:34 +0200	[thread overview]
Message-ID: <20050501005434.2d47131a.froese@gmx.de> (raw)
In-Reply-To: <Pine.LNX.4.58.0504301322130.2296@ppc970.osdl.org>

Linus Torvalds wrote:
>
> [...] I just rewrote it to give "almost correct 
> results" for "pretty much any crap you throw at it".

And I had the impression the strict checks in the original
version were intentionally ;-)

> I'll probably tweak it a bit more (make "no timezone means local 
> timezone", for example, rather than UTC like it is now).

Here's my try on that.  But whether it works everywhere ...

Btw, your %+03d%02d printf gave wrong results for i.e. -0130 (-01-30).



--- k/date.c  (mode:100644)
+++ l/date.c  (mode:100644)
@@ -10,7 +10,9 @@
 #include <ctype.h>
 #include <time.h>
 
-static time_t my_mktime(struct tm *tm)
+#define NO_TZ	11111
+
+static time_t utc_mktime(struct tm *tm)
 {
 	static const int mdays[] = {
 	    0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
@@ -23,12 +25,19 @@ static time_t my_mktime(struct tm *tm)
 		return -1;
 	if (month < 0 || month > 11) /* array bounds */
 		return -1;
+	if (day < 1 || day > 31)
+		return -1;
 	if (month < 2 || (year + 2) % 4)
 		day--;
 	return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
 		tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
 }
 
+static int local_offset(time_t *when)
+{
+	return (utc_mktime(localtime(when)) - *when) / 60;
+}
+
 static const char *month_names[] = {
 	"January", "February", "March", "April", "May", "June",
 	"July", "August", "September", "October", "November", "December"
@@ -138,7 +147,8 @@ static int match_alpha(const char *date,
 	for (i = 0; i < NR_TZ; i++) {
 		int match = match_string(date, timezone_names[i].name);
 		if (match >= 3) {
-			*offset = 60*timezone_names[i].offset;
+			if (*offset == NO_TZ)
+				*offset = 60*timezone_names[i].offset;
 			return match;
 		}
 	}
@@ -245,7 +255,7 @@ void parse_date(char *date, char *result
 	tm.tm_year = -1;
 	tm.tm_mon = -1;
 	tm.tm_mday = -1;
-	offset = 0;
+	offset = NO_TZ;
 
 	for (;;) {
 		int match = 0;
@@ -270,13 +280,20 @@ void parse_date(char *date, char *result
 		date += match;
 	}
 
-	then = my_mktime(&tm); /* mktime uses local timezone */
-	if (then == -1)
-		return;
-
-	then -= offset * 60;
+	if (offset == NO_TZ) {
+		tm.tm_isdst = -1;
+		then = mktime(&tm);
+		if (then == -1)
+			return;
+		offset = local_offset(&then);
+	} else {
+		then = utc_mktime(&tm);
+		if (then == -1)
+			return;
+		then -= offset * 60;
+	}
 
-	snprintf(result, maxlen, "%lu %+03d%02d", then, offset/60, offset % 60);
+	snprintf(result, maxlen, "%lu %+05d", then, offset/60*100 + offset%60);
 }
 
 void datestamp(char *buf, int bufsize)
@@ -285,9 +302,7 @@ void datestamp(char *buf, int bufsize)
 	int offset;
 
 	time(&now);
-
-	offset = my_mktime(localtime(&now)) - now;
-	offset /= 60;
+	offset = local_offset(&now);
 
 	snprintf(buf, bufsize, "%lu %+05d", now, offset/60*100 + offset%60);
 }

Ciao, ET.

  parent reply	other threads:[~2005-05-01  1:58 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-04-30  3:44 Trying to use AUTHOR_DATE Luck, Tony
2005-04-30  3:49 ` H. Peter Anvin
2005-04-30  4:02   ` Linus Torvalds
2005-04-30  4:22     ` Linus Torvalds
2005-04-30  4:32       ` Russ Allbery
2005-04-30  8:02         ` David Woodhouse
2005-04-30 10:40           ` Edgar Toernig
2005-04-30 18:10             ` Russ Allbery
2005-04-30 20:32               ` Linus Torvalds
2005-04-30 21:59                 ` Juliusz Chroboczek
2005-04-30 22:54                 ` Edgar Toernig [this message]
2005-04-30 23:18                   ` Linus Torvalds
2005-05-01 16:46                   ` Linus Torvalds
2005-05-01 16:57                     ` Randy.Dunlap
2005-05-01 17:23                     ` Edgar Toernig
2005-04-30  5:43       ` Junio C Hamano
2005-04-30 10:53       ` Edgar Toernig
2005-04-30 11:13         ` David Woodhouse
2005-04-30 12:08           ` Kay Sievers
2005-04-30 12:13             ` David Woodhouse
2005-04-30 12:49           ` Edgar Toernig
2005-04-30 12:59             ` David Woodhouse
2005-04-30 13:22               ` Edgar Toernig
2005-05-02 22:10               ` Krzysztof Halasa
2005-05-02 22:26                 ` H. Peter Anvin
2005-05-02 23:30                   ` Krzysztof Halasa
2005-05-02 23:32                     ` H. Peter Anvin
2005-05-03  0:30                       ` Krzysztof Halasa
2005-05-03  0:38                         ` H. Peter Anvin
2005-04-30 23:14           ` H. Peter Anvin
2005-04-30  4:50 ` Edgar Toernig
  -- strict thread matches above, loose matches on Subject: below --
2005-04-30  5:28 Luck, Tony
2005-04-30 23:14 ` H. Peter Anvin
2005-04-29 23:14 tony.luck
2005-04-29 23:35 ` H. Peter Anvin
2005-04-30  0:21   ` tony.luck
2005-04-30  3:23     ` Edgar Toernig
2005-04-30  3:47       ` H. Peter Anvin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20050501005434.2d47131a.froese@gmx.de \
    --to=froese@gmx.de \
    --cc=git@vger.kernel.org \
    --cc=torvalds@osdl.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).