Skip to content

Commit 642833d

Browse files
peffgitster
authored andcommitted
date: add "unix" format
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1a2a1e8 commit 642833d

File tree

5 files changed

+19
-1
lines changed

5 files changed

+19
-1
lines changed

Documentation/rev-list-options.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,10 @@ Note that the `-local` option does not affect the seconds-since-epoch
751751
value (which is always measured in UTC), but does switch the accompanying
752752
timezone value.
753753
+
754+
`--date=unix` shows the date as a Unix epoch timestamp (seconds since
755+
1970). As with `--raw`, this is always in UTC and therefore `-local`
756+
has no effect.
757+
+
754758
`--date=format:...` feeds the format `...` to your system `strftime`.
755759
Use `--date=format:%c` to show the date in your system locale's
756760
preferred format. See the `strftime` manual for a complete list of

builtin/blame.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2626,6 +2626,9 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
26262626
case DATE_RAW:
26272627
blame_date_width = sizeof("1161298804 -0700");
26282628
break;
2629+
case DATE_UNIX:
2630+
blame_date_width = sizeof("1161298804");
2631+
break;
26292632
case DATE_SHORT:
26302633
blame_date_width = sizeof("2006-10-19");
26312634
break;

cache.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,8 @@ struct date_mode {
12231223
DATE_ISO8601_STRICT,
12241224
DATE_RFC2822,
12251225
DATE_STRFTIME,
1226-
DATE_RAW
1226+
DATE_RAW,
1227+
DATE_UNIX
12271228
} type;
12281229
const char *strftime_fmt;
12291230
int local;

date.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ const char *show_date(unsigned long time, int tz, const struct date_mode *mode)
177177
struct tm *tm;
178178
static struct strbuf timebuf = STRBUF_INIT;
179179

180+
if (mode->type == DATE_UNIX) {
181+
strbuf_reset(&timebuf);
182+
strbuf_addf(&timebuf, "%lu", time);
183+
return timebuf.buf;
184+
}
185+
180186
if (mode->local)
181187
tz = local_tzoffset(time);
182188

@@ -792,6 +798,8 @@ static enum date_mode_type parse_date_type(const char *format, const char **end)
792798
return DATE_NORMAL;
793799
if (skip_prefix(format, "raw", end))
794800
return DATE_RAW;
801+
if (skip_prefix(format, "unix", end))
802+
return DATE_UNIX;
795803
if (skip_prefix(format, "format", end))
796804
return DATE_STRFTIME;
797805

t/t0006-date.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ check_show rfc2822 "$TIME" 'Wed, 15 Jun 2016 16:13:20 +0200'
4646
check_show short "$TIME" '2016-06-15'
4747
check_show default "$TIME" 'Wed Jun 15 16:13:20 2016 +0200'
4848
check_show raw "$TIME" '1466000000 +0200'
49+
check_show unix "$TIME" '1466000000'
4950
check_show iso-local "$TIME" '2016-06-15 14:13:20 +0000'
5051
check_show raw-local "$TIME" '1466000000 +0000'
52+
check_show unix-local "$TIME" '1466000000'
5153

5254
# arbitrary time absurdly far in the future
5355
FUTURE="5758122296 -0400"

0 commit comments

Comments
 (0)