Skip to content

Commit 172ae9d

Browse files
committed
event: Add String() to duration
* Add a String() method on duration to return a human friendly representation of the duration * Update MarshalJSON to marshal the underying time.Duration instead. Though this is less human friendly, it's more friendly to computers on the other side
1 parent ad6a181 commit 172ae9d

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

event.go

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gdq
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"strings"
67
"time"
@@ -13,7 +14,37 @@ type duration struct {
1314
}
1415

1516
func (d duration) MarshalJSON() (b []byte, err error) {
16-
return []byte(fmt.Sprintf(`"%s"`, d.String())), nil
17+
return json.Marshal(d.Duration)
18+
}
19+
20+
func (d duration) String() string {
21+
dur := d.Round(time.Minute)
22+
h := dur / time.Hour
23+
m := (dur - (h * time.Hour)) / time.Minute
24+
if h == 0 {
25+
if m == 1 {
26+
return "1 minute"
27+
}
28+
return fmt.Sprintf("%d minutes", m)
29+
}
30+
b := strings.Builder{}
31+
if h == 1 {
32+
b.WriteString("1 hour")
33+
} else {
34+
b.WriteString(fmt.Sprintf("%d hours", h))
35+
}
36+
37+
if m == 0 {
38+
return b.String()
39+
}
40+
41+
b.WriteString(" and ")
42+
if m == 1 {
43+
b.WriteString("1 minute")
44+
} else {
45+
b.WriteString(fmt.Sprintf("%d minutes", m))
46+
}
47+
return b.String()
1748
}
1849

1950
// Event represents a single event at a GDQ

event_test.go

+24-3
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,37 @@ import (
88
"github.com/anaskhan96/soup"
99
)
1010

11-
func TestDurationMarhsal(t *testing.T) {
11+
func TestDurationMarshal(t *testing.T) {
1212
d := duration{0}
1313
j, err := d.MarshalJSON()
1414
assertEqual(t, err, nil)
15-
assertEqual(t, string(j), "\"0s\"")
15+
assertEqual(t, string(j), "0")
1616

1717
dn := duration{1*time.Hour + 1*time.Minute + 1*time.Second}
1818
jn, err := dn.MarshalJSON()
1919
assertEqual(t, err, nil)
20-
assertEqual(t, string(jn), "\"1h1m1s\"")
20+
assertEqual(t, string(jn), "3661000000000")
21+
}
22+
23+
func TestDurationString(t *testing.T) {
24+
t.Run("no hours, no minutes", func(t *testing.T) {
25+
assertEqual(t, duration{0}.String(), "0 minutes")
26+
})
27+
t.Run("one hour, no minutes", func(t *testing.T) {
28+
assertEqual(t, duration{1 * time.Hour}.String(), "1 hour")
29+
})
30+
t.Run("one hour, one minute", func(t *testing.T) {
31+
assertEqual(t, duration{1*time.Hour + 1*time.Minute}.String(), "1 hour and 1 minute")
32+
})
33+
t.Run("one hour, two minutes", func(t *testing.T) {
34+
assertEqual(t, duration{1*time.Hour + 2*time.Minute}.String(), "1 hour and 2 minutes")
35+
})
36+
t.Run("one minute", func(t *testing.T) {
37+
assertEqual(t, duration{1 * time.Minute}.String(), "1 minute")
38+
})
39+
t.Run(" two minutes", func(t *testing.T) {
40+
assertEqual(t, duration{2 * time.Minute}.String(), "2 minutes")
41+
})
2142
}
2243

2344
func TestNicksToSlice(t *testing.T) {

0 commit comments

Comments
 (0)