@matheusbsbs / matheus-bagliano / commits / a7b02ccf9a
Fork 0

Add --date={local,relative,default}

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>

Junio C Hamano committed Apr 24, 2007 at 23:36 UTC a7b02ccf9a682fa0c2b28df6ca20f9199cdca4de
9 files changed +71 -17
Documentation/git-rev-list.txt
+13 -1
@@ -25,6 +25,7 @@ SYNOPSIS
25 [ \--cherry-pick ]
26 [ \--encoding[=<encoding>] ]
27 [ \--(author|committer|grep)=<pattern> ]
28 + [ \--date={local|relative|default} ]
29 [ [\--objects | \--objects-edge] [ \--unpacked ] ]
30 [ \--pretty | \--header ]
31 [ \--bisect ]
@@ -90,9 +91,20 @@ include::pretty-formats.txt[]
91
92 --relative-date::
93
93 - Show dates relative to the current time, e.g. "2 hours ago".
94 + Synonym for `--date=relative`.
95 +
96 +--date={relative,local,default}::
97 +
98 Only takes effect for dates shown in human-readable format, such
99 as when using "--pretty".
100 ++
101 +`--date=relative` shows dates relative to the current time,
102 +e.g. "2 hours ago".
103 ++
104 +`--date=local` shows timestamps in user's local timezone.
105 ++
106 +`--date=default` shows timestamps in the original timezone
107 +(either committer's or author's).
108
109 --header::
110
builtin-rev-list.c
+1 -1
@@ -95,7 +95,7 @@ static void show_commit(struct commit *commit)
95 static char pretty_header[16384];
96 pretty_print_commit(revs.commit_format, commit, ~0,
97 pretty_header, sizeof(pretty_header),
98 - revs.abbrev, NULL, NULL, revs.relative_date);
98 + revs.abbrev, NULL, NULL, revs.date_mode);
99 printf("%s%c", pretty_header, hdr_termination);
100 }
101 fflush(stdout);
cache.h
+1 -1
@@ -389,7 +389,7 @@ extern void *read_object_with_reference(const unsigned char *sha1,
389 unsigned long *size,
390 unsigned char *sha1_ret);
391
392 -enum date_mode { DATE_NORMAL = 0, DATE_RELATIVE, DATE_SHORT };
392 +enum date_mode { DATE_NORMAL = 0, DATE_RELATIVE, DATE_SHORT, DATE_LOCAL };
393 const char *show_date(unsigned long time, int timezone, enum date_mode mode);
394 const char *show_rfc2822_date(unsigned long time, int timezone);
395 int parse_date(const char *date, char *buf, int bufsize);
commit.c
+6 -6
@@ -526,7 +526,7 @@ static int add_rfc2047(char *buf, const char *line, int len,
526 }
527
528 static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf,
529 - const char *line, int relative_date,
529 + const char *line, enum date_mode dmode,
530 const char *encoding)
531 {
532 char *date;
@@ -569,7 +569,7 @@ static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf,
569 switch (fmt) {
570 case CMIT_FMT_MEDIUM:
571 ret += sprintf(buf + ret, "Date: %s\n",
572 - show_date(time, tz, relative_date));
572 + show_date(time, tz, dmode));
573 break;
574 case CMIT_FMT_EMAIL:
575 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,
577 break;
578 case CMIT_FMT_FULLER:
579 ret += sprintf(buf + ret, "%sDate: %s\n", what,
580 - show_date(time, tz, relative_date));
580 + show_date(time, tz, dmode));
581 break;
582 default:
583 /* notin' */
@@ -919,7 +919,7 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt,
919 char *buf, unsigned long space,
920 int abbrev, const char *subject,
921 const char *after_subject,
922 - int relative_date)
922 + enum date_mode dmode)
923 {
924 int hdr = 1, body = 0, seen_title = 0;
925 unsigned long offset = 0;
@@ -1023,14 +1023,14 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt,
1023 offset += add_user_info("Author", fmt,
1024 buf + offset,
1025 line + 7,
1026 - relative_date,
1026 + dmode,
1027 encoding);
1028 if (!memcmp(line, "committer ", 10) &&
1029 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER))
1030 offset += add_user_info("Commit", fmt,
1031 buf + offset,
1032 line + 10,
1033 - relative_date,
1033 + dmode,
1034 encoding);
1035 continue;
1036 }
commit.h
+1 -1
@@ -61,7 +61,7 @@ enum cmit_fmt {
61 };
62
63 extern enum cmit_fmt get_commit_format(const char *arg);
64 -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);
64 +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);
65
66 /** Removes the first commit from a list sorted by date, and adds all
67 * of its parents.
date.c
+33 -2
@@ -55,6 +55,32 @@ static struct tm *time_to_tm(unsigned long time, int tz)
55 return gmtime(&t);
56 }
57
58 +/*
59 + * What value of "tz" was in effect back then at "time" in the
60 + * local timezone?
61 + */
62 +static int local_tzoffset(unsigned long time)
63 +{
64 + time_t t, t_local;
65 + struct tm tm;
66 + int offset, eastwest;
67 +
68 + t = time;
69 + localtime_r(&t, &tm);
70 + t_local = my_mktime(&tm);
71 +
72 + if (t_local < t) {
73 + eastwest = -1;
74 + offset = t - t_local;
75 + } else {
76 + eastwest = 1;
77 + offset = t_local - t;
78 + }
79 + offset /= 60; /* in minutes */
80 + offset = (offset % 60) + ((offset / 60) * 100);
81 + return offset * eastwest;
82 +}
83 +
84 const char *show_date(unsigned long time, int tz, enum date_mode mode)
85 {
86 struct tm *tm;
@@ -102,6 +128,9 @@ const char *show_date(unsigned long time, int tz, enum date_mode mode)
128 /* Else fall back on absolute format.. */
129 }
130
131 + if (mode == DATE_LOCAL)
132 + tz = local_tzoffset(time);
133 +
134 tm = time_to_tm(time, tz);
135 if (!tm)
136 return NULL;
@@ -109,12 +138,14 @@ const char *show_date(unsigned long time, int tz, enum date_mode mode)
138 sprintf(timebuf, "%04d-%02d-%02d", tm->tm_year + 1900,
139 tm->tm_mon + 1, tm->tm_mday);
140 else
112 - sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d %+05d",
141 + sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
142 weekday_names[tm->tm_wday],
143 month_names[tm->tm_mon],
144 tm->tm_mday,
145 tm->tm_hour, tm->tm_min, tm->tm_sec,
117 - tm->tm_year + 1900, tz);
146 + tm->tm_year + 1900,
147 + (mode == DATE_LOCAL) ? 0 : ' ',
148 + tz);
149 return timebuf;
150 }
151
log-tree.c
+2 -2
@@ -267,7 +267,7 @@ void show_log(struct rev_info *opt, const char *sep)
267 if (opt->reflog_info) {
268 show_reflog_message(opt->reflog_info,
269 opt->commit_format == CMIT_FMT_ONELINE,
270 - opt->relative_date);
270 + opt->date_mode);
271 if (opt->commit_format == CMIT_FMT_ONELINE) {
272 printf("%s", sep);
273 return;
@@ -280,7 +280,7 @@ void show_log(struct rev_info *opt, const char *sep)
280 */
281 len = pretty_print_commit(opt->commit_format, commit, ~0u, this_header,
282 sizeof(this_header), abbrev, subject,
283 - extra_headers, opt->relative_date);
283 + extra_headers, opt->date_mode);
284
285 if (opt->add_signoff)
286 len = append_signoff(this_header, sizeof(this_header), len,
revision.c
+12 -1
@@ -1111,7 +1111,18 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, const ch
1111 continue;
1112 }
1113 if (!strcmp(arg, "--relative-date")) {
1114 - revs->relative_date = 1;
1114 + revs->date_mode = DATE_RELATIVE;
1115 + continue;
1116 + }
1117 + if (!strncmp(arg, "--date=", 7)) {
1118 + if (!strcmp(arg + 7, "relative"))
1119 + revs->date_mode = DATE_RELATIVE;
1120 + else if (!strcmp(arg + 7, "local"))
1121 + revs->date_mode = DATE_LOCAL;
1122 + else if (!strcmp(arg + 7, "default"))
1123 + revs->date_mode = DATE_NORMAL;
1124 + else
1125 + die("unknown date format %s", arg);
1126 continue;
1127 }
1128
revision.h
+2 -2
@@ -63,8 +63,8 @@ struct rev_info {
63
64 /* Format info */
65 unsigned int shown_one:1,
66 - abbrev_commit:1,
67 - relative_date:1;
66 + abbrev_commit:1;
67 + enum date_mode date_mode;
68
69 const char **ignore_packed; /* pretend objects in these are unpacked */
70 int num_ignore_packed;