git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Add date format --local-zone
@ 2007-04-25  6:36 Junio C Hamano
  2007-04-25  6:44 ` Shawn O. Pearce
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Junio C Hamano @ 2007-04-25  6:36 UTC (permalink / raw)
  To: git

This adds --local-zone option to log family of commands, to
display timestamps in user's local timezone.

Signed-off-by: Junio C Hamano <junkio@cox.net>
---

 * I got tired of looking at +0000 timestamps on commits
   imported from CVS at day-job.  Does not seem to break
   existing tests, but I am not sure if this is useful.

 builtin-rev-list.c |    2 +-
 cache.h            |    2 +-
 commit.c           |   12 ++++++------
 commit.h           |    2 +-
 date.c             |   31 +++++++++++++++++++++++++++++++
 log-tree.c         |    4 ++--
 revision.c         |    6 +++++-
 revision.h         |    2 +-
 8 files changed, 48 insertions(+), 13 deletions(-)

diff --git a/builtin-rev-list.c b/builtin-rev-list.c
index c0329dc..ebf53f5 100644
--- a/builtin-rev-list.c
+++ b/builtin-rev-list.c
@@ -95,7 +95,7 @@ static void show_commit(struct commit *commit)
 		static char pretty_header[16384];
 		pretty_print_commit(revs.commit_format, commit, ~0,
 				    pretty_header, sizeof(pretty_header),
-				    revs.abbrev, NULL, NULL, revs.relative_date);
+				    revs.abbrev, NULL, NULL, revs.date_mode);
 		printf("%s%c", pretty_header, hdr_termination);
 	}
 	fflush(stdout);
diff --git a/cache.h b/cache.h
index f05b67a..6620591 100644
--- a/cache.h
+++ b/cache.h
@@ -389,7 +389,7 @@ extern void *read_object_with_reference(const unsigned char *sha1,
 					unsigned long *size,
 					unsigned char *sha1_ret);
 
-enum date_mode { DATE_NORMAL = 0, DATE_RELATIVE, DATE_SHORT };
+enum date_mode { DATE_NORMAL = 0, DATE_RELATIVE, DATE_SHORT, DATE_LOCAL };
 const char *show_date(unsigned long time, int timezone, enum date_mode mode);
 const char *show_rfc2822_date(unsigned long time, int timezone);
 int parse_date(const char *date, char *buf, int bufsize);
diff --git a/commit.c b/commit.c
index 10466c4..f1ba972 100644
--- a/commit.c
+++ b/commit.c
@@ -526,7 +526,7 @@ static int add_rfc2047(char *buf, const char *line, int len,
 }
 
 static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf,
-			 const char *line, int relative_date,
+			 const char *line, enum date_mode dmode,
 			 const char *encoding)
 {
 	char *date;
@@ -569,7 +569,7 @@ static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf,
 	switch (fmt) {
 	case CMIT_FMT_MEDIUM:
 		ret += sprintf(buf + ret, "Date:   %s\n",
-			       show_date(time, tz, relative_date));
+			       show_date(time, tz, dmode));
 		break;
 	case CMIT_FMT_EMAIL:
 		ret += sprintf(buf + ret, "Date: %s\n",
@@ -577,7 +577,7 @@ static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf,
 		break;
 	case CMIT_FMT_FULLER:
 		ret += sprintf(buf + ret, "%sDate: %s\n", what,
-			       show_date(time, tz, relative_date));
+			       show_date(time, tz, dmode));
 		break;
 	default:
 		/* notin' */
@@ -919,7 +919,7 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt,
 				  char *buf, unsigned long space,
 				  int abbrev, const char *subject,
 				  const char *after_subject,
-				  int relative_date)
+				  enum date_mode dmode)
 {
 	int hdr = 1, body = 0, seen_title = 0;
 	unsigned long offset = 0;
@@ -1023,14 +1023,14 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt,
 				offset += add_user_info("Author", fmt,
 							buf + offset,
 							line + 7,
-							relative_date,
+							dmode,
 							encoding);
 			if (!memcmp(line, "committer ", 10) &&
 			    (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER))
 				offset += add_user_info("Commit", fmt,
 							buf + offset,
 							line + 10,
-							relative_date,
+							dmode,
 							encoding);
 			continue;
 		}
diff --git a/commit.h b/commit.h
index 59de17e..86e8dca 100644
--- a/commit.h
+++ b/commit.h
@@ -61,7 +61,7 @@ enum cmit_fmt {
 };
 
 extern enum cmit_fmt get_commit_format(const char *arg);
-extern unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *, unsigned long len, char *buf, unsigned long space, int abbrev, const char *subject, const char *after_subject, int relative_date);
+extern unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *, unsigned long len, char *buf, unsigned long space, int abbrev, const char *subject, const char *after_subject, enum date_mode dmode);
 
 /** Removes the first commit from a list sorted by date, and adds all
  * of its parents.
diff --git a/date.c b/date.c
index 0ceccbe..aa6ac33 100644
--- a/date.c
+++ b/date.c
@@ -55,6 +55,34 @@ static struct tm *time_to_tm(unsigned long time, int tz)
 	return gmtime(&t);
 }
 
+/*
+ * What value of "tz" was in effect back then at "time" in the
+ * local timezone?
+ */
+static int local_tzoffset(unsigned long time)
+{
+	time_t t, t_local, t_gm;
+	struct tm tm;
+	int offset, eastwest;
+
+	t = time;
+	localtime_r(&t, &tm);
+	t_local = my_mktime(&tm);
+	gmtime_r(&t, &tm);
+	t_gm = my_mktime(&tm);
+
+	if (t_local < t_gm) {
+		eastwest = -1;
+		offset = t_gm - t_local;
+	} else {
+		eastwest = 1;
+		offset = t_local - t_gm;
+	}
+	offset /= 60; /* in minutes */
+	offset = (offset % 60) + ((offset / 60) * 100);
+	return offset * eastwest;
+}
+
 const char *show_date(unsigned long time, int tz, enum date_mode mode)
 {
 	struct tm *tm;
@@ -102,6 +130,9 @@ const char *show_date(unsigned long time, int tz, enum date_mode mode)
 		/* Else fall back on absolute format.. */
 	}
 
+	if (mode == DATE_LOCAL)
+		tz = local_tzoffset(time);
+
 	tm = time_to_tm(time, tz);
 	if (!tm)
 		return NULL;
diff --git a/log-tree.c b/log-tree.c
index 300b733..c679324 100644
--- a/log-tree.c
+++ b/log-tree.c
@@ -267,7 +267,7 @@ void show_log(struct rev_info *opt, const char *sep)
 		if (opt->reflog_info) {
 			show_reflog_message(opt->reflog_info,
 				    opt->commit_format == CMIT_FMT_ONELINE,
-				    opt->relative_date);
+				    opt->date_mode);
 			if (opt->commit_format == CMIT_FMT_ONELINE) {
 				printf("%s", sep);
 				return;
@@ -280,7 +280,7 @@ void show_log(struct rev_info *opt, const char *sep)
 	 */
 	len = pretty_print_commit(opt->commit_format, commit, ~0u, this_header,
 				  sizeof(this_header), abbrev, subject,
-				  extra_headers, opt->relative_date);
+				  extra_headers, opt->date_mode);
 
 	if (opt->add_signoff)
 		len = append_signoff(this_header, sizeof(this_header), len,
diff --git a/revision.c b/revision.c
index 49bd292..2a8e2b8 100644
--- a/revision.c
+++ b/revision.c
@@ -1111,7 +1111,11 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, const ch
 				continue;
 			}
 			if (!strcmp(arg, "--relative-date")) {
-				revs->relative_date = 1;
+				revs->date_mode = DATE_RELATIVE;
+				continue;
+			}
+			if (!strcmp(arg, "--local-zone")) {
+				revs->date_mode = DATE_LOCAL;
 				continue;
 			}
 
diff --git a/revision.h b/revision.h
index 5b41e2d..8f8720a 100644
--- a/revision.h
+++ b/revision.h
@@ -64,7 +64,7 @@ struct rev_info {
 	/* Format info */
 	unsigned int	shown_one:1,
 			abbrev_commit:1,
-			relative_date:1;
+			date_mode:2;
 
 	const char **ignore_packed; /* pretend objects in these are unpacked */
 	int num_ignore_packed;
-- 
1.5.2.rc0.719.gb3626

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

* Re: [PATCH] Add date format --local-zone
  2007-04-25  6:36 [PATCH] Add date format --local-zone Junio C Hamano
@ 2007-04-25  6:44 ` Shawn O. Pearce
  2007-04-25 12:40 ` Johannes Schindelin
  2007-04-25 15:03 ` Linus Torvalds
  2 siblings, 0 replies; 12+ messages in thread
