dateparse/parseany_test.go

436 lines
16 KiB
Go
Raw Normal View History

2014-04-21 10:56:17 +08:00
package dateparse
import (
2014-04-25 08:51:56 +08:00
"fmt"
2017-04-23 10:57:16 +08:00
"runtime"
2014-04-21 10:56:17 +08:00
"testing"
"time"
)
/*
2014-05-21 13:08:25 +08:00
ANSIC = "Mon Jan _2 15:04:05 2006" x
UnixDate = "Mon Jan _2 15:04:05 MST 2006" x
RubyDate = "Mon Jan 02 15:04:05 -0700 2006" x
RFC822 = "02 Jan 06 15:04 MST" x
2014-04-21 10:56:17 +08:00
RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
RFC3339 = "2006-01-02T15:04:05Z07:00"
RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
Kitchen = "3:04PM"
// Handy time stamps.
Stamp = "Jan _2 15:04:05"
StampMilli = "Jan _2 15:04:05.000"
StampMicro = "Jan _2 15:04:05.000000"
StampNano = "Jan _2 15:04:05.000000000"
// unix etc
1398045032 time.Now().Unix()
1398045078199135196 time.Now().UnixNano()
// Others
"May 8, 2009 5:57:51 PM"
Apr 7, 2014 4:58:55 PM
2014-07-11 06:25:23 +08:00
2014/07/10 06:55:38.156283
2014-04-21 10:56:17 +08:00
03/19/2012 10:11:59
2014-05-12 04:33:13 +08:00
04/2/2014 03:00:37
2014-04-21 10:56:17 +08:00
3/1/2014
10/13/2014
01/02/2006
2015-08-19 06:50:04 +08:00
20140601
2014-04-21 10:56:17 +08:00
2016-03-14 00:00:00.000
2006-01-02
2014-05-11 23:42:56 +08:00
2014-05-11 08:20:13,787 // i couldn't find parser for this in go?
2014-04-21 10:56:17 +08:00
2016-03-02 05:25:00 +08:00
// only day or year level resolution
2006-01
2006
2014-04-21 10:56:17 +08:00
*/
2017-04-23 10:57:16 +08:00
func tt(t *testing.T, result bool, cd int, args ...interface{}) {
fn := func() {
t.Errorf("! Failure")
if len(args) > 0 {
t.Error("!", " -", fmt.Sprint(args...))
}
}
if !result {
_, file, line, _ := runtime.Caller(cd + 1)
t.Errorf("%s:%d", file, line)
fn()
t.FailNow()
}
}
2014-04-21 10:56:17 +08:00
2017-04-23 10:57:16 +08:00
func assert(t *testing.T, result bool, v ...interface{}) {
tt(t, result, 1, v...)
}
func assertf(t *testing.T, result bool, f string, v ...interface{}) {
tt(t, result, 1, fmt.Sprintf(f, v...))
2014-04-21 10:56:17 +08:00
}
func TestParse(t *testing.T) {
2014-05-07 12:15:43 +08:00
2017-04-24 02:24:32 +08:00
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)
}
2014-06-16 02:18:20 +08:00
zeroTime := time.Time{}.Unix()
ts, err := ParseAny("INVALID")
2017-04-23 10:57:16 +08:00
assert(t, ts.Unix() == zeroTime)
assert(t, err != nil)
2015-08-11 01:54:01 +08:00
2014-06-16 02:18:20 +08:00
ts, err = ParseAny("May 8, 2009 5:57:51 PM")
2017-04-23 10:57:16 +08:00
assert(t, "2009-05-08 17:57:51 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2014-04-26 07:59:10 +08:00
2014-05-21 13:53:52 +08:00
// ANSIC = "Mon Jan _2 15:04:05 2006"
ts, err = ParseAny("Mon Jan 2 15:04:05 2006")
2017-04-23 10:57:16 +08:00
assert(t, "2006-01-02 15:04:05 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2014-05-21 13:53:52 +08:00
// UnixDate = "Mon Jan _2 15:04:05 MST 2006"
ts, err = ParseAny("Mon Jan 2 15:04:05 MST 2006")
2017-04-24 02:24:32 +08:00
// 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)))
}
2014-05-21 13:53:52 +08:00
// RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
ts, err = ParseAny("Mon Jan 02 15:04:05 -0700 2006")
2017-04-24 02:24:32 +08:00
assertf(t, "2006-01-02 22:04:05 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)), "%v", ts.In(time.UTC))
2014-05-21 13:53:52 +08:00
2014-06-16 02:18:20 +08:00
// RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
ts, err = ParseAny("Monday, 02-Jan-06 15:04:05 MST")
2017-04-24 02:24:32 +08:00
_, 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))
}
2014-06-16 02:18:20 +08:00
2015-08-11 01:54:01 +08:00
// Another weird one, year on the end after UTC?
ts, err = ParseAny("Mon Aug 10 15:44:11 UTC+0100 2015")
2017-04-23 10:57:16 +08:00
assert(t, err == nil)
assert(t, "2015-08-10 15:44:11 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2015-08-11 01:54:01 +08:00
2015-07-04 01:27:48 +08:00
// Easily the worst Date format i have ever seen
// "Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)"
ts, err = ParseAny("Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)")
2017-04-23 10:57:16 +08:00
assert(t, "2015-07-03 17:04:07 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2015-07-04 01:27:48 +08:00
ts, err = ParseAny("Mon, 02 Jan 2006 15:04:05 MST")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
2017-04-24 02:24:32 +08:00
_, 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")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2006-01-02 22:04:05 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2017-02-06 02:04:03 +08:00
// not sure if this is anything close to a standard, never seen it before
ts, err = ParseAny("12 Feb 2006, 19:17")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2006-02-12 19:17:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2017-02-06 02:04:03 +08:00
2016-09-13 22:40:04 +08:00
ts, err = ParseAny("2015-02-18 00:12:00 +0000 GMT")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2015-02-18 00:12:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2016-09-13 22:40:04 +08:00
2015-04-17 02:37:50 +08:00
// Golang Native Format
ts, err = ParseAny("2015-02-18 00:12:00 +0000 UTC")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2015-02-18 00:12:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2014-05-21 13:53:52 +08:00
2014-07-11 06:25:23 +08:00
//---------------------------------------------
// mm/dd/yyyy ?
2014-04-21 10:56:17 +08:00
2014-05-07 12:15:43 +08:00
ts, err = ParseAny("3/31/2014")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2014-03-31 00:00:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2014-04-28 04:26:17 +08:00
2014-05-07 12:15:43 +08:00
ts, err = ParseAny("03/31/2014")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2014-03-31 00:00:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2014-04-28 04:26:17 +08:00
// what type of date is this? 08/21/71
ts, err = ParseAny("08/21/71")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "1971-08-21 00:00:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2016-01-06 03:34:31 +08:00
// m/d/yy
ts, err = ParseAny("8/1/71")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "1971-08-01 00:00:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2016-01-06 03:34:31 +08:00
2014-05-07 12:15:43 +08:00
ts, err = ParseAny("4/8/2014 22:05")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2014-04-08 22:05:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2014-05-07 12:15:43 +08:00
ts, err = ParseAny("04/08/2014 22:05")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2014-04-08 22:05:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2014-04-28 05:33:33 +08:00
2017-02-14 10:39:03 +08:00
ts, err = ParseAny("04/2/2014 4:00:51")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2014-04-02 04:00:51 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2014-05-12 04:33:13 +08:00
2017-02-14 08:49:48 +08:00
ts, err = ParseAny("8/8/1965 01:00:01 PM")
2017-04-23 10:57:16 +08:00
assert(t, "1965-08-08 13:00:01 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2017-02-14 08:49:48 +08:00
ts, err = ParseAny("8/8/1965 12:00:01 AM")
2017-04-23 10:57:16 +08:00
assert(t, "1965-08-08 00:00:01 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2017-02-14 08:49:48 +08:00
ts, err = ParseAny("8/8/1965 01:00 PM")
2017-04-23 10:57:16 +08:00
assert(t, "1965-08-08 13:00:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2017-02-14 10:39:03 +08:00
ts, err = ParseAny("8/8/1965 1:00 PM")
2017-04-23 10:57:16 +08:00
assert(t, "1965-08-08 13:00:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2017-02-14 08:49:48 +08:00
ts, err = ParseAny("8/8/1965 12:00 AM")
2017-04-23 10:57:16 +08:00
assert(t, "1965-08-08 00:00:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2015-10-01 03:09:08 +08:00
2014-05-12 04:33:13 +08:00
ts, err = ParseAny("4/02/2014 03:00:51")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2014-04-02 03:00:51 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2014-05-12 04:33:13 +08:00
2014-07-11 06:25:23 +08:00
ts, err = ParseAny("03/19/2012 10:11:59")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2012-03-19 10:11:59 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2014-04-26 07:59:10 +08:00
2014-07-11 06:25:23 +08:00
ts, err = ParseAny("03/19/2012 10:11:59.3186369")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2012-03-19 10:11:59.3186369 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2014-07-11 06:25:23 +08:00
//---------------------------------------------
// yyyy/mm/dd ?
ts, err = ParseAny("2014/3/31")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2014-03-31 00:00:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2014-07-11 06:25:23 +08:00
ts, err = ParseAny("2014/03/31")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2014-03-31 00:00:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2014-07-11 06:25:23 +08:00
ts, err = ParseAny("2014/4/8 22:05")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2014-04-08 22:05:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2014-07-11 06:25:23 +08:00
ts, err = ParseAny("2014/04/08 22:05")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2014-04-08 22:05:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2014-07-11 06:25:23 +08:00
ts, err = ParseAny("2014/04/2 03:00:51")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2014-04-02 03:00:51 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2014-07-11 06:25:23 +08:00
ts, err = ParseAny("2014/4/02 03:00:51")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2014-04-02 03:00:51 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2014-07-11 06:25:23 +08:00
ts, err = ParseAny("2012/03/19 10:11:59")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2012-03-19 10:11:59 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2014-07-11 06:25:23 +08:00
ts, err = ParseAny("2012/03/19 10:11:59.3186369")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2012-03-19 10:11:59.3186369 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2014-07-11 06:25:23 +08:00
//---------------------------------------------
// yyyy-mm-dd ?
2014-05-12 01:22:53 +08:00
ts, err = ParseAny("2009-08-12T22:15:09-07:00")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2009-08-13 05:15:09 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2017-06-26 10:09:06 +08:00
ts, err = ParseAny("2009-08-12T22:15:09.123-07:00")
assertf(t, err == nil, "%v", err)
assertf(t, "2009-08-13 05:15:09.123 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)), "%v", ts.In(time.UTC))
2014-04-25 08:51:56 +08:00
ts, err = ParseAny("2009-08-12T22:15Z")
assertf(t, err == nil, "%v", err)
assertf(t, "2009-08-12 22:15:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)), "%v", ts.In(time.UTC))
2016-07-13 23:33:23 +08:00
ts, err = ParseAny("2009-08-12T22:15:09Z")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
2017-06-26 10:09:06 +08:00
assertf(t, "2009-08-12 22:15:09 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)), "%v", ts.In(time.UTC))
2016-07-13 23:33:23 +08:00
2017-05-04 02:15:04 +08:00
ts, err = ParseAny("2009-08-12T22:15:09.99Z")
assertf(t, err == nil, "%v", err)
assert(t, "2009-08-12 22:15:09.99 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
ts, err = ParseAny("2009-08-12T22:15:09.9999Z")
assertf(t, err == nil, "%v", err)
assert(t, "2009-08-12 22:15:09.9999 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
ts, err = ParseAny("2009-08-12T22:15:09.99999999Z")
assertf(t, err == nil, "%v", err)
assert(t, "2009-08-12 22:15:09.99999999 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
ts, err = ParseAny("2009-08-12T22:15:09.123")
assertf(t, err == nil, "%v", err)
assert(t, "2009-08-12 22:15:09.123 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2017-06-26 10:09:06 +08:00
ts, err = ParseAny("2009-08-12T22:15:09.123456")
assertf(t, err == nil, "%v", err)
assert(t, "2009-08-12 22:15:09.123456 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2017-05-04 02:15:04 +08:00
ts, err = ParseAny("2009-08-12T22:15:09.12")
assertf(t, err == nil, "%v", err)
assert(t, "2009-08-12 22:15:09.12 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
ts, err = ParseAny("2009-08-12T22:15:09.1")
assertf(t, err == nil, "%v", err)
assert(t, "2009-08-12 22:15:09.1 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2015-06-26 05:09:39 +08:00
ts, err = ParseAny("2009-08-12T22:15:09")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2009-08-12 22:15:09 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2015-06-26 05:09:39 +08:00
2014-05-07 03:34:24 +08:00
ts, err = ParseAny("2014-04-26 17:24:37.3186369")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2014-04-26 17:24:37.3186369 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2014-05-07 03:32:49 +08:00
2015-06-26 08:24:55 +08:00
// 2015-06-25 01:25:37.115208593 +0000 UTC
ts, err = ParseAny("2012-08-03 18:31:59.257000000 +0000 UTC")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2012-08-03 18:31:59.257 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2015-06-26 08:24:55 +08:00
2015-10-01 03:09:08 +08:00
ts, err = ParseAny("2015-09-30 18:48:56.35272715 +0000 UTC")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2015-09-30 18:48:56.35272715 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2015-10-01 03:09:08 +08:00
2017-01-31 09:18:48 +08:00
ts, err = ParseAny("2017-01-27 00:07:31.945167")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2017-01-27 00:07:31.945167 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2017-01-31 09:18:48 +08:00
2014-05-12 09:08:56 +08:00
ts, err = ParseAny("2012-08-03 18:31:59.257000000")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2012-08-03 18:31:59.257 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2014-05-12 09:08:56 +08:00
2014-05-12 09:22:16 +08:00
ts, err = ParseAny("2013-04-01 22:43:22")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2013-04-01 22:43:22 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2014-05-12 09:22:16 +08:00
2014-05-07 09:14:58 +08:00
ts, err = ParseAny("2014-04-26 17:24:37.123")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2014-04-26 17:24:37.123 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2014-05-07 09:14:58 +08:00
2017-04-12 06:30:42 +08:00
ts, err = ParseAny("2014-04-26 17:24:37.123456 UTC")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2014-04-26 17:24:37.123456 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2017-04-12 06:30:42 +08:00
ts, err = ParseAny("2014-04-26 17:24:37.123 UTC")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2014-04-26 17:24:37.123 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2017-04-12 06:30:42 +08:00
ts, err = ParseAny("2014-04-26 17:24:37.12 UTC")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2014-04-26 17:24:37.12 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2017-04-12 06:30:42 +08:00
ts, err = ParseAny("2014-04-26 17:24:37.1 UTC")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2014-04-26 17:24:37.1 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2017-04-12 06:30:42 +08:00
ts, err = ParseAny("2014-04-26 17:24:37.123 +0800")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2014-04-26 09:24:37.123 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2017-04-12 06:30:42 +08:00
ts, err = ParseAny("2014-04-26 17:24:37.123 -0800")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2014-04-27 01:24:37.123 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2017-04-12 06:30:42 +08:00
ts, err = ParseAny("2014-04-26 17:24:37.123456 +0800")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2014-04-26 09:24:37.123456 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2017-04-12 06:30:42 +08:00
ts, err = ParseAny("2014-04-26 17:24:37.123456 -0800")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2014-04-27 01:24:37.123456 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2017-04-12 06:30:42 +08:00
2014-12-03 06:57:00 +08:00
ts, err = ParseAny("2014-12-16 06:20:00 UTC")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2014-12-16 06:20:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2016-09-13 22:40:04 +08:00
ts, err = ParseAny("2014-12-16 06:20:00 GMT")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2014-12-16 06:20:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2014-12-03 06:57:00 +08:00
2014-05-07 09:14:58 +08:00
ts, err = ParseAny("2014-04-26 05:24:37 PM")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2014-04-26 17:24:37 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2014-05-07 12:15:43 +08:00
ts, err = ParseAny("2014-04-26")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2014-04-26 00:00:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2014-05-11 23:28:23 +08:00
2016-03-02 05:25:00 +08:00
ts, err = ParseAny("2014-04")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2014-04-01 00:00:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2016-03-02 05:25:00 +08:00
2014-05-11 23:28:23 +08:00
ts, err = ParseAny("2014-05-11 08:20:13,787")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2014-05-11 08:20:13.787 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2014-07-11 06:25:23 +08:00
2017-06-15 04:56:12 +08:00
ts, err = ParseAny("2014-05-11 08:20:13 +00:00")
assertf(t, err == nil, "%v", err)
assert(t, "2014-05-11 08:20:13 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2017-07-12 02:00:40 +08:00
ts, err = ParseAny("2016-06-21T19:55:00+01:00")
assertf(t, err == nil, "%v", err)
assert(t, "2016-06-21 18:55:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
ts, err = ParseAny("2016-06-21T19:55:00.799+01:00")
assertf(t, err == nil, "%v", err)
assert(t, "2016-06-21 18:55:00.799 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2017-06-15 04:56:12 +08:00
ts, err = ParseAny("2014-05-11 08:20:13 +0000")
assertf(t, err == nil, "%v", err)
assert(t, "2014-05-11 08:20:13 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2016-03-02 05:25:00 +08:00
// yyyymmdd and similar
ts, err = ParseAny("2014")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2014-01-01 00:00:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2016-03-02 05:25:00 +08:00
2015-08-19 06:50:04 +08:00
ts, err = ParseAny("20140601")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2014-06-01 00:00:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2015-08-19 06:50:04 +08:00
2014-07-11 06:25:23 +08:00
ts, err = ParseAny("1332151919")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2012-03-19 10:11:59 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2014-07-11 06:25:23 +08:00
2014-10-08 09:30:17 +08:00
ts, err = ParseAny("1384216367189")
2017-04-23 10:57:16 +08:00
assertf(t, err == nil, "%v", err)
assert(t, "2013-11-12 00:32:47.189 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2014-10-08 09:30:17 +08:00
2014-05-11 23:28:23 +08:00
}
2014-05-21 13:53:52 +08:00
// func TestWIP(t *testing.T) {
// ts, err := ParseAny("2013-04-01 22:43:22")
2017-04-23 10:57:16 +08:00
// assertf(t, err == nil, "%v", err)
// assert(t, "2013-04-01 22:43:22 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
2014-05-21 13:53:52 +08:00
// }