summaryrefslogtreecommitdiff
path: root/strbuf.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2015-08-03 18:01:27 (GMT)
committerJunio C Hamano <gitster@pobox.com>2015-08-03 18:01:27 (GMT)
commitd939af12bd96db7ad3e671a0585ad8570aa7e9d3 (patch)
tree5772ac1192ed299db657ffcfbc71c318521db653 /strbuf.c
parent980a3d3dd742d534b394b04e578c2de5e3ee4793 (diff)
parente4f031e34b08e3217c10942e682920a6939308a0 (diff)
downloadgit-d939af12bd96db7ad3e671a0585ad8570aa7e9d3.zip
git-d939af12bd96db7ad3e671a0585ad8570aa7e9d3.tar.gz
git-d939af12bd96db7ad3e671a0585ad8570aa7e9d3.tar.bz2
Merge branch 'jk/date-mode-format'
Teach "git log" and friends a new "--date=format:..." option to format timestamps using system's strftime(3). * jk/date-mode-format: strbuf: make strbuf_addftime more robust introduce "format" date-mode convert "enum date_mode" into a struct show-branch: use DATE_RELATIVE instead of magic number
Diffstat (limited to 'strbuf.c')
-rw-r--r--strbuf.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/strbuf.c b/strbuf.c
index bbaf32e..cce5eed 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -710,3 +710,36 @@ char *xstrfmt(const char *fmt, ...)
return ret;
}
+
+void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm)
+{
+ size_t hint = 128;
+ size_t len;
+
+ if (!*fmt)
+ return;
+
+ strbuf_grow(sb, hint);
+ len = strftime(sb->buf + sb->len, sb->alloc - sb->len, fmt, tm);
+
+ if (!len) {
+ /*
+ * strftime reports "0" if it could not fit the result in the buffer.
+ * Unfortunately, it also reports "0" if the requested time string
+ * takes 0 bytes. So our strategy is to munge the format so that the
+ * output contains at least one character, and then drop the extra
+ * character before returning.
+ */
+ struct strbuf munged_fmt = STRBUF_INIT;
+ strbuf_addf(&munged_fmt, "%s ", fmt);
+ while (!len) {
+ hint *= 2;
+ strbuf_grow(sb, hint);
+ len = strftime(sb->buf + sb->len, sb->alloc - sb->len,
+ munged_fmt.buf, tm);
+ }
+ strbuf_release(&munged_fmt);
+ len--; /* drop munged space */
+ }
+ strbuf_setlen(sb, sb->len + len);
+}