Skip to content

Commit

Permalink
Fix jonas#428: Change date formatting to show timezones by default
Browse files Browse the repository at this point in the history
The issues:

 1. timezone information is missing from non-local dates,
    e.g. "2018-04-09 15:15 +0800" is displayed as "2018-04-09 15:15"
         "2018-03-19 04:48 -0700" is displayed as "2018-03-19 04:48"
 2. custom dates are formatting %z as "+0000"

With this change:

 1. Show timezone information by default for non-local dates using the
    format string "%Y-%m-%d %H:%M %z"
 2. display the actual timezone when formatting "%z" instead of "+0000"

[ jf: Updated the tests to match the new date formatting and fixed
  mkdate to replace %z and %Z with the timezone rather than passing it
  to strftime and expecting "+0000", which is not always the case. ]

Closes jonas#811
  • Loading branch information
PaulChanHK authored and rolandwalker committed May 21, 2018
1 parent 970c1b7 commit 8803f3c
Show file tree
Hide file tree
Showing 52 changed files with 1,288 additions and 1,195 deletions.
1 change: 1 addition & 0 deletions NEWS.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Improvements:
- Improve worktree and submodule support. (GH #459, #781, #783)
- Support running Tig via a Git alias. (GH #763)
- Use ISO-8601 letters for short relative dates. (GH #759)
- Change date formatting to show timezones by default. (GH #428, #811)

Bug fixes:

Expand Down
73 changes: 65 additions & 8 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ mkdate(const struct time *time, enum date date, bool local, const char *custom_f
static char buf[SIZEOF_STR];
struct tm tm;
const char *format;
bool tz_fmt;

if (!date || !time || !time->sec)
return "";
Expand All @@ -191,18 +192,74 @@ 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 && custom_format)
? custom_format
: local
? "%Y-%m-%d %H:%M"
: "%Y-%m-%d %H:%M %z";

tz_fmt = strstr(format, "%z") || strstr(format, "%Z");

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

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

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;
if (local || (!tz_fmt))
return !strftime(buf, sizeof(buf), format, &tm) ? NULL : buf;

{
char format_buf[SIZEOF_STR];
char *format_pos = format_buf;
char *buf_pos = buf;
size_t buf_size = sizeof(buf);
int tz = ABS(time->tz);

string_ncopy(format_buf, format, strlen(format));

while (*format_pos) {
char *z_pos = strstr(format_pos, "%z");
char *Z_pos = strstr(format_pos, "%Z");
char *tz_pos = (z_pos && Z_pos) ? MIN(z_pos, Z_pos) : MAX(z_pos, Z_pos);
size_t time_len;

if (tz_pos)
*tz_pos = 0;

time_len = strftime(buf_pos, buf_size, format_pos, &tm);
if (!time_len)
return NULL;

buf_pos += time_len;
buf_size -= time_len;

if (!tz_pos)
break;

/* Skip the %z format flag and insert the timezone. */
format_pos = tz_pos + 2;

if (buf_size < 5)
return NULL;

buf_pos[0] = time->tz > 0 ? '-' : '+';
buf_pos[1] = '0' + (tz / 60 / 60 / 10);
buf_pos[2] = '0' + (tz / 60 / 60 % 10);
buf_pos[3] = '0' + (tz / 60 % 60 / 10);
buf_pos[4] = '0' + (tz / 60 % 60 % 10);
buf_pos[5] = 0;

buf_pos += 5;
buf_size -= 5;
}

#undef buf_size
}

return buf;
}

const char *
Expand Down
148 changes: 74 additions & 74 deletions test/blame/default-test
Original file line number Diff line number Diff line change
Expand Up @@ -28,47 +28,47 @@ in_work_dir create_repo_from_tgz "$base_dir/files/scala-js-benchmarks.tgz"
test_tig blame project/Build.scala

assert_equals 'blame-default.screen' <<EOF
90286e0 Jonas Fonseca 2013-10-14 13:15 1| import sbt._
90286e0 Jonas Fonseca 2013-10-14 13:15 2| import Keys._
90286e0 Jonas Fonseca 2013-10-14 13:15 3|
74537d9 Sébastien Doeraene 2013-10-29 18:46 4| import scala.scalajs.sbtplugin.
90286e0 Jonas Fonseca 2013-10-14 13:15 5| import ScalaJSPlugin._
90286e0 Jonas Fonseca 2013-10-14 13:15 6| import ScalaJSKeys._
90286e0 Jonas Fonseca 2013-10-14 13:15 7|
4779f9b Jonas Fonseca 2013-11-26 20:13 8| object ScalaJSBenchmarks extend
90286e0 Jonas Fonseca 2013-10-14 13:15 9|
4779f9b Jonas Fonseca 2013-11-26 20:13 10| val scalaJSScalaVersion = "2.
90286e0 Jonas Fonseca 2013-10-14 13:15 11|
90286e0 Jonas Fonseca 2013-10-14 13:15 12| val projectSettings = Default
90286e0 Jonas Fonseca 2013-10-14 13:15 13| organization := "scalajs-
90286e0 Jonas Fonseca 2013-10-14 13:15 14| version := "0.1-SNAPSHOT"
90286e0 Jonas Fonseca 2013-10-14 13:15 15| )
90286e0 Jonas Fonseca 2013-10-14 13:15 16|
90286e0 Jonas Fonseca 2013-10-14 13:15 17| val defaultSettings = project
4779f9b Jonas Fonseca 2013-11-26 20:13 18| scalaVersion := scalaJSSc
90286e0 Jonas Fonseca 2013-10-14 13:15 19| scalacOptions ++= Seq(
90286e0 Jonas Fonseca 2013-10-14 13:15 20| "-deprecation",
90286e0 Jonas Fonseca 2013-10-14 13:15 21| "-unchecked",
90286e0 Jonas Fonseca 2013-10-14 13:15 22| "-feature",
90286e0 Jonas Fonseca 2013-10-14 13:15 23| "-encoding", "utf8"
90286e0 Jonas Fonseca 2013-10-14 13:15 24| )
90286e0 Jonas Fonseca 2013-10-14 13:15 25| )
90286e0 Jonas Fonseca 2013-10-14 13:15 26|
90286e0 Jonas Fonseca 2013-10-14 13:15 27| lazy val parent: Project = Pr
90286e0 Jonas Fonseca 2013-10-14 13:15 28| id = "parent",
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 1| import sbt._
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 2| import Keys._
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 3|
74537d9 Sébastien Doeraene 2013-10-29 18:46 +0100 4| import scala.scalajs.sbtp
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 5| import ScalaJSPlugin._
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 6| import ScalaJSKeys._
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 7|
4779f9b Jonas Fonseca 2013-11-26 20:13 -0500 8| object ScalaJSBenchmarks
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 9|
4779f9b Jonas Fonseca 2013-11-26 20:13 -0500 10| val scalaJSScalaVersion
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 11|
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 12| val projectSettings = D
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 13| organization := "sc
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 14| version := "0.1-SNA
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 15| )
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 16|
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 17| val defaultSettings = p
4779f9b Jonas Fonseca 2013-11-26 20:13 -0500 18| scalaVersion := sca
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 19| scalacOptions ++= S
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 20| "-deprecation",
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 21| "-unchecked",
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 22| "-feature",
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 23| "-encoding", "u
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 24| )
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 25| )
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 26|
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 27| lazy val parent: Projec
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 28| id = "parent",
[blame] project/Build.scala - line 1 of 64 43%
EOF

