parse yyyyMMddhhmmss digit format closes #65

This commit is contained in:
Aaron Raddon 2018-07-21 14:28:33 -07:00
parent 089f77b1d9
commit a95177ee19
4 changed files with 207 additions and 192 deletions

View File

@ -157,9 +157,12 @@ var examples = []string{
"2014.03",
// yyyymmdd and similar
"20140601",
// unix seconds, ms
"20140722105203",
// unix seconds, ms, micro, nano
"1332151919",
"1384216367189",
"1384216367111222",
"1384216367111222333",
}
var (
@ -194,9 +197,9 @@ func main() {
}
/*
+-------------------------------------------------------+----------------------------------------+
+-------------------------------------------------------+-----------------------------------------+
| Input | Parsed, and Output as %v |
+-------------------------------------------------------+----------------------------------------+
+-------------------------------------------------------+-----------------------------------------+
| May 8, 2009 5:57:51 PM | 2009-05-08 17:57:51 +0000 UTC |
| oct 7, 1970 | 1970-10-07 00:00:00 +0000 UTC |
| oct 7, '70 | 1970-10-07 00:00:00 +0000 UTC |
@ -276,9 +279,12 @@ func main() {
| 08.21.71 | 1971-08-21 00:00:00 +0000 UTC |
| 2014.03 | 2014-03-01 00:00:00 +0000 UTC |
| 20140601 | 2014-06-01 00:00:00 +0000 UTC |
| 20140722105203 | 2014-07-22 10:52:03 +0000 UTC |
| 1332151919 | 2012-03-19 10:11:59 +0000 UTC |
| 1384216367189 | 2013-11-12 00:32:47.189 +0000 UTC |
+-------------------------------------------------------+----------------------------------------+
| 1384216367111222 | 2013-11-12 00:32:47.111222 +0000 UTC |
| 1384216367111222333 | 2013-11-12 00:32:47.111222333 +0000 UTC |
+-------------------------------------------------------+-----------------------------------------+
*/
```

View File

@ -96,9 +96,12 @@ var examples = []string{
"2014.03",
// yyyymmdd and similar
"20140601",
// unix seconds, ms
"20140722105203",
// unix seconds, ms, micro, nano
"1332151919",
"1384216367189",
"1384216367111222",
"1384216367111222333",
}
var (
@ -133,9 +136,9 @@ func main() {
}
/*
+-------------------------------------------------------+----------------------------------------+
+-------------------------------------------------------+-----------------------------------------+
| Input | Parsed, and Output as %v |
+-------------------------------------------------------+----------------------------------------+
+-------------------------------------------------------+-----------------------------------------+
| May 8, 2009 5:57:51 PM | 2009-05-08 17:57:51 +0000 UTC |
| oct 7, 1970 | 1970-10-07 00:00:00 +0000 UTC |
| oct 7, '70 | 1970-10-07 00:00:00 +0000 UTC |
@ -215,7 +218,10 @@ func main() {
| 08.21.71 | 1971-08-21 00:00:00 +0000 UTC |
| 2014.03 | 2014-03-01 00:00:00 +0000 UTC |
| 20140601 | 2014-06-01 00:00:00 +0000 UTC |
| 20140722105203 | 2014-07-22 10:52:03 +0000 UTC |
| 1332151919 | 2012-03-19 10:11:59 +0000 UTC |
| 1384216367189 | 2013-11-12 00:32:47.189 +0000 UTC |
+-------------------------------------------------------+----------------------------------------+
| 1384216367111222 | 2013-11-12 00:32:47.111222 +0000 UTC |
| 1384216367111222333 | 2013-11-12 00:32:47.111222333 +0000 UTC |
+-------------------------------------------------------+-----------------------------------------+
*/

View File

@ -1413,26 +1413,37 @@ iterRunes:
switch p.stateDate {
case dateDigit:
// unixy timestamps ish
// 1499979655583057426 nanoseconds
// 1499979795437000 micro-seconds
// 1499979795437 milliseconds
// 1384216367189
// 1332151919 seconds
// 20140601 yyyymmdd
// 2014 yyyy
// example ct type
// 1499979655583057426 19 nanoseconds
// 1499979795437000 16 micro-seconds
// 20180722105203 14 yyyyMMddhhmmss
// 1499979795437 13 milliseconds
// 1332151919 10 seconds
// 20140601 8 yyyymmdd
// 2014 4 yyyy
t := time.Time{}
if len(datestr) > len("1499979795437000") {
if len(datestr) == len("1499979655583057426") { // 19
// nano-seconds
if nanoSecs, err := strconv.ParseInt(datestr, 10, 64); err == nil {
t = time.Unix(0, nanoSecs)
}
} else if len(datestr) > len("1499979795437") {
} else if len(datestr) == len("1499979795437000") { // 16
// micro-seconds
if microSecs, err := strconv.ParseInt(datestr, 10, 64); err == nil {
t = time.Unix(0, microSecs*1000)
}
} else if len(datestr) > len("1332151919") {
} else if len(datestr) == len("yyyyMMddhhmmss") { // 14
// yyyyMMddhhmmss
p.format = []byte("20060102150405")
return p, nil
} else if len(datestr) == len("1332151919000") { // 13
if miliSecs, err := strconv.ParseInt(datestr, 10, 64); err == nil {
t = time.Unix(0, miliSecs*1000*1000)
}
} else if len(datestr) == len("1332151919") { //10
if secs, err := strconv.ParseInt(datestr, 10, 64); err == nil {
t = time.Unix(secs, 0)
}
} else if len(datestr) == len("20140601") {
p.format = []byte("20060102")
return p, nil
@ -1442,16 +1453,6 @@ iterRunes:
} else if len(datestr) < 4 {
return nil, fmt.Errorf("unrecognized format, to short %v", datestr)
}
if t.IsZero() {
if secs, err := strconv.ParseInt(datestr, 10, 64); err == nil {
if secs > 0 {
// Now, for unix-seconds we aren't going to guess a lot
// nothing before unix-epoch
t = time.Unix(secs, 0)
p.t = &t
}
}
}
if !t.IsZero() {
if loc == nil {
p.t = &t

View File

@ -322,6 +322,8 @@ var testInputs = []dateTest{
// yyyymmdd and similar
{in: "2014", out: "2014-01-01 00:00:00 +0000 UTC"},
{in: "20140601", out: "2014-06-01 00:00:00 +0000 UTC"},
{in: "20140722105203", out: "2014-07-22 10:52:03 +0000 UTC"},
// all digits: unix secs, ms etc
{in: "1332151919", out: "2012-03-19 10:11:59 +0000 UTC"},
{in: "1332151919", out: "2012-03-19 10:11:59 +0000 UTC", loc: "America/Denver"},