Skip to content

Commit add00ba

Browse files
peffgitster
authored andcommitted
date: make "local" orthogonal to date format
Most of our "--date" modes are about the format of the date: which items we show and in what order. But "--date=local" is a bit of an oddball. It means "show the date in the normal format, but using the local timezone". The timezone we use is orthogonal to the actual format, and there is no reason we could not have "localized iso8601", etc. This patch adds a "local" boolean field to "struct date_mode", and drops the DATE_LOCAL element from the date_mode_type enum (it's now just DATE_NORMAL plus local=1). The new feature is accessible to users by adding "-local" to any date mode (e.g., "iso-local"), and we retain "local" as an alias for "default-local" for backwards compatibility. Signed-off-by: Jeff King <[email protected]> Signed-off-by: John Keeping <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dc6d782 commit add00ba

File tree

4 files changed

+61
-33
lines changed

4 files changed

+61
-33
lines changed

Documentation/rev-list-options.txt

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -702,12 +702,16 @@ include::pretty-options.txt[]
702702
--date=<format>::
703703
Only takes effect for dates shown in human-readable format, such
704704
as when using `--pretty`. `log.date` config variable sets a default
705-
value for the log command's `--date` option.
705+
value for the log command's `--date` option. By default, dates
706+
are shown in the original time zone (either committer's or
707+
author's). If `-local` is appended to the format (e.g.,
708+
`iso-local`), the user's local time zone is used instead.
706709
+
707710
`--date=relative` shows dates relative to the current time,
708-
e.g. ``2 hours ago''.
711+
e.g. ``2 hours ago''. The `-local` option cannot be used with
712+
`--raw` or `--relative`.
709713
+
710-
`--date=local` shows timestamps in user's local time zone.
714+
`--date=local` is an alias for `--date=default-local`.
711715
+
712716
`--date=iso` (or `--date=iso8601`) shows timestamps in a ISO 8601-like format.
713717
The differences to the strict ISO 8601 format are:
@@ -730,10 +734,15 @@ format, often found in email messages.
730734
`--date=format:...` feeds the format `...` to your system `strftime`.
731735
Use `--date=format:%c` to show the date in your system locale's
732736
preferred format. See the `strftime` manual for a complete list of
733-
format placeholders.
737+
format placeholders. When using `-local`, the correct syntax is
738+
`--date=format-local:...`.
734739
+
735-
`--date=default` shows timestamps in the original time zone
736-
(either committer's or author's).
740+
`--date=default` is the default format, and is similar to
741+
`--date=rfc2822`, with a few exceptions:
742+
743+
- there is no comma after the day-of-week
744+
745+
- the time zone is omitted when the local time zone is used
737746

738747
ifdef::git-rev-list[]
739748
--header::

builtin/blame.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2600,7 +2600,6 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
26002600
fewer display columns. */
26012601
blame_date_width = utf8_strwidth(_("4 years, 11 months ago")) + 1; /* add the null */
26022602
break;
2603-
case DATE_LOCAL:
26042603
case DATE_NORMAL:
26052604
blame_date_width = sizeof("Thu Oct 19 16:00:04 2006 -0700");
26062605
break;

cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1110,14 +1110,14 @@ struct date_mode {
11101110
DATE_NORMAL = 0,
11111111
DATE_RELATIVE,
11121112
DATE_SHORT,
1113-
DATE_LOCAL,
11141113
DATE_ISO8601,
11151114
DATE_ISO8601_STRICT,
11161115
DATE_RFC2822,
11171116
DATE_STRFTIME,
11181117
DATE_RAW
11191118
} type;
11201119
const char *strftime_fmt;
1120+
int local;
11211121
};
11221122