From: Shawn O. Pearce @ 2007-04-25  6:44 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

Junio C Hamano <junkio@cox.net> wrote:
> This adds --local-zone option to log family of commands, to
> display timestamps in user's local timezone.
> 
> Signed-off-by: Junio C Hamano <junkio@cox.net>
> ---
> 
>  * I got tired of looking at +0000 timestamps on commits
>    imported from CVS at day-job.  Does not seem to break
>    existing tests, but I am not sure if this is useful.

Not only is it useful, I'd like to see a way to make it my default!
(By my default here I mean a .gitconfig thing that I can set,
but not making it the git out-of-the-box default.)

I don't like looking at timestamps in other people's timezones.
If git-log is going to go through the motions of performing unicode
translations for my tty, and indent messages, and convert the epoch
seconds to a human readable date, it most certainly can be nice and
put the times into my default frame of reference.  Which is whatever
my congresscritters have decided the time is this month...  ;-)

-- 
Shawn.

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

* Re: [PATCH] Add date format --local-zone
  2007-04-25  6:36 [PATCH] Add date format --local-zone Junio C Hamano
  2007-04-25  6:44 ` Shawn O. Pearce
@ 2007-04-25 12:40 ` Johannes Schindelin
  2007-04-25 15:03 ` Linus Torvalds
  2 siblings, 0 replies; 12+ messages in thread
From: Johannes Schindelin @ 2007-04-25 12:40 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

Hi,

On Tue, 24 Apr 2007, Junio C Hamano wrote:

> --- a/revision.c
> +++ b/revision.c
> @@ -1111,7 +1111,11 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, const ch
>  				continue;
>  			}
>  			if (!strcmp(arg, "--relative-date")) {
> -				revs->relative_date = 1;
> +				revs->date_mode = DATE_RELATIVE;
> +				continue;
> +			}
> +			if (!strcmp(arg, "--local-zone")) {
> +				revs->date_mode = DATE_LOCAL;
>  				continue;
>  			}
>  

I think that it will become more convenient at some stage to add 
"--date=local", and have a common option parser in date.c.

> diff --git a/revision.h b/revision.h
> index 5b41e2d..8f8720a 100644
> --- a/revision.h
> +++ b/revision.h
> @@ -64,7 +64,7 @@ struct rev_info {
>  	/* Format info */
>  	unsigned int	shown_one:1,
>  			abbrev_commit:1,
> -			relative_date:1;
> +			date_mode:2;

Why not go the full nine yards, and make this a full int? It's not like 
rev_info is allocated a million times...

Ciao,
Dscho

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

* Re: [PATCH] Add date format --local-zone
  2007-04-25  6:36 [PATCH] Add date format --local-zone Junio C Hamano
  2007-04-25  6:44 ` Shawn O. Pearce
  2007-04-25 12:40 ` Johannes Schindelin
@ 2007-04-25 15:03 ` Linus Torvalds
  2007-04-25 15:22   ` Linus Torvalds
  2007-04-26  4:56   ` [PATCH 1/2] Add --date={local,relative,default} Junio C Hamano
  2 siblings, 2 replies; 12+ messages in thread
