-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor out and add test for minuteTime.
- Loading branch information
Showing
3 changed files
with
47 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |