Skip to content

Commit

Permalink
Add less than text
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbanham committed Mar 31, 2023
1 parent a15ce59 commit bb23251
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ example := time.Hour * 24 + time.Minute * 4 + time.Second * 8
fmt.Println(human_duration.String(example, "second")) // 1 day 4 minutes 8 seconds
fmt.Println(human_duration.String(example, "minute")) // 1 day 4 minutes
fmt.Println(human_duration.String(example, "day")) // 1 day
fmt.Println(human_duration.String(example, "year")) // less than a year

day := time.Hour * 24
year := day * 365
Expand Down
22 changes: 18 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ func StringCeiling(duration time.Duration, precision, ceiling string) string {
return StringCeilingPadded(duration, precision, ceiling, false)
}

type chunk struct {
singularName string
amount int64
}

func StringCeilingPadded(duration time.Duration, precision, ceiling string, padded bool) string {
years := int64(duration.Hours() / 24 / 365)
weeks := int64(math.Mod(float64(int64(duration.Hours()/24/7)), 52))
Expand Down Expand Up @@ -81,10 +86,7 @@ func StringCeilingPadded(duration time.Duration, precision, ceiling string, padd
years = 0
}

chunks := []struct {
singularName string
amount int64
}{
chunks := []chunk{
{"year", years},
{"week", weeks},
{"day", days},
Expand All @@ -100,12 +102,20 @@ func StringCeilingPadded(duration time.Duration, precision, ceiling string, padd
unpaddedNumberFormat := "%d"
paddedNumberFormat := "%02d"

hitSomething := false
var targetChunk chunk

for _, chunk := range chunks {
if preciseEnough {
continue
}

if chunk.amount != 0 {
hitSomething = true
}

if chunk.singularName == precision || chunk.singularName+"s" == precision {
targetChunk = chunk
preciseEnough = true
}

Expand All @@ -126,6 +136,10 @@ func StringCeilingPadded(duration time.Duration, precision, ceiling string, padd
}
}

if !hitSomething {
return "less than a " + targetChunk.singularName
}

return strings.Join(parts, " ")
}

Expand Down
15 changes: 15 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ func TestString(t *testing.T) {
precision: "day",
result: "1 day",
},
{
duration: day + time.Minute*4 + time.Second*8,
precision: "year",
result: "less than a year",
},
{
duration: year*4 + day*2,
precision: "second",
Expand Down Expand Up @@ -116,6 +121,16 @@ func TestString(t *testing.T) {
precision: "week",
result: "2 weeks",
},
{
duration: day,
precision: "week",
result: "less than a week",
},
{
duration: day + time.Hour,
precision: "week",
result: "less than a week",
},
}

for _, fixture := range data {
Expand Down

0 comments on commit bb23251

Please sign in to comment.