Fix tests for local-time issues

This commit is contained in:
Aaron Raddon 2017-04-23 11:24:32 -07:00
parent 65d52c8723
commit f1f37e5e29

View File

@ -77,6 +77,13 @@ func assertf(t *testing.T, result bool, f string, v ...interface{}) {
func TestParse(t *testing.T) { func TestParse(t *testing.T) {
mstZone, err := time.LoadLocation("America/Denver")
assert(t, err == nil)
n := time.Now()
if fmt.Sprintf("%v", n) == fmt.Sprintf("%v", n.In(mstZone)) {
t.Logf("you are testing and in MST %v", mstZone)
}
zeroTime := time.Time{}.Unix() zeroTime := time.Time{}.Unix()
ts, err := ParseAny("INVALID") ts, err := ParseAny("INVALID")
assert(t, ts.Unix() == zeroTime) assert(t, ts.Unix() == zeroTime)
@ -91,16 +98,35 @@ func TestParse(t *testing.T) {
// UnixDate = "Mon Jan _2 15:04:05 MST 2006" // UnixDate = "Mon Jan _2 15:04:05 MST 2006"
ts, err = ParseAny("Mon Jan 2 15:04:05 MST 2006") ts, err = ParseAny("Mon Jan 2 15:04:05 MST 2006")
assert(t, "2006-01-02 22:04:05 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC))) // The time-zone of local machine appears to effect the results?
// Why is the zone/offset for MST not always the same depending on local time zone?
// Why is offset = 0 at all?
// https://play.golang.org/p/lSOT9AeNxz
// https://github.com/golang/go/issues/18012
_, offset := ts.Zone()
// WHY doesn't this work? seems to be underlying issue in go not finding
// the MST?
//assert(t, offset != 0, "Should have found zone/offset !=0 ", offset)
if offset == 0 {
assert(t, "2006-01-02 15:04:05 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
} else {
// for some reason i don't understand the offset is != 0
// IF you have your local time-zone set to US MST?
assert(t, "2006-01-02 22:04:05 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
}
// RubyDate = "Mon Jan 02 15:04:05 -0700 2006" // RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
ts, err = ParseAny("Mon Jan 02 15:04:05 -0700 2006") ts, err = ParseAny("Mon Jan 02 15:04:05 -0700 2006")
// Are we SURE this is right time? assertf(t, "2006-01-02 22:04:05 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)), "%v", ts.In(time.UTC))
assert(t, "2006-01-02 15:04:05 -0700 MST" == fmt.Sprintf("%v", ts))
// RFC850 = "Monday, 02-Jan-06 15:04:05 MST" // RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
ts, err = ParseAny("Monday, 02-Jan-06 15:04:05 MST") ts, err = ParseAny("Monday, 02-Jan-06 15:04:05 MST")
assert(t, "2006-01-02 15:04:05 -0700 MST" == fmt.Sprintf("%v", ts)) _, offset = ts.Zone()
if offset == 0 {
assert(t, "2006-01-02 15:04:05 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)), ts.In(time.UTC))
} else {
assert(t, "2006-01-02 22:04:05 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)), ts.In(time.UTC))
}
// Another weird one, year on the end after UTC? // Another weird one, year on the end after UTC?
ts, err = ParseAny("Mon Aug 10 15:44:11 UTC+0100 2015") ts, err = ParseAny("Mon Aug 10 15:44:11 UTC+0100 2015")
@ -114,7 +140,12 @@ func TestParse(t *testing.T) {
ts, err = ParseAny("Mon, 02 Jan 2006 15:04:05 MST") ts, err = ParseAny("Mon, 02 Jan 2006 15:04:05 MST")
assertf(t, err == nil, "%v", err) assertf(t, err == nil, "%v", err)
assert(t, "2006-01-02 22:04:05 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC))) _, offset = ts.Zone()
if offset == 0 {
assert(t, "2006-01-02 15:04:05 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)), ts.In(time.UTC))
} else {
assert(t, "2006-01-02 22:04:05 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)), ts.In(time.UTC))
}
ts, err = ParseAny("Mon, 02 Jan 2006 15:04:05 -0700") ts, err = ParseAny("Mon, 02 Jan 2006 15:04:05 -0700")
assertf(t, err == nil, "%v", err) assertf(t, err == nil, "%v", err)