11231123
/*

date.c

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ struct date_mode *date_mode_from_type(enum date_mode_type type)
166166
if (type == DATE_STRFTIME)
167167
die("BUG: cannot create anonymous strftime date_mode struct");
168168
mode.type = type;
169+
mode.local = 0;
169170
return &mode;
170171
}
171172

@@ -174,7 +175,7 @@ const char *show_date(unsigned long time, int tz, const struct date_mode *mode)
174175
struct tm *tm;
175176
static struct strbuf timebuf = STRBUF_INIT;
176177

177-
if (mode->type == DATE_LOCAL)
178+
if (mode->local)
178179
tz = local_tzoffset(time);
179180

180181
if (mode->type == DATE_RAW) {
@@ -232,7 +233,7 @@ const char *show_date(unsigned long time, int tz, const struct date_mode *mode)
232233
tm->tm_mday,
233234
tm->tm_hour, tm->tm_min, tm->tm_sec,
234235
tm->tm_year + 1900,
235-
(mode->type == DATE_LOCAL) ? 0 : ' ',
236+
mode->local ? 0 : ' ',
236237
tz);
237238
return timebuf.buf;
238239
}
@@ -770,31 +771,50 @@ int parse_date(const char *date, struct strbuf *result)
770771
return 0;
771772
}
772773

774+
static enum date_mode_type parse_date_type(const char *format, const char **end)
775+
{
776+
if (skip_prefix(format, "relative", end))
777+
return DATE_RELATIVE;
778+
if (skip_prefix(format, "iso8601-strict", end) ||
779+
skip_prefix(format, "iso-strict", end))
780+
return DATE_ISO8601_STRICT;
781+
if (skip_prefix(format, "iso8601", end) ||
782+
skip_prefix(format, "iso", end))
783+
return DATE_ISO8601;
784+
if (skip_prefix(format, "rfc2822", end) ||
785+
skip_prefix(format, "rfc", end))
786+
return DATE_RFC2822;
787+
if (skip_prefix(format, "short", end))
788+
return DATE_SHORT;
789+
if (skip_prefix(format, "default", end))
790+
return DATE_NORMAL;
791+
if (skip_prefix(format, "raw", end))
792+
return DATE_RAW;
793+
if (skip_prefix(format, "format", end))
794+
return DATE_STRFTIME;
795+
796+
die("unknown date format %s", format);
797+
}
798+
773799
void parse_date_format(const char *format, struct date_mode *mode)
774800
{
775-
if (!strcmp(format, "relative"))
776-
mode->type = DATE_RELATIVE;
777-
else if (!strcmp(format, "iso8601") ||
778-
!strcmp(format, "iso"))
779-
mode->type = DATE_ISO8601;
780-
else if (!strcmp(format, "iso8601-strict") ||
781-
!strcmp(format, "iso-strict"))
782-
mode->type = DATE_ISO8601_STRICT;
783-
else if (!strcmp(format, "rfc2822") ||
784-
!strcmp(format, "rfc"))
785-
mode->type = DATE_RFC2822;
786-
else if (!strcmp(format, "short"))
787-
mode->type = DATE_SHORT;
788-
else if (!strcmp(format, "local"))
789-
mode->type = DATE_LOCAL;
790-
else if (!strcmp(format, "default"))
791-
mode->type = DATE_NORMAL;
792-
else if (!strcmp(format, "raw"))
793-
mode->type = DATE_RAW;
794-
else if (skip_prefix(format, "format:", &format)) {
795-
mode->type = DATE_STRFTIME;
796-
mode->strftime_fmt = xstrdup(format);
797-
} else
801+
const char *p;
802+
803+
/* historical alias */
804+
if (!strcmp(format, "local"))
805+
format = "default-local";
806+
807+
mode->type = parse_date_type(format, &p);
808+
mode->local = 0;
809+
810+
if (skip_prefix(p, "-local", &p))
811+
mode->local = 1;
812+
813+
if (mode->type == DATE_STRFTIME) {
814+
if (!skip_prefix(p, ":", &p))
815+
die("date format missing colon separator: %s", format);
816+
mode->strftime_fmt = xstrdup(p);
817+
} else if (*p)
798818
die("unknown date format %s", format);
799819
}
800820

0 commit comments

Comments
 (0)