Skip to content

Commit f95b2c0

Browse files
author
Matthew
committed
feat(time): embed time struct
This way functions like .Year() are available to the user without having to run `t.Time.Year()`. now all they need to do is call `t.Year()`.
1 parent 0251474 commit f95b2c0

2 files changed

Lines changed: 28 additions & 8 deletions

File tree

time/time.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ const PyOpenSSLTimeDateTimeFormat = "2006-01-02T15:04:05"
1111

1212
// Time represents the standard datetime format used across the Deis Platform.
1313
type Time struct {
14-
time.Time
14+
*time.Time
1515
}
1616

1717
func (t *Time) format() string {
18-
return t.Time.Format(DeisDatetimeFormat)
18+
return t.Format(DeisDatetimeFormat)
1919
}
2020

2121
// MarshalJSON implements the json.Marshaler interface.
2222
// The time is a quoted string in Deis' datetime format.
2323
func (t *Time) MarshalJSON() ([]byte, error) {
24-
return []byte(t.Time.Format(`"` + DeisDatetimeFormat + `"`)), nil
24+
return []byte(t.Format(`"` + DeisDatetimeFormat + `"`)), nil
2525
}
2626

2727
// UnmarshalText implements the encoding.TextUnmarshaler interface.
@@ -31,7 +31,7 @@ func (t *Time) UnmarshalText(data []byte) error {
3131
if _, ok := err.(*time.ParseError); ok {
3232
tt, err = time.Parse(PyOpenSSLTimeDateTimeFormat, string(data))
3333
}
34-
*t = Time{tt}
34+
*t = Time{&tt}
3535
return err
3636
}
3737

@@ -43,6 +43,6 @@ func (t *Time) UnmarshalJSON(data []byte) error {
4343
if _, ok := err.(*time.ParseError); ok {
4444
tt, err = time.Parse(`"`+PyOpenSSLTimeDateTimeFormat+`"`, string(data))
4545
}
46-
*t = Time{tt}
46+
*t = Time{&tt}
4747
return err
4848
}

time/time_test.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,40 @@
11
package time
22

33
import (
4+
"fmt"
45
"testing"
56
)
67

78
func TestUnMarshalText(t *testing.T) {
89
dummyTime := Time{}
910

10-
goodTimeFormats := []string{
11+
standardTimeFormats := []string{
1112
"2006-01-02T15:04:05MST",
12-
"2006-01-02T15:04:05",
13+
"2006-01-02T15:04:05UTC",
14+
"2006-01-02T15:04:05PST",
1315
}
14-
for _, goodTime := range goodTimeFormats {
16+
for _, goodTime := range standardTimeFormats {
1517
if dummyTime.UnmarshalText([]byte(goodTime)) != nil {
1618
t.Error("expected " + goodTime + " to be marshal-able.")
1719
}
20+
if dummyTime.Year() != 2006 {
21+
t.Error(fmt.Sprintf("expected year to be 2006; got %d.", dummyTime.Year()))
22+
}
23+
}
24+
25+
alternateTimeFormats := []string{
26+
"2007-01-02T15:04:05",
27+
"2007-01-02T15:04:05",
28+
"2007-01-02T15:04:05",
29+
}
30+
31+
for _, goodTime := range alternateTimeFormats {
32+
if dummyTime.UnmarshalText([]byte(goodTime)) != nil {
33+
t.Error("expected " + goodTime + " to be marshal-able.")
34+
}
35+
if dummyTime.Year() != 2007 {
36+
t.Error(fmt.Sprintf("expected year to be 2007; got %d.", dummyTime.Year()))
37+
}
1838
}
1939

2040
badTime := "this is a bad time, isn't it?"

0 commit comments

Comments
 (0)