From: Linus Torvalds @ 2007-04-25 15:03 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git



On Tue, 24 Apr 2007, Junio C Hamano wrote:
> 
>  * I got tired of looking at +0000 timestamps on commits
>    imported from CVS at day-job.  Does not seem to break
>    existing tests, but I am not sure if this is useful.

I think it's useful (read: I've occasionally wanted it), but I think we 
should make the date option syntax be more extensible.

Ie, it would be better, I think, to use a

	--date=local
	--date=relative
	--date=UTC
	...

kind of syntax, than have each date flag be different ("--relative-date" 
vs "--local-zone"?).

			Linus

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

* Re: [PATCH] Add date format --local-zone
  2007-04-25 15:03 ` Linus Torvalds
@ 2007-04-25 15:22   ` Linus Torvalds
  2007-04-25 19:59     ` Junio C Hamano
  2007-04-25 20:12     ` Alex Riesen
  2007-04-26  4:56   ` [PATCH 1/2] Add --date={local,relative,default} Junio C Hamano
  1 sibling, 2 replies; 12+ messages in thread
From: Linus Torvalds @ 2007-04-25 15:22 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git



On Wed, 25 Apr 2007, Linus Torvalds wrote:
> 
> Ie, it would be better, I think, to use a
> 
> 	--date=local
> 	--date=relative
> 	--date=UTC
> 	...
> 
> kind of syntax, than have each date flag be different ("--relative-date" 
> vs "--local-zone"?).

Btw, when you do "--local-zone", you should drop the TZ printout too, I 
think. It doesn't seem to make any sense to give the *wrong* timezone.

			Linus

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

* Re: [PATCH] Add date format --local-zone
  2007-04-25 15:22   ` Linus Torvalds
