From de724897dbd164c86669fc00b4dc297d4ab861fd Mon Sep 17 00:00:00 2001 From: John Beisley Date: Wed, 9 Oct 2013 22:23:57 +0100 Subject: [PATCH] Make datetime tests more robust against DST/timezone changes. --- soap/types.go | 13 ++++++++++--- soap/types_test.go | 16 +++++++++++----- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/soap/types.go b/soap/types.go index 9ce85f1..ad28422 100644 --- a/soap/types.go +++ b/soap/types.go @@ -10,6 +10,13 @@ import ( "unicode/utf8" ) +var ( + // localLoc acts like time.Local for this package, but is faked out by the + // unit tests to ensure that things stay constant (especially when running + // this test in a place where local time is UTC which might mask bugs). + localLoc = time.Local +) + // MarshalFixed14_4 marshals float64 to SOAP "fixed.14.4" type. func MarshalFixed14_4(v float64) (string, error) { if v >= 1e14 || v <= -1e14 { @@ -53,7 +60,7 @@ func UnmarshalChar(s string) (rune, error) { // MarshalDate marshals time.Time to SOAP "date" type. Note that this converts // to local time, and discards the time-of-day components. func MarshalDate(v time.Time) (string, error) { - return v.Local().Format("2006-01-02"), nil + return v.In(localLoc).Format("2006-01-02"), nil } var dateFmts = []string{"2006-01-02", "20060102"} @@ -62,7 +69,7 @@ var dateFmts = []string{"2006-01-02", "20060102"} // date as midnight in the local time zone. func UnmarshalDate(s string) (time.Time, error) { for _, f := range dateFmts { - if t, err := time.ParseInLocation(f, s, time.Local); err == nil { + if t, err := time.ParseInLocation(f, s, localLoc); err == nil { return t, nil } } @@ -187,7 +194,7 @@ func UnmarshalTimeOfDayTz(s string) (TimeOfDay, error) { // MarshalDatetime marshals time.Time to SOAP "date" type. Note that this // converts to local time. func MarshalDatetime(v time.Time) (string, error) { - return v.Local().Format("2006-01-02T15:04:05"), nil + return v.In(localLoc).Format("2006-01-02T15:04:05"), nil } // UnmarshalDatetime unmarshals time.Time from the SOAP "dateTime" type. This diff --git a/soap/types_test.go b/soap/types_test.go index e55fbf9..50c927d 100644 --- a/soap/types_test.go +++ b/soap/types_test.go @@ -122,6 +122,12 @@ func Test(t *testing.T) { const time01 time.Duration = (1 * 3600) * time.Second const time235959 time.Duration = (23*3600 + 59*60 + 59) * time.Second + // Fake out the local time for the implementation. + localLoc = time.FixedZone("Fake/Local", 6*3600) + defer func() { + localLoc = time.Local + }() + tests := []testCase{ // fixed.14.4 {str: "0.0000", value: Fixed14_4Test(0)}, @@ -142,8 +148,8 @@ func Test(t *testing.T) { {str: "", value: CharTest(0), wantMarshalErr: true, wantUnmarshalErr: true}, // date - {str: "2013-10-08", value: DateTest{time.Date(2013, 10, 8, 0, 0, 0, 0, time.Local)}, tag: "no:dateTime"}, - {str: "20131008", value: DateTest{time.Date(2013, 10, 8, 0, 0, 0, 0, time.Local)}, noMarshal: true, tag: "no:dateTime"}, + {str: "2013-10-08", value: DateTest{time.Date(2013, 10, 8, 0, 0, 0, 0, localLoc)}, tag: "no:dateTime"}, + {str: "20131008", value: DateTest{time.Date(2013, 10, 8, 0, 0, 0, 0, localLoc)}, noMarshal: true, tag: "no:dateTime"}, {str: "2013-10-08T10:30:50", value: DateTest{}, wantUnmarshalErr: true, noMarshal: true, tag: "no:dateTime"}, {str: "2013-10-08T10:30:50Z", value: DateTest{}, wantUnmarshalErr: true, noMarshal: true, tag: "no:dateTime"}, {str: "", value: DateTest{}, wantMarshalErr: true, wantUnmarshalErr: true, noMarshal: true}, @@ -190,9 +196,9 @@ func Test(t *testing.T) { {str: "01:02:03-0123", value: TimeOfDayTzTest{TimeOfDay{time010203, true, -(3600 + 23*60)}}, noMarshal: true}, // datetime - {str: "2013-10-08T00:00:00", value: DatetimeTest{time.Date(2013, 10, 8, 0, 0, 0, 0, time.Local)}}, - {str: "20131008", value: DatetimeTest{time.Date(2013, 10, 8, 0, 0, 0, 0, time.Local)}, noMarshal: true}, - {str: "2013-10-08T10:30:50", value: DatetimeTest{time.Date(2013, 10, 8, 10, 30, 50, 0, time.Local)}}, + {str: "2013-10-08T00:00:00", value: DatetimeTest{time.Date(2013, 10, 8, 0, 0, 0, 0, localLoc)}}, + {str: "20131008", value: DatetimeTest{time.Date(2013, 10, 8, 0, 0, 0, 0, localLoc)}, noMarshal: true}, + {str: "2013-10-08T10:30:50", value: DatetimeTest{time.Date(2013, 10, 8, 10, 30, 50, 0, localLoc)}}, {str: "2013-10-08T10:30:50T", value: DatetimeTest{}, wantUnmarshalErr: true, noMarshal: true}, }