summaryrefslogtreecommitdiff
path: root/date.c
diff options
context:
space:
mode:
authorLibravatar Jeff King <peff@peff.net>2015-09-03 22:48:59 +0100
committerLibravatar Junio C Hamano <gitster@pobox.com>2015-09-03 15:45:26 -0700
commitadd00ba2de971e0c5ba00f1f02b73c5534079d2c (patch)
tree40a05fca8f59a33785ab7156f428f183e7a8ab10 /date.c
parentdate: check for "local" before anything else (diff)
downloadtgif-add00ba2de971e0c5ba00f1f02b73c5534079d2c.tar.xz
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 <peff@peff.net> Signed-off-by: John Keeping <john@keeping.me.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'date.c')
-rw-r--r--date.c70
1 files changed, 45 insertions, 25 deletions
diff --git a/date.c b/date.c
index 9f0a5ddf6c..7c9f76998a 100644
--- a/date.c
+++ b/date.c
@@ -166,6 +166,7 @@ struct date_mode *date_mode_from_type(enum date_mode_type type)
if (type == DATE_STRFTIME)
die("BUG: cannot create anonymous strftime date_mode struct");
mode.type = type;
+ mode.local = 0;
return &mode;
}
@@ -174,7 +175,7 @@ const char *show_date(unsigned long time, int tz, const struct date_mode *mode)
struct tm *tm;
static struct strbuf timebuf = STRBUF_INIT;
- if (mode->type == DATE_LOCAL)
+ if (mode->local)
tz = local_tzoffset(time);
if (mode->type == DATE_RAW) {
@@ -232,7 +233,7 @@ const char *show_date(unsigned long time, int tz, const struct date_mode *mode)
tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec,
tm->tm_year + 1900,
- (mode->type == DATE_LOCAL) ? 0 : ' ',
+ mode->local ? 0 : ' ',
tz);
return timebuf.buf;
}
@@ -770,31 +771,50 @@ int parse_date(const char *date, struct strbuf *result)
return 0;
}
+static enum date_mode_type parse_date_type(const char *format, const char **end)
+{
+ if (skip_prefix(format, "relative", end))
+ return DATE_RELATIVE;
+ if (skip_prefix(format, "iso8601-strict", end) ||
+ skip_prefix(format, "iso-strict", end))
+ return DATE_ISO8601_STRICT;
+ if (skip_prefix(format, "iso8601", end) ||
+ skip_prefix(format, "iso", end))
+ return DATE_ISO8601;
+ if (skip_prefix(format, "rfc2822", end) ||
+ skip_prefix(format, "rfc", end))
+ return DATE_RFC2822;
+ if (skip_prefix(format, "short", end))
+ return DATE_SHORT;
+ if (skip_prefix(format, "default", end))
+ return DATE_NORMAL;
+ if (skip_prefix(format, "raw", end))
+ return DATE_RAW;
+ if (skip_prefix(format, "format", end))
+ return DATE_STRFTIME;
+
+ die("unknown date format %s", format);
+}
+
void parse_date_format(const char *format, struct date_mode *mode)
{
- if (!strcmp(format, "relative"))
- mode->type = DATE_RELATIVE;
- else if (!strcmp(format, "iso8601") ||
- !strcmp(format, "iso"))
- mode->type = DATE_ISO8601;
- else if (!strcmp(format, "iso8601-strict") ||
- !strcmp(format, "iso-strict"))
- mode->type = DATE_ISO8601_STRICT;
- else if (!strcmp(format, "rfc2822") ||
- !strcmp(format, "rfc"))
- mode->type = DATE_RFC2822;
- else if (!strcmp(format, "short"))
- mode->type = DATE_SHORT;
- else if (!strcmp(format, "local"))
- mode->type = DATE_LOCAL;
- else if (!strcmp(format, "default"))
- mode->type = DATE_NORMAL;
- else if (!strcmp(format, "raw"))
- mode->type = DATE_RAW;
- else if (skip_prefix(format, "format:", &format)) {
- mode->type = DATE_STRFTIME;
- mode->strftime_fmt = xstrdup(format);
- } else
+ const char *p;
+
+ /* historical alias */
+ if (!strcmp(format, "local"))
+ format = "default-local";
+
+ mode->type = parse_date_type(format, &p);
+ mode->local = 0;
+
+ if (skip_prefix(p, "-local", &p))
+ mode->local = 1;
+
+ if (mode->type == DATE_STRFTIME) {
+ if (!skip_prefix(p, ":", &p))
+ die("date format missing colon separator: %s", format);
+ mode->strftime_fmt = xstrdup(p);
+ } else if (*p)
die("unknown date format %s", format);
}