Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom date format fixes #673

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ Bug fixes:
- Fix reading from stdin for `tig show`.
- Document problem of outdated system-wide `tigrc` files in Homebrew. (GH #598)
- Repaint the display when toggling `line-graphics`. (GH #527)
- Fix custom date formatting to support longer strings and the time zone offset
specifier ("%z"). (GH #522)

tig-2.2.2
---------
Expand Down
2 changes: 2 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ AC_CHECK_FUNCS([mkstemps], [AC_SUBST([NO_MKSTEMPS], ["#"])])
AC_CHECK_FUNCS([setenv], [AC_SUBST([NO_SETENV], ["#"])])
AC_CHECK_FUNCS([strndup], [AC_SUBST([NO_STRNDUP], ["#"])])

AC_CHECK_MEMBERS([struct tm.tm_gmtoff],,,[#include <time.h>])

AX_WITH_CURSES
case "$ax_cv_ncurses" in "no")
AC_MSG_ERROR([ncurses not found])
Expand Down
8 changes: 5 additions & 3 deletions doc/tigrc.5.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -453,11 +453,13 @@ date::
How to display dates. If set to "relative" or "relative-compact" a
relative date will be used, e.g. "2 minutes ago" or "2m". If set to
"custom", the strftime(3) string format specified in the "format"
option is used.
option is used.
- 'local' (bool): If true, use localtime(3) to convert to local
timezone. Note that relative dates always use local offsets.
- 'format' (string): format string to pass to strftime(3) when 'custom'
display mode has been selected.
- 'format' (string): custom date format string to pass to strftime(3)
when 'custom' display mode has been selected. For time zones, "%z" is
supported whereas "%Z" will always show "UTC", since Git only stores
time zone offsets.
- 'width' (int): Width of the column. When set to zero, the width is
automatically sized to fit the content.

Expand Down
31 changes: 25 additions & 6 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,10 @@ get_relative_date(const struct time *time, char *buf, size_t buflen, bool compac
const char *
mkdate(const struct time *time, enum date date, bool local, const char *custom_format)
{
static char buf[STRING_SIZE("2006-04-29 14:21") + 1];
static char buf[SIZEOF_STR];
struct tm tm;
const char *format;
char tzbuf[20] = "";

if (!date || !time || !time->sec)
return "";
Expand All @@ -191,17 +192,35 @@ mkdate(const struct time *time, enum date date, bool local, const char *custom_f
return get_relative_date(time, buf, sizeof(buf),
date == DATE_RELATIVE_COMPACT);

format = date != DATE_CUSTOM
? "%Y-%m-%d %H:%M"
: custom_format ? custom_format : "%Y-%m-%d";

tzset();
if (local) {
time_t date = time->sec + time->tz;
localtime_r(&date, &tm);
}
else {

} else {
gmtime_r(&time->sec, &tm);

#if HAVE_STRUCT_TM_TM_GMTOFF
/* Format dates with time zones by temporarily setting
* the TZ environment variable and calling tzset(3) so
* gmtime and strftime has the proper time zone info.
* NOTE: Only works for %z (ie. formatting time zones as
* offset +0200) since Git only records offsets in the
* ident dates. */
if (format == custom_format) {
if (!string_format(tzbuf, "UTC%+03d:%02d",
(time->tz / 60 / 60), (time->tz / 60 % 60)))
return "";
tm.tm_gmtoff = time->tz;
tm.tm_zone = tzbuf;
}
#endif
}

format = date != DATE_CUSTOM
? "%Y-%m-%d %H:%M"
: custom_format ? custom_format : "%Y-%m-%d";
return strftime(buf, sizeof(buf), format, &tm) ? buf : NULL;
}

Expand Down
67 changes: 67 additions & 0 deletions test/main/date-test
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ steps '

:toggle date
:save-display no-date.screen

:set main-view-date = custom
:set main-view-date-local = true
:set main-view-date-format = "%F %R %Z"
:save-display custom-date.screen

:set main-view-date-local = false
:set main-view-date-format = "%F %R %z"
:save-display custom-date-non-local.screen
'

test_tig --pretty=raw < "$source_dir/$test.in"
Expand Down Expand Up @@ -205,3 +214,61 @@ Jonas Fonseca Only split the tree view when the tree view is visible
Jonas Fonseca Initial commit
[main] 91912eb97da4f6907015dab41ef9bba315730854 - commit 1 of 25 100%
EOF

assert_equals 'custom-date.screen' <<EOF
2015-08-30 19:01 UTC Kentaro Wada Add zsh completion file for autoload
2015-08-23 22:32 UTC Jonas Fonseca Refactor DEFINE_ALLOCATOR to use helper
2015-07-16 09:46 UTC Sven Wegener display: factor out separator functions
2015-08-22 20:20 UTC Jonas Fonseca Merge pull request #429 from ideal/mast
2015-08-18 07:24 UTC ideal check if mem is NULL in DEFINE_ALLOCATO
2015-06-08 03:44 UTC Jonas Fonseca Use git_init to initialize the test/sta
2015-04-07 00:34 UTC Jonas Fonseca Keep unstaged changes view open after a
2015-03-30 11:43 UTC Jonas Fonseca When redrawing the readline prompt also
2015-02-03 02:53 UTC Jonas Fonseca Move script helper before usages in the
2015-01-29 15:44 UTC Jonas Fonseca Merge branch 'hashed-refs'
2014-12-04 16:22 UTC Jonas Fonseca hashed-refs: Use a hash table as the ma
2014-11-08 14:55 UTC Jonas Fonseca Unify option_info lookup for normal opt
2014-10-07 01:22 UTC Jonas Fonseca Add option to turn off automatic enabli
2014-09-23 10:54 UTC Tom Greuter Add option to install Tig with Homebrew
2014-09-04 11:19 UTC Jonas Fonseca Fix diff context restoring for diff ope
2013-12-14 22:55 UTC Michael Barlow Add mouse support
2012-10-01 02:43 UTC Jonas Fonseca Refactor stage view title formatting
2012-08-30 14:47 UTC Jonas Fonseca [GH #83] WIP: main view lazy navigation
2011-10-13 06:16 UTC P. Sadik Create and use dup() of STDIN_FILENO in
2010-09-16 01:01 UTC Jonathan Neuschäfer Don't show out-of-sight tildes
2010-06-28 08:26 UTC Pierre Habouzit Add an option to ignore unknown directo
2009-01-17 22:10 UTC Jonas Fonseca Fix another regression from the dirty f
2007-09-29 21:23 UTC Jonas Fonseca tig-0.10.git
2006-09-11 22:22 UTC Jonas Fonseca Only split the tree view when the tree
2006-04-10 16:39 UTC Jonas Fonseca Initial commit
[main] 91912eb97da4f6907015dab41ef9bba315730854 - commit 1 of 25 100%
EOF

assert_equals 'custom-date-non-local.screen' <<EOF
2015-08-31 04:01 +0900 Kentaro Wada Add zsh completion file for autoload
2015-08-23 18:32 -0400 Jonas Fonseca Refactor DEFINE_ALLOCATOR to use help
2015-07-16 11:46 +0200 Sven Wegener display: factor out separator functio
2015-08-22 16:20 -0400 Jonas Fonseca Merge pull request #429 from ideal/ma
2015-08-18 15:24 +0800 ideal check if mem is NULL in DEFINE_ALLOCA
2015-06-07 23:44 -0400 Jonas Fonseca Use git_init to initialize the test/s
2015-04-06 20:34 -0400 Jonas Fonseca Keep unstaged changes view open after
2015-03-30 07:43 -0400 Jonas Fonseca When redrawing the readline prompt al
2015-02-02 21:53 -0500 Jonas Fonseca Move script helper before usages in t
2015-01-29 10:44 -0500 Jonas Fonseca Merge branch 'hashed-refs'
2014-12-04 11:22 -0500 Jonas Fonseca hashed-refs: Use a hash table as the
2014-11-08 09:55 -0500 Jonas Fonseca Unify option_info lookup for normal o
2014-10-06 21:22 -0400 Jonas Fonseca Add option to turn off automatic enab
2014-09-23 12:54 +0200 Tom Greuter Add option to install Tig with Homebr
2014-09-04 07:19 -0400 Jonas Fonseca Fix diff context restoring for diff o
2013-12-15 09:55 +1100 Michael Barlow Add mouse support
2012-09-30 22:43 -0400 Jonas Fonseca Refactor stage view title formatting
2012-08-30 10:47 -0400 Jonas Fonseca [GH #83] WIP: main view lazy navigati
2011-10-13 11:46 +0000 P. Sadik Create and use dup() of STDIN_FILENO
2010-09-15 21:01 -0400 Jonathan Neuschäfer Don't show out-of-sight tildes
2010-06-28 10:26 +0200 Pierre Habouzit Add an option to ignore unknown direc
2009-01-17 23:10 +0100 Jonas Fonseca Fix another regression from the dirty
2007-09-29 23:23 +0200 Jonas Fonseca tig-0.10.git
2006-09-12 00:22 +0200 Jonas Fonseca Only split the tree view when the tre
2006-04-10 18:39 +0200 Jonas Fonseca Initial commit
[main] 91912eb97da4f6907015dab41ef9bba315730854 - commit 1 of 25 100%
EOF