Skip to content

Commit

Permalink
Factor out and add test for minuteTime.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitshur committed Jun 20, 2015
1 parent 7dd02de commit c38f621
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
7 changes: 1 addition & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,7 @@ func loadTemplates() error {
var err error
t = template.New("").Funcs(template.FuncMap{
"commitId": func(commitId vcs.CommitID) vcs.CommitID { return commitId[:8] },
"time": func(then time.Time) string {
if time.Since(then) < time.Minute {
return "less than a minute ago"
}
return humanize.Time(then)
},
"time": minuteTime,
})
t, err = t.ParseGlob("./assets/*.tmpl")
return err
Expand Down
19 changes: 19 additions & 0 deletions time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"time"

"github.com/dustin/go-humanize"
)

// minuteTime is like humanize.Time, but it returns "less than a minute ago" for durations under a minute.
func minuteTime(then time.Time) string {
return minuteRelTime(then, time.Now())
}

func minuteRelTime(then, now time.Time) string {
if now.Sub(then) < time.Minute {
return "less than a minute ago"
}
return humanize.RelTime(then, now, "ago", "from now")
}
27 changes: 27 additions & 0 deletions time_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"fmt"
"time"

"github.com/dustin/go-humanize"
)

func ExampleMinuteRelTime() {
then := time.Unix(0, 0)
now0 := time.Unix(59, 999999999)
now1 := now0.Add(1)

fmt.Println(humanize.RelTime(then, now0, "ago", "from now"))
fmt.Println(humanize.RelTime(then, now1, "ago", "from now"))
fmt.Println()
fmt.Println(minuteRelTime(then, now0))
fmt.Println(minuteRelTime(then, now1))

// Output:
// 59 seconds ago
// 1 minute ago
//
// less than a minute ago
// 1 minute ago
}

0 comments on commit c38f621

Please sign in to comment.