@ 2007-04-25 19:59     ` Junio C Hamano
  2007-04-25 20:12     ` Alex Riesen
  1 sibling, 0 replies; 12+ messages in thread
From: Junio C Hamano @ 2007-04-25 19:59 UTC (permalink / raw)
  To: Linus Torvalds; +Cc: git

Linus Torvalds <torvalds@linux-foundation.org> writes:

> On Wed, 25 Apr 2007, Linus Torvalds wrote:
>> 
>> Ie, it would be better, I think, to use a
>> 
>> 	--date=local
>> 	--date=relative
>> 	--date=UTC
>> 	...
>> 
>> kind of syntax, than have each date flag be different ("--relative-date" 
>> vs "--local-zone"?).

Probably.

> Btw, when you do "--local-zone", you should drop the TZ printout too, I 
> think. It doesn't seem to make any sense to give the *wrong* timezone.

I do not think the definition of "wrong" is so clear.

I would not disagree that showing the authordate in timezone the
author was not in means --local-zone is showing a wrong zone,
but "show timestamps in _my_ zone" is what the option is
specifically asking for.

Originally I did it without timezone, but later I found it
amusing to see "log --since=2007-03-10 --until=2007-03-12".

	Note: in TZ=US/Pacific you would see -0700 and -0800
	during the above commit-time range.

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

* Re: [PATCH] Add date format --local-zone
  2007-04-25 15:22   ` Linus Torvalds
  2007-04-25 19:59     ` Junio C Hamano
@ 2007-04-25 20:12     ` Alex Riesen
  2007-04-25 20:30       ` Junio C Hamano
  1 sibling, 1 reply; 12+ messages in thread
From: Alex Riesen @ 2007-04-25 20:12 UTC (permalink / raw)
  To: Linus Torvalds; +Cc: Junio C Hamano, git

Linus Torvalds, Wed, Apr 25, 2007 17:22:20 +0200:
> 
> Btw, when you do "--local-zone", you should drop the TZ printout too, I 
> think. It doesn't seem to make any sense to give the *wrong* timezone.
> 

The timezone is interesting to see: it is a hint as to _where_ the
commit is coming from. Obviously not reliable, it can be amusing.

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