assert_equals 'blame-with-diff.screen' <<EOF
90286e0 Jonas Fonseca 2013-10-14 13:15 1| import sbt._
90286e0 Jonas Fonseca 2013-10-14 13:15 2| import Keys._
90286e0 Jonas Fonseca 2013-10-14 13:15 3|
74537d9 Sébastien Doeraene 2013-10-29 18:46 4| import scala.scalajs.sbtplugin.
90286e0 Jonas Fonseca 2013-10-14 13:15 5| import ScalaJSPlugin._
90286e0 Jonas Fonseca 2013-10-14 13:15 6| import ScalaJSKeys._
90286e0 Jonas Fonseca 2013-10-14 13:15 7|
4779f9b Jonas Fonseca 2013-11-26 20:13 8| object ScalaJSBenchmarks extend
90286e0 Jonas Fonseca 2013-10-14 13:15 9|
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 1| import sbt._
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 2| import Keys._
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 3|
74537d9 Sébastien Doeraene 2013-10-29 18:46 +0100 4| import scala.scalajs.sbtp
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 5| import ScalaJSPlugin._
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 6| import ScalaJSKeys._
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 7|
4779f9b Jonas Fonseca 2013-11-26 20:13 -0500 8| object ScalaJSBenchmarks
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 9|
[blame] project/Build.scala - line 4 of 64 14%
commit 74537d9b257954056d3caa19eb3837500aded883
Author: Sébastien Doeraene <sjrdoeraene@gmail.com>
Expand All @@ -92,15 +92,15 @@ index 560bca1..1713681 100644
EOF

