diff --git a/README.md b/README.md index 5415dc6..0adb47f 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ A little Go util to print duration strings in a human-friendly format The [String](https://godoc.org/github.com/davidbanham/human_duration#String) function takes a Duration and the precision that's important to the user. -The allowed precisions are year, day, hour, minute and second +The allowed precisions are year, week, day, hour, minute and second ## Usage diff --git a/main.go b/main.go index ad87716..2a36b1d 100644 --- a/main.go +++ b/main.go @@ -16,6 +16,7 @@ const ( Minute = "minute" Hour = "hour" Day = "day" + Week = "week" Year = "year" ) @@ -29,6 +30,8 @@ func precisionToDuration(precision string) time.Duration { return time.Hour case Day: return time.Hour * 24 + case Week: + return time.Hour * 24 * 7 case Year: return time.Hour * 24 * 365 default: @@ -47,7 +50,8 @@ func StringCeiling(duration time.Duration, precision, ceiling string) string { func StringCeilingPadded(duration time.Duration, precision, ceiling string, padded bool) string { years := int64(duration.Hours() / 24 / 365) - days := int64(math.Mod(float64(int64(duration.Hours()/24)), 365)) + weeks := int64(math.Mod(float64(int64(duration.Hours()/24/7)), 52)) + days := int64(math.Mod(float64(int64(duration.Hours()/24)), 365)) - (weeks * 7) hours := int64(math.Mod(duration.Hours(), 24)) minutes := int64(math.Mod(duration.Minutes(), 60)) seconds := int64(math.Mod(duration.Seconds(), 60)) @@ -71,6 +75,10 @@ func StringCeilingPadded(duration time.Duration, precision, ceiling string, padd case Day: days = int64(float64(int64(duration.Hours() / 24))) years = 0 + weeks = 0 + case Week: + weeks = int64(float64(int64(duration.Hours() / 24 / 7))) + years = 0 } chunks := []struct { @@ -78,6 +86,7 @@ func StringCeilingPadded(duration time.Duration, precision, ceiling string, padd amount int64 }{ {"year", years}, + {"week", weeks}, {"day", days}, {"hour", hours}, {"minute", minutes}, @@ -133,6 +142,8 @@ func shorten(str string) string { str = strings.Replace(str, "year", "y", 1) str = strings.Replace(str, "days", "d", 1) str = strings.Replace(str, "day", "d", 1) + str = strings.Replace(str, "weeks", "w", 1) + str = strings.Replace(str, "week", "w", 1) str = strings.Replace(str, "hours", "h", 1) str = strings.Replace(str, "hour", "h", 1) str = strings.Replace(str, "minutes", "m", 1) diff --git a/main_test.go b/main_test.go index 0838009..25911f7 100644 --- a/main_test.go +++ b/main_test.go @@ -22,7 +22,7 @@ func ExampleString() { fmt.Println(String(stop.Sub(start), Second)) // Output: 1 year 8 hours 33 minutes 24 seconds - // 1 year 33 days 1 hour 1 minute 1 second + // 1 year 4 weeks 5 days 1 hour 1 minute 1 second } @@ -111,6 +111,11 @@ func TestString(t *testing.T) { precision: "hours", result: "1 year 1 day 2 hours", }, + { + duration: day * 14, + precision: "week", + result: "2 weeks", + }, } for _, fixture := range data { @@ -158,6 +163,11 @@ func TestShortString(t *testing.T) { precision: "second", result: "2y2d2m2s", }, + { + duration: 2*year + 16*day + 2*time.Minute + 2*time.Second, + precision: "second", + result: "2y2w2d2m2s", + }, } for _, fixture := range data {