* Re: [PATCH] Add date format --local-zone
  2007-04-25 20:12     ` Alex Riesen
@ 2007-04-25 20:30       ` Junio C Hamano
  2007-04-25 20:41         ` Alex Riesen
  0 siblings, 1 reply; 12+ messages in thread
From: Junio C Hamano @ 2007-04-25 20:30 UTC (permalink / raw)
  To: Alex Riesen; +Cc: Linus Torvalds, git

Alex Riesen <raa.lkml@gmail.com> writes:

> Linus Torvalds, Wed, Apr 25, 2007 17:22:20 +0200:
>> 
>> Btw, when you do "--local-zone", you should drop the TZ printout too, I 
>> think. It doesn't seem to make any sense to give the *wrong* timezone.
>
> The timezone is interesting to see: it is a hint as to _where_ the
> commit is coming from. Obviously not reliable, it can be amusing.

Unless you spotted a bug in the patch, with --local-zone, I do
not think you would get any such hint from the zone information.

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

* Re: [PATCH] Add date format --local-zone
  2007-04-25 20:30       ` Junio C Hamano
@ 2007-04-25 20:41         ` Alex Riesen
  2007-04-25 21:02           ` Junio C Hamano
  0 siblings, 1 reply; 12+ messages in thread
From: Alex Riesen @ 2007-04-25 20:41 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Linus Torvalds, git

Junio C Hamano, Wed, Apr 25, 2007 22:30:46 +0200:
> >> 
> >> Btw, when you do "--local-zone", you should drop the TZ printout too, I 
> >> think. It doesn't seem to make any sense to give the *wrong* timezone.
> >
> > The timezone is interesting to see: it is a hint as to _where_ the
> > commit is coming from. Obviously not reliable, it can be amusing.
> 
> Unless you spotted a bug in the patch, with --local-zone, I do
> not think you would get any such hint from the zone information.
> 

No, not in your patch. For example, look at the timezone information
in the Date: and Received: headers of this mail.

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

* Re: [PATCH] Add date format --local-zone
  2007-04-25 20:41         ` Alex Riesen
@ 2007-04-25 21:02           ` Junio C Hamano
  0 siblings, 0 replies; 12+ messages in thread
From: Junio C Hamano @ 2007-04-25 21:02 UTC (permalink / raw)
  To: Alex Riesen; +Cc: Linus Torvalds, git

Alex Riesen <raa.lkml@gmail.com> writes:

> Junio C Hamano, Wed, Apr 25, 2007 22:30:46 +0200:
>> >> 
>> >> Btw, when you do "--local-zone", you should drop the TZ printout too, I 
>> >> think. It doesn't seem to make any sense to give the *wrong* timezone.
>> >
>> > The timezone is interesting to see: it is a hint as to _where_ the
>> > commit is coming from. Obviously not reliable, it can be amusing.
>> 
>> Unless you spotted a bug in the patch, with --local-zone, I do
>> not think you would get any such hint from the zone information.
>> 
>
> No, not in your patch. For example, look at the timezone information
> in the Date: and Received: headers of this mail.

Ok, I misunderstood.  I did not realize that you were making a
comment that was totally unrelated to the --local-zone option.

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

* [PATCH 1/2] Add --date={local,relative,default}
  2007-04-25 15:03 ` Linus Torvalds
  2007-04-25 15:22   ` Linus Torvalds
@ 2007-04-26  4:56   ` Junio C Hamano
  2007-04-26  6:54     ` David Kågedal
  1 sibling, 1 reply; 12+ messages in thread
From: Junio C Hamano @ 2007-04-26  4:56 UTC (permalink / raw)
  To: git; +Cc: Linus Torvalds

This adds --date={local,relative,default} option to log family of commands,
to allow displaying timestamps in user's local timezone, relative time, or
the default format.

Existing --relative-date option is a synonym of --date=relative; we could
probably deprecate it in the long run.

