git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[RFC PATCH 1/2] Use localized date in log output

From
Peter Krefting <peter@softwolves.pp.se>
Date
Oct 23, 2012, 12:25 UTC
Message-ID
<alpine.DEB.2.00.1210231325010.705@ds9.cixit.se>
In-Reply-To
<alpine.DEB.2.00.1210231323480.602@ds9.cixit.se>
When outputting a normal log, without having specified which date format
to use, we should output the current user locale's default format. Do this
by initializing LC_TIME properly and using strftime() to format the date.
---
  date.c    |  4 +++-
  gettext.c |  1 +
  strbuf.c  | 10 ++++++++++
  strbuf.h  |  1 +
  4 files changed, 15 insertions(+), 1 deletion(-)
diff --git a/date.c b/date.c
index 57331ed..88f928c 100644
--- a/date.c
+++ b/date.c
@@ -203,7 +203,7 @@ const char *show_date(unsigned long time, int tz, enum date_mode mode)
  			weekday_names[tm->tm_wday], tm->tm_mday,
  			month_names[tm->tm_mon], tm->tm_year + 1900,
  			tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
-	else
+	else if (mode == DATE_RAW)
  		strbuf_addf(&timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
  				weekday_names[tm->tm_wday],
  				month_names[tm->tm_mon],
@@ -212,6 +212,8 @@ const char *show_date(unsigned long time, int tz, enum date_mode mode)
  				tm->tm_year + 1900,
  				(mode == DATE_LOCAL) ? 0 : ' ',
  				tz);
+	else
+		strbuf_strftime(&timebuf,"%c",tm);
  	return timebuf.buf;
  }

diff --git a/gettext.c b/gettext.c
index 71e9545..a87d144 100644
--- a/gettext.c
+++ b/gettext.c
@@ -126,6 +126,7 @@ void git_setup_gettext(void)
  		podir = GIT_LOCALE_PATH;
  	bindtextdomain("git", podir);
  	setlocale(LC_MESSAGES, "");
+	setlocale(LC_TIME, "");
  	init_gettext_charset("git");
  	textdomain("git");
  }
diff --git a/strbuf.c b/strbuf.c
index 0510f76..d393de9 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -228,6 +228,16 @@ void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap)
  	strbuf_setlen(sb, sb->len + len);
  }

+void strbuf_strftime(struct strbuf *sb, const char *fmt, struct tm *tm)
+{
+	int len;
+
+	if (!strbuf_avail(sb))
+		strbuf_grow(sb, 256);
+	len = strftime(sb->buf + sb->len, sb->alloc - sb->len, fmt, tm);
+	strbuf_setlen(sb, sb->len + len);
+}
+
  void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
  		   void *context)
  {
diff --git a/strbuf.h b/strbuf.h
index be941ee..9ca1d59 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -99,6 +99,7 @@ __attribute__((format (printf,2,3)))
  extern void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
  __attribute__((format (printf,2,0)))
  extern void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap);
+extern void strbuf_strfime(struct strbuf *sb, const char *fmt, struct tm *tm);

  extern void strbuf_add_lines(struct strbuf *sb, const char *prefix, const char *buf, size_t size);
-- 
1.8.0
Previous: Peter KreftingNext: Peter Krefting
Message 2 of 3 in “Localize log output”
  1. 0/2 Localize log outputPeter Krefting, Oct 23, 2012
  2. 1/2 Use localized date in log outputPeter Krefting, Oct 23, 2012
  3. 2/2 Localize diff and log outputPeter Krefting, Oct 23, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.