assert_equals 'blame-with-diff-no-file-filter.screen' <<EOF
90286e0 Jonas Fonseca 2013-10-14 13:15 1| import sbt._
90286e0 Jonas Fonseca 2013-10-14 13:15 2| import Keys._
90286e0 Jonas Fonseca 2013-10-14 13:15 3|
74537d9 Sébastien Doeraene 2013-10-29 18:46 4| import scala.scalajs.sbtplugin.
90286e0 Jonas Fonseca 2013-10-14 13:15 5| import ScalaJSPlugin._
90286e0 Jonas Fonseca 2013-10-14 13:15 6| import ScalaJSKeys._
90286e0 Jonas Fonseca 2013-10-14 13:15 7|
4779f9b Jonas Fonseca 2013-11-26 20:13 8| object ScalaJSBenchmarks extend
90286e0 Jonas Fonseca 2013-10-14 13:15 9|
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 1| import sbt._
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 2| import Keys._
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 3|
74537d9 Sébastien Doeraene 2013-10-29 18:46 +0100 4| import scala.scalajs.sbtp
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 5| import ScalaJSPlugin._
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 6| import ScalaJSKeys._
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 7|
4779f9b Jonas Fonseca 2013-11-26 20:13 -0500 8| object ScalaJSBenchmarks
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 9|
[blame] project/Build.scala - line 4 of 64 14%
commit 74537d9b257954056d3caa19eb3837500aded883
Author: Sébastien Doeraene <sjrdoeraene@gmail.com>
Expand All @@ -124,33 +124,33 @@ index 560bca1..1713681 100644
EOF

assert_equals 'blame-parent-of-74537d9.screen' <<EOF
90286e0 Jonas Fonseca 2013-10-14 13:15 1| import sbt._
90286e0 Jonas Fonseca 2013-10-14 13:15 2| import Keys._
90286e0 Jonas Fonseca 2013-10-14 13:15 3|
90286e0 Jonas Fonseca 2013-10-14 13:15 4| import ch.epfl.lamp.sbtscalajs._
90286e0 Jonas Fonseca 2013-10-14 13:15 5| import ScalaJSPlugin._
90286e0 Jonas Fonseca 2013-10-14 13:15 6| import ScalaJSKeys._
90286e0 Jonas Fonseca 2013-10-14 13:15 7|
90286e0 Jonas Fonseca 2013-10-14 13:15 8| object ScalaJSBuild extends Build {
90286e0 Jonas Fonseca 2013-10-14 13:15 9|
90286e0 Jonas Fonseca 2013-10-14 13:15 10| val scalajsScalaVersion = "2.10.2"
90286e0 Jonas Fonseca 2013-10-14 13:15 11|
90286e0 Jonas Fonseca 2013-10-14 13:15 12| val projectSettings = Defaults.def
90286e0 Jonas Fonseca 2013-10-14 13:15 13| organization := "scalajs-bench
90286e0 Jonas Fonseca 2013-10-14 13:15 14| version := "0.1-SNAPSHOT"
90286e0 Jonas Fonseca 2013-10-14 13:15 15| )
90286e0 Jonas Fonseca 2013-10-14 13:15 16|
90286e0 Jonas Fonseca 2013-10-14 13:15 17| val defaultSettings = projectSetti
90286e0 Jonas Fonseca 2013-10-14 13:15 18| scalaVersion := scalajsScalaVe
90286e0 Jonas Fonseca 2013-10-14 13:15 19| scalacOptions ++= Seq(
90286e0 Jonas Fonseca 2013-10-14 13:15 20| "-deprecation",
90286e0 Jonas Fonseca 2013-10-14 13:15 21| "-unchecked",
90286e0 Jonas Fonseca 2013-10-14 13:15 22| "-feature",
90286e0 Jonas Fonseca 2013-10-14 13:15 23| "-encoding", "utf8"
90286e0 Jonas Fonseca 2013-10-14 13:15 24| )
90286e0 Jonas Fonseca 2013-10-14 13:15 25| )
90286e0 Jonas Fonseca 2013-10-14 13:15 26|
90286e0 Jonas Fonseca 2013-10-14 13:15 27| lazy val benchmarkSettings = defau
90286e0 Jonas Fonseca 2013-10-14 13:15 28| unmanagedSources in (Compile,
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 1| import sbt._
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 2| import Keys._
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 3|
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 4| import ch.epfl.lamp.sbtscalajs
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 5| import ScalaJSPlugin._
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 6| import ScalaJSKeys._
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 7|
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 8| object ScalaJSBuild extends Bu
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 9|
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 10| val scalajsScalaVersion = "2
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 11|
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 12| val projectSettings = Defaul
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 13| organization := "scalajs
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 14| version := "0.1-SNAPSHOT
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 15| )
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 16|
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 17| val defaultSettings = projec
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 18| scalaVersion := scalajsS
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 19| scalacOptions ++= Seq(
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 20| "-deprecation",
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 21| "-unchecked",
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 22| "-feature",
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 23| "-encoding", "utf8"
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 24| )
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 25| )
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 26|
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 27| lazy val benchmarkSettings =
90286e0 Jonas Fonseca 2013-10-14 13:15 -0400 28| unmanagedSources in (Com
[blame] project/Build.scala - line 4 of 66 42%
EOF
Loading

0 comments on commit 8803f3c

Please sign in to comment.