Support for timestamp with timezone containing offset as ID

This commit is contained in:
DanB
2017-11-14 12:10:13 +01:00
parent 239b5678dc
commit f43dd4d35d
2 changed files with 19 additions and 0 deletions

View File

@@ -165,6 +165,7 @@ func ParseTimeDetectLayout(tmStr string, timezone string) (time.Time, error) {
rfc3339Rule := regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.+$`)
sqlRule := regexp.MustCompile(`^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}$`)
gotimeRule := regexp.MustCompile(`^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.?\d*\s[+,-]\d+\s\w+$`)
gotimeRule2 := regexp.MustCompile(`^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.?\d*\s[+,-]\d+\s[+,-]\d+$`)
fsTimestamp := regexp.MustCompile(`^\d{16}$`)
astTimestamp := regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d*[+,-]\d+$`)
unixTimestampRule := regexp.MustCompile(`^\d{10}$`)
@@ -179,6 +180,8 @@ func ParseTimeDetectLayout(tmStr string, timezone string) (time.Time, error) {
return time.Parse(time.RFC3339, tmStr)
case gotimeRule.MatchString(tmStr):
return time.Parse("2006-01-02 15:04:05.999999999 -0700 MST", tmStr)
case gotimeRule2.MatchString(tmStr):
return time.Parse("2006-01-02 15:04:05.999999999 -0700 -0700", tmStr)
case sqlRule.MatchString(tmStr):
return time.ParseInLocation("2006-01-02 15:04:05", tmStr, loc)
case fsTimestamp.MatchString(tmStr):

View File

@@ -153,6 +153,22 @@ func TestParseTimeDetectLayout(t *testing.T) {
if err == nil {
t.Errorf("Expecting error")
}
loc, err := time.LoadLocation("Asia/Kabul")
if err != nil {
t.Error(err)
}
expectedTime = time.Date(2013, 12, 30, 15, 0, 1, 0, loc)
goTmStr2 := "2013-12-30 15:00:01 +0430 +0430"
goTm, err = ParseTimeDetectLayout(goTmStr2, "")
if err != nil {
t.Error(err)
} else if !goTm.Equal(expectedTime) {
t.Errorf("Unexpected time parsed: %v, expecting: %v", goTm, expectedTime)
}
//goTmStr2 = "2013-12-30 15:00:01 +0430"
//if _, err = ParseTimeDetectLayout(goTmStr2, ""); err != nil {
// t.Errorf("Expecting error")
//}
fsTmstampStr := "1394291049287234"
fsTm, err := ParseTimeDetectLayout(fsTmstampStr, "")
expectedTime = time.Date(2014, 3, 8, 15, 4, 9, 287234000, time.UTC)