summaryrefslogtreecommitdiff
path: root/timeutil
diff options
authorJohn R. Lenton <jlenton@gmail.com>2018-03-01 10:06:16 +0000
committerJohn R. Lenton <jlenton@gmail.com>2018-03-01 10:06:16 +0000
commitf8be7bfedaacca8e3c93cdc559851915bc6ef86f (patch)
tree2ed98fd308fa39e62d2d1b576f7b16f8b7cf369b /timeutil
parentefbe84532fc3edd5149ab48c44e9dbbbff8c32d8 (diff)
timeutil: make Human also work with times in the future
Diffstat (limited to 'timeutil')
-rw-r--r--timeutil/human.go27
-rw-r--r--timeutil/human_test.go25
2 files changed, 29 insertions, 23 deletions
diff --git a/timeutil/human.go b/timeutil/human.go
index 77897d5cda..c157cb1088 100644
--- a/timeutil/human.go
+++ b/timeutil/human.go
@@ -32,22 +32,25 @@ func noon(t time.Time) time.Time {
return time.Date(y, m, d, 12, 0, 0, 0, t.Location())
}
-// Human turns the time (which must be in the past) into something
-// more meant for human consumption.
+// Human turns the time into a relative expression of time meant for human
+// consumption.
+// Human(t) --> "today at 07:47 "
func Human(then time.Time) string {
- return humanTimeSince(then, time.Now())
+ return humanTimeSince(then.Local(), time.Now().Local())
}
func humanTimeSince(then, now time.Time) string {
- then = then.Local()
- now = now.Local()
-
- switch d := int(math.Ceil(noon(now).Sub(noon(then)).Hours() / 24)); d {
- case 0:
- return then.Format(i18n.G("today at 15:04-07"))
- case 1:
- return then.Format(i18n.G("yesterday at 15:04-07"))
+ d := int(math.Floor(noon(then).Sub(noon(now)).Hours() / 24))
+ switch {
+ case d > 1:
+ return fmt.Sprintf(then.Format(i18n.G("in %d days, at 15:04 MST")), d)
+ case d < -1:
+ return fmt.Sprintf(then.Format(i18n.G("%d days ago, at 15:04 MST")), -d)
+ case d == 1:
+ return then.Format(i18n.G("tomorrow at 15:04 MST"))
+ case d == -1:
+ return then.Format(i18n.G("yesterday at 15:04 MST"))
default:
- return fmt.Sprintf(then.Format(i18n.G("%d days ago at 15:04-07")), d)
+ return then.Format(i18n.G("today at 15:04 MST"))
}
}
diff --git a/timeutil/human_test.go b/timeutil/human_test.go
index f8d9ccbdf4..bf58931a2c 100644
--- a/timeutil/human_test.go
+++ b/timeutil/human_test.go
@@ -46,31 +46,34 @@ func (s *humanSuite) SetUpSuite(c *check.C) {
s.afterDSTends = time.Date(2017, 10, 29, 1, 1, 0, 0, loc)
// sanity check
- c.Check(s.beforeDSTbegins.Format("-07"), check.Equals, s.afterDSTends.Format("-07"))
- c.Check(s.beforeDSTbegins.Format("-07"), check.Equals, "+00")
- c.Check(s.afterDSTbegins.Format("Z07"), check.Equals, s.beforeDSTends.Format("Z07"))
- c.Check(s.afterDSTbegins.Format("Z07"), check.Equals, "+01")
+ c.Check(s.beforeDSTbegins.Format("MST"), check.Equals, s.afterDSTends.Format("MST"))
+ c.Check(s.beforeDSTbegins.Format("MST"), check.Equals, "GMT")
+ c.Check(s.afterDSTbegins.Format("MST"), check.Equals, s.beforeDSTends.Format("MST"))
+ c.Check(s.afterDSTbegins.Format("MST"), check.Equals, "BST")
// “The month, day, hour, min, sec, and nsec values may be outside their
// usual ranges and will be normalized during the conversion.”
- // so you can always substract 1 from a day and it'll just work \o/
+ // so you can always add or subtract 1 from a day and it'll just work \o/
c.Check(time.Date(2017, -1, -1, -1, -1, -1, 0, loc), check.DeepEquals, time.Date(2016, 10, 29, 22, 58, 59, 0, loc))
+ c.Check(time.Date(2017, 13, 32, 25, 61, 63, 0, loc), check.DeepEquals, time.Date(2018, 2, 2, 2, 2, 3, 0, loc))
}
func (s *humanSuite) TestHumanTimeDST(c *check.C) {
- c.Check(timeutil.HumanTimeSince(s.beforeDSTbegins, s.afterDSTbegins), check.Equals, "today at 00:59+00")
- c.Check(timeutil.HumanTimeSince(s.beforeDSTends, s.afterDSTends), check.Equals, "today at 01:59+01")
- c.Check(timeutil.HumanTimeSince(s.beforeDSTbegins, s.afterDSTends), check.Equals, "218 days ago at 00:59+00")
+ c.Check(timeutil.HumanTimeSince(s.beforeDSTbegins, s.afterDSTbegins), check.Equals, "today at 00:59 GMT")
+ c.Check(timeutil.HumanTimeSince(s.beforeDSTends, s.afterDSTends), check.Equals, "today at 01:59 BST")
+ c.Check(timeutil.HumanTimeSince(s.beforeDSTbegins, s.afterDSTends), check.Equals, "218 days ago, at 00:59 GMT")
}
func (*humanSuite) TestHuman(c *check.C) {
now := time.Now()
- timePart := now.Format("15:04-07")
+ timePart := now.Format("15:04 MST")
y, m, d := now.Date()
H, M, S := now.Clock()
loc := now.Location()
- c.Check(timeutil.Human(now), check.Equals, "today at "+timePart)
+ c.Check(timeutil.Human(time.Date(y, m, d-2, H, M, S, 0, loc)), check.Equals, "2 days ago, at "+timePart)
c.Check(timeutil.Human(time.Date(y, m, d-1, H, M, S, 0, loc)), check.Equals, "yesterday at "+timePart)
- c.Check(timeutil.Human(time.Date(y, m, d-2, H, M, S, 0, loc)), check.Equals, "2 days ago at "+timePart)
+ c.Check(timeutil.Human(now), check.Equals, "today at "+timePart)
+ c.Check(timeutil.Human(time.Date(y, m, d+1, H, M, S, 0, loc)), check.Equals, "tomorrow at "+timePart)
+ c.Check(timeutil.Human(time.Date(y, m, d+2, H, M, S, 0, loc)), check.Equals, "in 2 days, at "+timePart)
}