Skip to content

Commit 4e1b2fb

Browse files
author
Matthew Fisher
committed
Merge pull request #11 from bacongobbler/check-parseerror
check for a time.ParseError, add unit tests
2 parents 49ce800 + f95b2c0 commit 4e1b2fb

2 files changed

Lines changed: 58 additions & 13 deletions

File tree

time/time.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,46 @@ package time
33
import "time"
44

55
// DeisDatetimeFormat is the standard date/time representation used in Deis.
6-
const DeisDatetimeFormat string = "2006-01-02T15:04:05MST"
6+
const DeisDatetimeFormat = "2006-01-02T15:04:05MST"
7+
78
// Different format to deal with the pyopenssl formatting
89
// http://www.pyopenssl.org/en/stable/api/crypto.html#OpenSSL.crypto.X509.get_notAfter
9-
const PyOpenSSLTimeDateTimeFormat string = "2006-01-02T15:04:05"
10+
const PyOpenSSLTimeDateTimeFormat = "2006-01-02T15:04:05"
1011

1112
// Time represents the standard datetime format used across the Deis Platform.
1213
type Time struct {
13-
time.Time
14+
*time.Time
1415
}
1516

1617
func (t *Time) format() string {
17-
return t.Time.Format(DeisDatetimeFormat)
18+
return t.Format(DeisDatetimeFormat)
1819
}
1920

2021
// MarshalJSON implements the json.Marshaler interface.
2122
// The time is a quoted string in Deis' datetime format.
2223
func (t *Time) MarshalJSON() ([]byte, error) {
23-
return []byte(t.Time.Format(`"` + DeisDatetimeFormat + `"`)), nil
24+
return []byte(t.Format(`"` + DeisDatetimeFormat + `"`)), nil
2425
}
2526

2627
// UnmarshalText implements the encoding.TextUnmarshaler interface.
2728
// The time is expected to be in Deis' datetime format.
28-
func (t *Time) UnmarshalText(data []byte) (err error) {
29+
func (t *Time) UnmarshalText(data []byte) error {
2930
tt, err := time.Parse(DeisDatetimeFormat, string(data))
30-
if err != nil {
31+
if _, ok := err.(*time.ParseError); ok {
3132
tt, err = time.Parse(PyOpenSSLTimeDateTimeFormat, string(data))
3233
}
33-
*t = Time{tt}
34-
return
34+
*t = Time{&tt}
35+
return err
3536
}
3637

3738
// UnmarshalJSON implements the json.Unmarshaler interface.
3839
// The time is expected to be a quoted string in Deis' datetime format.
39-
func (t *Time) UnmarshalJSON(data []byte) (err error) {
40+
func (t *Time) UnmarshalJSON(data []byte) error {
4041
// Fractional seconds are handled implicitly by Parse.
4142
tt, err := time.Parse(`"`+DeisDatetimeFormat+`"`, string(data))
42-
if err != nil {
43+
if _, ok := err.(*time.ParseError); ok {
4344
tt, err = time.Parse(`"`+PyOpenSSLTimeDateTimeFormat+`"`, string(data))
4445
}
45-
*t = Time{tt}
46-
return
46+
*t = Time{&tt}
47+
return err
4748
}

time/time_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package time
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestUnMarshalText(t *testing.T) {
9+
dummyTime := Time{}
10+
11+
standardTimeFormats := []string{
12+
"2006-01-02T15:04:05MST",
13+
"2006-01-02T15:04:05UTC",
14+
"2006-01-02T15:04:05PST",
15+
}
16+
for _, goodTime := range standardTimeFormats {
17+
if dummyTime.UnmarshalText([]byte(goodTime)) != nil {
18+
t.Error("expected " + goodTime + " to be marshal-able.")
19+
}
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+
}
38+
}
39+
40+
badTime := "this is a bad time, isn't it?"
41+
if dummyTime.UnmarshalText([]byte(badTime)) == nil {
42+
t.Error("expected " + badTime + "to be unmarshal-able.")
43+
}
44+
}

0 commit comments

Comments
 (0)