summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDustin Sallings <dustin@spy.net>2017-11-10 12:55:13 -0800
committerDustin Sallings <dustin@spy.net>2017-11-10 23:37:23 -0800
commitbb3d318650d48840a39aa21a027c6630e198e626 (patch)
tree04b6900c10e16cd60468bd05672afa46d9150849
parent0b19b17f90333e44518aa31bbf8126017960aee3 (diff)
downloadgo-humanize-bb3d318650d48840a39aa21a027c6630e198e626.tar.xz
Fix for reported off-by-one (nanosecond) error in RelTime
User reported this error with a test case. I'm not entirely sure how it's different from existing cases, but it does appear to do something undesirable.
-rw-r--r--times.go2
-rw-r--r--times_test.go11
2 files changed, 12 insertions, 1 deletions
diff --git a/times.go b/times.go
index b311f11..dd3fbf5 100644
--- a/times.go
+++ b/times.go
@@ -91,7 +91,7 @@ func CustomRelTime(a, b time.Time, albl, blbl string, magnitudes []RelTimeMagnit
}
n := sort.Search(len(magnitudes), func(i int) bool {
- return magnitudes[i].D >= diff
+ return magnitudes[i].D > diff
})
if n >= len(magnitudes) {
diff --git a/times_test.go b/times_test.go
index c90a71b..b1ab8bf 100644
--- a/times_test.go
+++ b/times_test.go
@@ -37,6 +37,17 @@ func TestPast(t *testing.T) {
}.validate(t)
}
+func TestReltimeOffbyone(t *testing.T) {
+ testList{
+ {"1w-1", RelTime(time.Unix(0, 0), time.Unix(7*24*60*60, -1), "ago", ""), "6 days ago"},
+ {"1w±0", RelTime(time.Unix(0, 0), time.Unix(7*24*60*60, 0), "ago", ""), "1 week ago"},
+ {"1w+1", RelTime(time.Unix(0, 0), time.Unix(7*24*60*60, 1), "ago", ""), "1 week ago"},
+ {"2w-1", RelTime(time.Unix(0, 0), time.Unix(14*24*60*60, -1), "ago", ""), "1 week ago"},
+ {"2w±0", RelTime(time.Unix(0, 0), time.Unix(14*24*60*60, 0), "ago", ""), "2 weeks ago"},
+ {"2w+1", RelTime(time.Unix(0, 0), time.Unix(14*24*60*60, 1), "ago", ""), "2 weeks ago"},
+ }.validate(t)
+}
+
func TestFuture(t *testing.T) {
// Add a little time so that these things properly line up in
// the future.