introduce "format" date-mode
This feeds the format directly to strftime. Besides being a little more flexible, the main advantage is that your system strftime may know more about your locale's preferred format (e.g., how to spell the days of the week). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Jun 25, 2015 at 12:55 UTC
aa1462cc3d3b0c4c8ad6a60aaf31e0f3a424162d
8 files changed
+61
-1
Documentation/rev-list-options.txt
+5
@@ -727,6 +727,11 @@ format, often found in email messages.
727
+
728
`--date=raw` shows the date in the internal raw Git format `%s %z` format.
729
+
730
+`--date=format:...` feeds the format `...` to your system `strftime`.
731
+Use `--date=format:%c` to show the date in your system locale's
732
+preferred format. See the `strftime` manual for a complete list of
733
+format placeholders.
734
++
735
`--date=default` shows timestamps in the original time zone
736
(either committer's or author's).
737
builtin/blame.c
+3
@@ -2604,6 +2604,9 @@ parse_done:
2604
case DATE_NORMAL:
2605
blame_date_width = sizeof("Thu Oct 19 16:00:04 2006 -0700");
2606
break;
2607
+ case DATE_STRFTIME:
2608
+ blame_date_width = strlen(show_date(0, 0, &blame_date_mode)) + 1; /* add the null */
2609
+ break;
2610
}
2611
blame_date_width -= 1; /* strip the null */
2612
cache.h
+2
@@ -1114,8 +1114,10 @@ struct date_mode {
1114
DATE_ISO8601,
1115
DATE_ISO8601_STRICT,
1116
DATE_RFC2822,
1117
+ DATE_STRFTIME,
1118
DATE_RAW
1119
} type;
1120
+ const char *strftime_fmt;
1121
};
1122
1123
/*
date.c
+8
-1
@@ -163,6 +163,8 @@ void show_date_relative(unsigned long time, int tz,
163
struct date_mode *date_mode_from_type(enum date_mode_type type)
164
{
165
static struct date_mode mode;
166
+ if (type == DATE_STRFTIME)
167
+ die("BUG: cannot create anonymous strftime date_mode struct");
168
mode.type = type;
169
return &mode;
170
}
@@ -221,6 +223,8 @@ const char *show_date(unsigned long time, int tz, const struct date_mode *mode)
223
weekday_names[tm->tm_wday], tm->tm_mday,
224
month_names[tm->tm_mon], tm->tm_year + 1900,
225
tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
226
+ else if (mode->type == DATE_STRFTIME)
227
+ strbuf_addftime(&timebuf, mode->strftime_fmt, tm);
228
else
229
strbuf_addf(&timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
230
weekday_names[tm->tm_wday],
@@ -787,7 +791,10 @@ void parse_date_format(const char *format, struct date_mode *mode)
791
mode->type = DATE_NORMAL;
792
else if (!strcmp(format, "raw"))
793
mode->type = DATE_RAW;
790
- else
794
+ else if (skip_prefix(format, "format:", &format)) {
795
+ mode->type = DATE_STRFTIME;
796
+ mode->strftime_fmt = xstrdup(format);
797
+ } else
798
die("unknown date format %s", format);
799
}
800
gettext.c
+1
@@ -162,6 +162,7 @@ void git_setup_gettext(void)
162
podir = GIT_LOCALE_PATH;
163
bindtextdomain("git", podir);
164
setlocale(LC_MESSAGES, "");
165
+ setlocale(LC_TIME, "");
166
init_gettext_charset("git");
167
textdomain("git");
168
}
strbuf.c
+29
@@ -709,3 +709,32 @@ char *xstrfmt(const char *fmt, ...)
709
710
return ret;
711
}
712
+
713
+void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm)
714
+{
715
+ size_t len;
716
+
717
+ /*
718
+ * strftime reports "0" if it could not fit the result in the buffer.
719
+ * Unfortunately, it also reports "0" if the requested time string
720
+ * takes 0 bytes. So if we were to probe and grow, we have to choose
721
+ * some arbitrary cap beyond which we guess that the format probably
722
+ * just results in a 0-length output. Since we have to choose some
723
+ * reasonable cap anyway, and since it is not that big, we may
724
+ * as well just grow to their in the first place.
725
+ */
726
+ strbuf_grow(sb, 128);
727
+ len = strftime(sb->buf + sb->len, sb->alloc - sb->len, fmt, tm);
728
+
729
+ if (!len) {
730
+ /*
731
+ * Either we failed, or the format actually produces a 0-length
732
+ * output. There's not much we can do, so we leave it blank.
733
+ * However, the output array is left in an undefined state, so
734
+ * we must re-assert our NUL terminator.
735
+ */
736
+ sb->buf[sb->len] = '\0';
737
+ } else {
738
+ sb->len += len;
739
+ }
740
+}
strbuf.h
+5
@@ -344,6 +344,11 @@ extern void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...);
344
__attribute__((format (printf,2,0)))
345
extern void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap);
346
347
+/**
348
+ * Add the time specified by `tm`, as formatted by `strftime`.
349
+ */
350
+extern void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm);
351
+
352
/**
353
* Read a given size of data from a FILE* pointer to the buffer.
354
*
t/t6300-for-each-ref.sh
+8
@@ -227,6 +227,14 @@ test_expect_success 'Check format "rfc2822" date fields output' '
227
test_cmp expected actual
228
'
229
230
+test_expect_success 'Check format of strftime date fields' '
231
+ echo "my date is 2006-07-03" >expected &&
232
+ git for-each-ref \
233
+ --format="%(authordate:format:my date is %Y-%m-%d)" \
234
+ refs/heads >actual &&
235
+ test_cmp expected actual
236
+'
237
+
238
cat >expected <<\EOF
239
refs/heads/master
240
refs/remotes/origin/master