Signed-off-by: Junio C Hamano <junkio@cox.net>
---

  >>   I think it's useful (read: I've occasionally wanted it), but I think we 
  >>   should make the date option syntax be more extensible.
  >> 
  >>   Ie, it would be better, I think, to use a
  >> 
  >> 	  --date=local
  >> 	  --date=relative
  >> 	  --date=UTC
  >> 	  ...
  >> 
  >>   kind of syntax, than have each date flag be different ("--relative-date" 

  And I obviously agree.

 Documentation/git-rev-list.txt |   14 +++++++++++++-
 builtin-rev-list.c             |    2 +-
 cache.h                        |    2 +-
 commit.c                       |   12 ++++++------
 commit.h                       |    2 +-
 date.c                         |   35 +++++++++++++++++++++++++++++++++--
 log-tree.c                     |    4 ++--
 revision.c                     |   13 ++++++++++++-
 revision.h                     |    4 ++--
 9 files changed, 71 insertions(+), 17 deletions(-)

diff --git a/Documentation/git-rev-list.txt b/Documentation/git-rev-list.txt
index 77e068b..1b12b4f 100644
--- a/Documentation/git-rev-list.txt
+++ b/Documentation/git-rev-list.txt
@@ -25,6 +25,7 @@ SYNOPSIS
 	     [ \--cherry-pick ]
 	     [ \--encoding[=<encoding>] ]
 	     [ \--(author|committer|grep)=<pattern> ]
+	     [ \--date={local|relative|default} ]
 	     [ [\--objects | \--objects-edge] [ \--unpacked ] ]
 	     [ \--pretty | \--header ]
 	     [ \--bisect ]
@@ -90,9 +91,20 @@ include::pretty-formats.txt[]
 
 --relative-date::
 
-	Show dates relative to the current time, e.g. "2 hours ago".
+	Synonym for `--date=relative`.
+
+--date={relative,local,default}::
+
 	Only takes effect for dates shown in human-readable format, such
 	as when using "--pretty".
++
+`--date=relative` shows dates relative to the current time,
+e.g. "2 hours ago".
++
+`--date=local` shows timestamps in user's local timezone.
++
+`--date=default` shows timestamps in the original timezone
+(either committer's or author's).
 
 --header::
 
diff --git a/builtin-rev-list.c b/builtin-rev-list.c
index c0329dc..ebf53f5 100644
--- a/builtin-rev-list.c
+++ b/builtin-rev-list.c
@@ -95,7 +95,7 @@ static void show_commit(struct commit *commit)
 		static char pretty_header[16384];
 		pretty_print_commit(revs.commit_format, commit, ~0,
 				    pretty_header, sizeof(pretty_header),
-				    revs.abbrev, NULL, NULL, revs.relative_date);
+				    revs.abbrev, NULL, NULL, revs.date_mode);
 		printf("%s%c", pretty_header, hdr_termination);
 	}
 	fflush(stdout);
diff --git a/cache.h b/cache.h
index 38a0cbd..8e76152 100644
--- a/cache.h
+++ b/cache.h
@@ -389,7 +389,7 @@ extern void *read_object_with_reference(const unsigned char *sha1,
 					unsigned long *size,
 					unsigned char *sha1_ret);
 
-enum date_mode { DATE_NORMAL = 0, DATE_RELATIVE, DATE_SHORT };
+enum date_mode { DATE_NORMAL = 0, DATE_RELATIVE, DATE_SHORT, DATE_LOCAL };
 const char *show_date(unsigned long time, int timezone, enum date_mode mode);
 const char *show_rfc2822_date(unsigned long time, int timezone);
 int parse_date(const char *date, char *buf, int bufsize);
diff --git a/commit.c b/commit.c
index 10466c4..f1ba972 100644
--- a/commit.c
+++ b/commit.c
@@ -526,7 +526,7 @@ static int add_rfc2047(char *buf, const char *line, int len,
 }
 
 static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf,
