Skip to content

Commit

Permalink
Merge pull request #2 from m1ome/refactoring
Browse files Browse the repository at this point in the history
Library refactoring
  • Loading branch information
davidbanham authored Mar 11, 2019
2 parents 951fcf1 + 470b44e commit 9dde876
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The allowed precisions are year, day, hour, minute and second
```go
import "github.com/davidbanham/human_duration"

example := time.Hour * 25 + time.Minute * 4 + time.Second * 8
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
Expand Down
18 changes: 16 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Package human_duration provides human readable output of
// time.Duration
package human_duration

import (
Expand All @@ -7,9 +9,21 @@ import (
"time"
)

// Available precisions
const (
Second = "second"
Minute = "minute"
Hour = "hour"
Day = "day"
Year = "year"
)

// String converts duration to human readable format, according to precision.
// Example:
// fmt.Println(human_duration.String(time.Hour*24), human_duration.Hour)
func String(duration time.Duration, precision string) string {
years := int64(duration.Hours() / 24 / 365)
days := int64(duration.Hours() / 24)
days := int64(math.Mod(float64(int64(duration.Hours()/24)), 365))
hours := int64(math.Mod(duration.Hours(), 24))
minutes := int64(math.Mod(duration.Minutes(), 60))
seconds := int64(math.Mod(duration.Seconds(), 60))
Expand All @@ -26,7 +40,6 @@ func String(duration time.Duration, precision string) string {
}

parts := []string{}

preciseEnough := false

for _, chunk := range chunks {
Expand All @@ -45,5 +58,6 @@ func String(duration time.Duration, precision string) string {
parts = append(parts, fmt.Sprintf("%d %ss", chunk.amount, chunk.singularName))
}
}

return strings.Join(parts, " ")
}
39 changes: 28 additions & 11 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package human_duration

import (
"fmt"
"testing"
"time"
)
Expand All @@ -11,25 +12,36 @@ type fixture struct {
result string
}

func TestString(t *testing.T) {
t.Parallel()
func ExampleString() {
duration := time.Hour*24*365 + time.Hour*8 + time.Minute*33 + time.Second*24
fmt.Println(String(duration, Second))

start, _ := time.Parse(time.RFC3339, "2012-11-01T22:08:41+00:00")
stop, _ := time.Parse(time.RFC3339, "2013-12-04T23:09:42+00:00")

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
}

func TestString(t *testing.T) {
day := time.Hour * 24
year := day * 365

data := []fixture{
{
duration: time.Hour*25 + time.Minute*4 + time.Second*8,
duration: day + time.Minute*4 + time.Second*8,
precision: "second",
result: "1 day 4 minutes 8 seconds",
},
{
duration: time.Hour*25 + time.Minute*4 + time.Second*8,
duration: day + time.Minute*4 + time.Second*8,
precision: "minute",
result: "1 day 4 minutes",
},
{
duration: time.Hour*25 + time.Minute*4 + time.Second*8,
duration: day + time.Minute*4 + time.Second*8,
precision: "day",
result: "1 day",
},
Expand Down Expand Up @@ -64,7 +76,7 @@ func TestString(t *testing.T) {
result: "1 hour 1 minute 10 seconds",
},
{
duration: time.Hour * 23,
duration: time.Hour * 24,
precision: "day",
result: "1 day",
},
Expand Down Expand Up @@ -93,15 +105,20 @@ func TestString(t *testing.T) {
precision: "hours",
result: "1 hour",
},
{
duration: year + day + time.Hour*2,
precision: "hours",
result: "1 year 1 day 2 hours",
},
}

for _, fixture := range data {
t.Run(fixture.result+fixture.duration.String(), func(t *testing.T) {
f := fixture
t.Run(f.result+" "+f.duration.String(), func(t *testing.T) {
t.Parallel()

result := String(fixture.duration, fixture.precision)
if result != fixture.result {
t.Errorf("got %s, want %s", result, fixture.result)
result := String(f.duration, f.precision)
if result != f.result {
t.Errorf("got %s, want %s", result, f.result)
}
})
}
Expand Down

0 comments on commit 9dde876

Please sign in to comment.