date: add "unix" format
authorJeff King <peff@peff.net>
Fri, 22 Jul 2016 19:51:49 +0000 (15:51 -0400)
committerJunio C Hamano <gitster@pobox.com>
Wed, 27 Jul 2016 21:15:51 +0000 (14:15 -0700)
We already have "--date=raw", which is a Unix epoch
timestamp plus a contextual timezone (either the author's or
the local). But one may not care about the timezone and just
want the epoch timestamp by itself. It's not hard to parse
the two apart, but if you are using a pretty-print format,
you may want git to show the "finished" form that the user
will see.

We can accomodate this by adding a new date format, "unix",
which is basically "raw" without the timezone.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/rev-list-options.txt
builtin/blame.c
cache.h
date.c
t/t0006-date.sh
index 9215534afa7006273585b19eb68b4b0b698bf88e..fd86ed12dc97df66fda44bdae85f1d260294bf54 100644 (file)
@@ -751,6 +751,10 @@ Note that the `-local` option does not affect the seconds-since-epoch
 value (which is always measured in UTC), but does switch the accompanying
 timezone value.
 +
+`--date=unix` shows the date as a Unix epoch timestamp (seconds since
+1970).  As with `--raw`, this is always in UTC and therefore `-local`
+has no effect.
++
 `--date=format:...` feeds the format `...` to your system `strftime`.
 Use `--date=format:%c` to show the date in your system locale's
 preferred format.  See the `strftime` manual for a complete list of
index 7417edf6ef15bf4c0611c8e177e96d5734c98ac2..002e70f9b59b5a6d4aefc8fe30bad6360cb1b63e 100644 (file)
@@ -2626,6 +2626,9 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
        case DATE_RAW:
                blame_date_width = sizeof("1161298804 -0700");
                break;
+       case DATE_UNIX:
+               blame_date_width = sizeof("1161298804");
+               break;
        case DATE_SHORT:
                blame_date_width = sizeof("2006-10-19");
                break;
diff --git a/cache.h b/cache.h
index c73becbf2dc8ae939116617e36c76afa60857cef..ace7f70e3b6eb30740c3964c80333c2572496d6b 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -1223,7 +1223,8 @@ struct date_mode {
                DATE_ISO8601_STRICT,
                DATE_RFC2822,
                DATE_STRFTIME,
-               DATE_RAW
+               DATE_RAW,
+               DATE_UNIX
        } type;
        const char *strftime_fmt;
        int local;
diff --git a/date.c b/date.c
index 4c7aa9ba853c0924d49f4b8c2cc06826fe08956c..a996331f5b33703c9f70844c6b49453ec39d16a7 100644 (file)
--- a/date.c
+++ b/date.c
@@ -177,6 +177,12 @@ 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_UNIX) {
+               strbuf_reset(&timebuf);
+               strbuf_addf(&timebuf, "%lu", time);
+               return timebuf.buf;
+       }
+
        if (mode->local)
                tz = local_tzoffset(time);
 
@@ -792,6 +798,8 @@ static enum date_mode_type parse_date_type(const char *format, const char **end)
                return DATE_NORMAL;
        if (skip_prefix(format, "raw", end))
                return DATE_RAW;
+       if (skip_prefix(format, "unix", end))
+               return DATE_UNIX;
        if (skip_prefix(format, "format", end))
                return DATE_STRFTIME;
 
index 482fec0d7e889a1542dc7428b3da993fd2e38024..c0c910867d75368832ce8b297e9dd82ee984a85a 100755 (executable)
@@ -46,8 +46,10 @@ check_show rfc2822 "$TIME" 'Wed, 15 Jun 2016 16:13:20 +0200'
 check_show short "$TIME" '2016-06-15'
 check_show default "$TIME" 'Wed Jun 15 16:13:20 2016 +0200'
 check_show raw "$TIME" '1466000000 +0200'
+check_show unix "$TIME" '1466000000'
 check_show iso-local "$TIME" '2016-06-15 14:13:20 +0000'
 check_show raw-local "$TIME" '1466000000 +0000'
+check_show unix-local "$TIME" '1466000000'
 
 # arbitrary time absurdly far in the future
 FUTURE="5758122296 -0400"