-			 const char *line, int relative_date,
+			 const char *line, enum date_mode dmode,
 			 const char *encoding)
 {
 	char *date;
@@ -569,7 +569,7 @@ static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf,
 	switch (fmt) {
 	case CMIT_FMT_MEDIUM:
 		ret += sprintf(buf + ret, "Date:   %s\n",
-			       show_date(time, tz, relative_date));
+			       show_date(time, tz, dmode));
 		break;
 	case CMIT_FMT_EMAIL:
 		ret += sprintf(buf + ret, "Date: %s\n",
@@ -577,7 +577,7 @@ static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf,
 		break;
 	case CMIT_FMT_FULLER:
 		ret += sprintf(buf + ret, "%sDate: %s\n", what,
-			       show_date(time, tz, relative_date));
+			       show_date(time, tz, dmode));
 		break;
 	default:
 		/* notin' */
@@ -919,7 +919,7 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt,
 				  char *buf, unsigned long space,
 				  int abbrev, const char *subject,
 				  const char *after_subject,
-				  int relative_date)
+				  enum date_mode dmode)
 {
 	int hdr = 1, body = 0, seen_title = 0;
 	unsigned long offset = 0;
@@ -1023,14 +1023,14 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt,
 				offset += add_user_info("Author", fmt,
 							buf + offset,
 							line + 7,
-							relative_date,
+							dmode,
 							encoding);
 			if (!memcmp(line, "committer ", 10) &&
 			    (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER))
 				offset += add_user_info("Commit", fmt,
 							buf + offset,
 							line + 10,
-							relative_date,
+							dmode,
 							encoding);
 			continue;
 		}
diff --git a/commit.h b/commit.h
index 59de17e..86e8dca 100644
--- a/commit.h
+++ b/commit.h
@@ -61,7 +61,7 @@ enum cmit_fmt {
 };
 
 extern enum cmit_fmt get_commit_format(const char *arg);
-extern unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *, unsigned long len, char *buf, unsigned long space, int abbrev, const char *subject, const char *after_subject, int relative_date);
+extern unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *, unsigned long len, char *buf, unsigned long space, int abbrev, const char *subject, const char *after_subject, enum date_mode dmode);
 
 /** Removes the first commit from a list sorted by date, and adds all
  * of its parents.
diff --git a/date.c b/date.c
index 0ceccbe..a9b59a2 100644
--- a/date.c
+++ b/date.c
@@ -55,6 +55,32 @@ static struct tm *time_to_tm(unsigned long time, int tz)
 	return gmtime(&t);
 }
 
+/*
+ * What value of "tz" was in effect back then at "time" in the
+ * local timezone?
+ */
+static int local_tzoffset(unsigned long time)
+{
+	time_t t, t_local;
+	struct tm tm;
+	int offset, eastwest;
+
+	t = time;
+	localtime_r(&t, &tm);
+	t_local = my_mktime(&tm);
+
+	if (t_local < t) {
+		eastwest = -1;
+		offset = t - t_local;
+	} else {
+		eastwest = 1;
+		offset = t_local - t;
+	}
+	offset /= 60; /* in minutes */
+	offset = (offset % 60) + ((offset / 60) * 100);
+	return offset * eastwest;
+}
+
 const char *show_date(unsigned long time, int tz, enum date_mode mode)
 {
 	struct tm *tm;
@@ -102,6 +128,9 @@ const char *show_date(unsigned long time, int tz, enum date_mode mode)
 		/* Else fall back on absolute format.. */
 	}
 
+	if (mode == DATE_LOCAL)
+		tz = local_tzoffset(time);
+
 	tm = time_to_tm(time, tz);
 	if (!tm)
 		return NULL;
@@ -109,12 +138,14 @@ const char *show_date(unsigned long time, int tz, enum date_mode mode)
 		sprintf(timebuf, "%04d-%02d-%02d", tm->tm_year + 1900,
 				tm->tm_mon + 1, tm->tm_mday);
 	else
-		sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d %+05d",
+		sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
 				weekday_names[tm->tm_wday],
 				month_names[tm->tm_mon],
 				tm->tm_mday,
 				tm->tm_hour, tm->tm_min, tm->tm_sec,
-				tm->tm_year + 1900, tz);
+				tm->tm_year + 1900,
+				(mode == DATE_LOCAL) ? 0 : ' ',
+				tz);
 	return timebuf;
 }
 
