Skip to content

Commit

Permalink
Add weeks as precision
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbanham committed Feb 8, 2023
1 parent da8d5da commit 93659c2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 12 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
Minute = "minute"
Hour = "hour"
Day = "day"
Week = "week"
Year = "year"
)

Expand All @@ -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:
Expand All @@ -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))
Expand All @@ -71,13 +75,18 @@ 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 {
singularName string
amount int64
}{
{"year", years},
{"week", weeks},
{"day", days},
{"hour", hours},
{"minute", minutes},
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 11 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 93659c2

Please sign in to comment.