diff --git a/log-tree.c b/log-tree.c
index 300b733..c679324 100644
--- a/log-tree.c
+++ b/log-tree.c
@@ -267,7 +267,7 @@ void show_log(struct rev_info *opt, const char *sep)
 		if (opt->reflog_info) {
 			show_reflog_message(opt->reflog_info,
 				    opt->commit_format == CMIT_FMT_ONELINE,
-				    opt->relative_date);
+				    opt->date_mode);
 			if (opt->commit_format == CMIT_FMT_ONELINE) {
 				printf("%s", sep);
 				return;
@@ -280,7 +280,7 @@ void show_log(struct rev_info *opt, const char *sep)
 	 */
 	len = pretty_print_commit(opt->commit_format, commit, ~0u, this_header,
 				  sizeof(this_header), abbrev, subject,
-				  extra_headers, opt->relative_date);
+				  extra_headers, opt->date_mode);
 
 	if (opt->add_signoff)
 		len = append_signoff(this_header, sizeof(this_header), len,
diff --git a/revision.c b/revision.c
index 49bd292..e60a26c 100644
--- a/revision.c
+++ b/revision.c
@@ -1111,7 +1111,18 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, const ch
 				continue;
 			}
 			if (!strcmp(arg, "--relative-date")) {
-				revs->relative_date = 1;
+				revs->date_mode = DATE_RELATIVE;
+				continue;
+			}
+			if (!strncmp(arg, "--date=", 7)) {
+				if (!strcmp(arg + 7, "relative"))
+					revs->date_mode = DATE_RELATIVE;
+				else if (!strcmp(arg + 7, "local"))
+					revs->date_mode = DATE_LOCAL;
+				else if (!strcmp(arg + 7, "default"))
+					revs->date_mode = DATE_NORMAL;
+				else
+					die("unknown date format %s", arg);
 				continue;
 			}
 
diff --git a/revision.h b/revision.h
index 5b41e2d..cdf94ad 100644
--- a/revision.h
+++ b/revision.h
@@ -63,8 +63,8 @@ struct rev_info {
 
 	/* Format info */
 	unsigned int	shown_one:1,
-			abbrev_commit:1,
-			relative_date:1;
+			abbrev_commit:1;
+	enum date_mode date_mode;
 
 	const char **ignore_packed; /* pretend objects in these are unpacked */
 	int num_ignore_packed;
-- 
1.5.2.rc0.719.gb3626

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

* Re: [PATCH 1/2] Add --date={local,relative,default}
  2007-04-26  4:56   ` [PATCH 1/2] Add --date={local,relative,default} Junio C Hamano
@ 2007-04-26  6:54     ` David Kågedal
  0 siblings, 0 replies; 12+ messages in thread
From: David Kågedal @ 2007-04-26  6:54 UTC (permalink / raw)
  To: git

Junio C Hamano <junkio@cox.net> writes:

> ++
> +`--date=relative` shows dates relative to the current time,
> +e.g. "2 hours ago".
> ++
> +`--date=local` shows timestamps in user's local timezone.
> ++
> +`--date=default` shows timestamps in the original timezone
> +(either committer's or author's).

Why is "default" called "default" and not, say, "original"?  What if I
changed the default format to "local" using local configuration.  What
would "default" show then?

-- 
David Kågedal

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

end of thread, other threads:[~2007-04-26  9:05 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-04-25  6:36 [PATCH] Add date format --local-zone Junio C Hamano
2007-04-25  6:44 ` Shawn O. Pearce
2007-04-25 12:40 ` Johannes Schindelin
2007-04-25 15:03 ` Linus Torvalds
2007-04-25 15:22   ` Linus Torvalds
2007-04-25 19:59     ` Junio C Hamano
2007-04-25 20:12     ` Alex Riesen
2007-04-25 20:30       ` Junio C Hamano
2007-04-25 20:41         ` Alex Riesen
2007-04-25 21:02           ` Junio C Hamano
2007-04-26  4:56   ` [PATCH 1/2] Add --date={local,relative,default} Junio C Hamano
2007-04-26  6:54     ` David Kågedal

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).