mirror of
https://github.com/araddon/dateparse.git
synced 2024-11-10 11:51:54 +08:00
merge master
This commit is contained in:
commit
303fede690
34
README.md
34
README.md
@ -9,7 +9,7 @@ Parse many date strings without knowing format in advance. Uses a scanner to re
|
|||||||
[![Build Status](https://travis-ci.org/araddon/dateparse.svg?branch=master)](https://travis-ci.org/araddon/dateparse)
|
[![Build Status](https://travis-ci.org/araddon/dateparse.svg?branch=master)](https://travis-ci.org/araddon/dateparse)
|
||||||
[![Go ReportCard](https://goreportcard.com/badge/araddon/dateparse)](https://goreportcard.com/report/araddon/dateparse)
|
[![Go ReportCard](https://goreportcard.com/badge/araddon/dateparse)](https://goreportcard.com/report/araddon/dateparse)
|
||||||
|
|
||||||
**MM/DD/YYYY VS DD/MM/YYYY** Right now this uses mm/dd/yyyy WHEN ambiguous will. Plan an alternate api for preferring dd/mm https://github.com/araddon/dateparse/issues/28
|
**MM/DD/YYYY VS DD/MM/YYYY** Right now this uses mm/dd/yyyy WHEN ambiguous if this is not desired behavior, use `ParseStrict` which will fail on ambiguous date strings.
|
||||||
|
|
||||||
**Timezones** The location your server is configured effects the results! See example or https://play.golang.org/p/IDHRalIyXh and last paragraph here https://golang.org/pkg/time/#Parse.
|
**Timezones** The location your server is configured effects the results! See example or https://play.golang.org/p/IDHRalIyXh and last paragraph here https://golang.org/pkg/time/#Parse.
|
||||||
|
|
||||||
@ -23,24 +23,6 @@ t, err := dateparse.ParseAny("3/1/2014")
|
|||||||
t, err := dateparse.ParseStrict("3/1/2014")
|
t, err := dateparse.ParseStrict("3/1/2014")
|
||||||
> returns error
|
> returns error
|
||||||
|
|
||||||
// Parse with Location, equivalent to time.ParseInLocation() timezone/offset
|
|
||||||
// rules. Using location arg, if timezone/offset info exists in the
|
|
||||||
// datestring, it uses the given location rules for any zone interpretation.
|
|
||||||
// That is, MST means one thing when using America/Denver and something else
|
|
||||||
// in other locations.
|
|
||||||
denverLoc, _ := time.LoadLocation("America/Denver")
|
|
||||||
t, err := dateparse.ParseIn("3/1/2014", denverLoc)
|
|
||||||
|
|
||||||
// Set Location to time.Local. Same as ParseIn Location but lazily uses
|
|
||||||
// the global time.Local variable for Location argument.
|
|
||||||
denverLoc, _ := time.LoadLocation("America/Denver")
|
|
||||||
// use time.Local global variable to store location
|
|
||||||
time.Local = denverLoc
|
|
||||||
t, err := dateparse.ParseLocal("3/1/2014")
|
|
||||||
// Equivalent to
|
|
||||||
t, err := dateparse.ParseIn("3/1/2014", time.Local)
|
|
||||||
|
|
||||||
|
|
||||||
// Return a string that represents the layout to parse the given date-time.
|
// Return a string that represents the layout to parse the given date-time.
|
||||||
layout, err := dateparse.ParseFormat("May 8, 2009 5:57:51 PM")
|
layout, err := dateparse.ParseFormat("May 8, 2009 5:57:51 PM")
|
||||||
> "Jan 2, 2006 3:04:05 PM"
|
> "Jan 2, 2006 3:04:05 PM"
|
||||||
@ -157,9 +139,12 @@ var examples = []string{
|
|||||||
"2014.03",
|
"2014.03",
|
||||||
// yyyymmdd and similar
|
// yyyymmdd and similar
|
||||||
"20140601",
|
"20140601",
|
||||||
// unix seconds, ms
|
"20140722105203",
|
||||||
|
// unix seconds, ms, micro, nano
|
||||||
"1332151919",
|
"1332151919",
|
||||||
"1384216367189",
|
"1384216367189",
|
||||||
|
"1384216367111222",
|
||||||
|
"1384216367111222333",
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -194,9 +179,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
+-------------------------------------------------------+----------------------------------------+
|
+-------------------------------------------------------+-----------------------------------------+
|
||||||
| Input | Parsed, and Output as %v |
|
| Input | Parsed, and Output as %v |
|
||||||
+-------------------------------------------------------+----------------------------------------+
|
+-------------------------------------------------------+-----------------------------------------+
|
||||||
| May 8, 2009 5:57:51 PM | 2009-05-08 17:57:51 +0000 UTC |
|
| 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, 1970 | 1970-10-07 00:00:00 +0000 UTC |
|
||||||
| oct 7, '70 | 1970-10-07 00:00:00 +0000 UTC |
|
| oct 7, '70 | 1970-10-07 00:00:00 +0000 UTC |
|
||||||
@ -276,9 +261,12 @@ func main() {
|
|||||||
| 08.21.71 | 1971-08-21 00:00:00 +0000 UTC |
|
| 08.21.71 | 1971-08-21 00:00:00 +0000 UTC |
|
||||||
| 2014.03 | 2014-03-01 00:00:00 +0000 UTC |
|
| 2014.03 | 2014-03-01 00:00:00 +0000 UTC |
|
||||||
| 20140601 | 2014-06-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 |
|
| 1332151919 | 2012-03-19 10:11:59 +0000 UTC |
|
||||||
| 1384216367189 | 2013-11-12 00:32:47.189 +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 |
|
||||||
|
+-------------------------------------------------------+-----------------------------------------+
|
||||||
*/
|
*/
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@ -96,9 +96,12 @@ var examples = []string{
|
|||||||
"2014.03",
|
"2014.03",
|
||||||
// yyyymmdd and similar
|
// yyyymmdd and similar
|
||||||
"20140601",
|
"20140601",
|
||||||
// unix seconds, ms
|
"20140722105203",
|
||||||
|
// unix seconds, ms, micro, nano
|
||||||
"1332151919",
|
"1332151919",
|
||||||
"1384216367189",
|
"1384216367189",
|
||||||
|
"1384216367111222",
|
||||||
|
"1384216367111222333",
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -133,9 +136,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
+-------------------------------------------------------+----------------------------------------+
|
+-------------------------------------------------------+-----------------------------------------+
|
||||||
| Input | Parsed, and Output as %v |
|
| Input | Parsed, and Output as %v |
|
||||||
+-------------------------------------------------------+----------------------------------------+
|
+-------------------------------------------------------+-----------------------------------------+
|
||||||
| May 8, 2009 5:57:51 PM | 2009-05-08 17:57:51 +0000 UTC |
|
| 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, 1970 | 1970-10-07 00:00:00 +0000 UTC |
|
||||||
| oct 7, '70 | 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 |
|
| 08.21.71 | 1971-08-21 00:00:00 +0000 UTC |
|
||||||
| 2014.03 | 2014-03-01 00:00:00 +0000 UTC |
|
| 2014.03 | 2014-03-01 00:00:00 +0000 UTC |
|
||||||
| 20140601 | 2014-06-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 |
|
| 1332151919 | 2012-03-19 10:11:59 +0000 UTC |
|
||||||
| 1384216367189 | 2013-11-12 00:32:47.189 +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 |
|
||||||
|
+-------------------------------------------------------+-----------------------------------------+
|
||||||
*/
|
*/
|
||||||
|
47
parseany.go
47
parseany.go
@ -943,6 +943,12 @@ iterRunes:
|
|||||||
p.offseti = i
|
p.offseti = i
|
||||||
case ' ':
|
case ' ':
|
||||||
// 17:57:51 MST 2009
|
// 17:57:51 MST 2009
|
||||||
|
p.tzlen = i - p.tzi
|
||||||
|
if p.tzlen == 4 {
|
||||||
|
p.set(p.tzi, " MST")
|
||||||
|
} else if p.tzlen == 3 {
|
||||||
|
p.set(p.tzi, "MST")
|
||||||
|
}
|
||||||
p.stateTime = timeWsAlphaWs
|
p.stateTime = timeWsAlphaWs
|
||||||
p.yeari = i + 1
|
p.yeari = i + 1
|
||||||
}
|
}
|
||||||
@ -1284,26 +1290,37 @@ iterRunes:
|
|||||||
switch p.stateDate {
|
switch p.stateDate {
|
||||||
case dateDigit:
|
case dateDigit:
|
||||||
// unixy timestamps ish
|
// unixy timestamps ish
|
||||||
// 1499979655583057426 nanoseconds
|
// example ct type
|
||||||
// 1499979795437000 micro-seconds
|
// 1499979655583057426 19 nanoseconds
|
||||||
// 1499979795437 milliseconds
|
// 1499979795437000 16 micro-seconds
|
||||||
// 1384216367189
|
// 20180722105203 14 yyyyMMddhhmmss
|
||||||
// 1332151919 seconds
|
// 1499979795437 13 milliseconds
|
||||||
// 20140601 yyyymmdd
|
// 1332151919 10 seconds
|
||||||
// 2014 yyyy
|
// 20140601 8 yyyymmdd
|
||||||
|
// 2014 4 yyyy
|
||||||
t := time.Time{}
|
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 {
|
if nanoSecs, err := strconv.ParseInt(datestr, 10, 64); err == nil {
|
||||||
t = time.Unix(0, nanoSecs)
|
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 {
|
if microSecs, err := strconv.ParseInt(datestr, 10, 64); err == nil {
|
||||||
t = time.Unix(0, microSecs*1000)
|
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 {
|
if miliSecs, err := strconv.ParseInt(datestr, 10, 64); err == nil {
|
||||||
t = time.Unix(0, miliSecs*1000*1000)
|
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") {
|
} else if len(datestr) == len("20140601") {
|
||||||
p.format = []byte("20060102")
|
p.format = []byte("20060102")
|
||||||
return p, nil
|
return p, nil
|
||||||
@ -1313,16 +1330,6 @@ iterRunes:
|
|||||||
} else if len(datestr) < 4 {
|
} else if len(datestr) < 4 {
|
||||||
return nil, fmt.Errorf("unrecognized format, to short %v", datestr)
|
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 !t.IsZero() {
|
||||||
if loc == nil {
|
if loc == nil {
|
||||||
p.t = &t
|
p.t = &t
|
||||||
|
@ -48,6 +48,7 @@ var testInputs = []dateTest{
|
|||||||
{in: "Thu May 8 17:57:51 MST 2009", out: "2009-05-08 17:57:51 +0000 UTC"},
|
{in: "Thu May 8 17:57:51 MST 2009", out: "2009-05-08 17:57:51 +0000 UTC"},
|
||||||
{in: "Thu May 8 17:57:51 PST 2009", out: "2009-05-08 17:57:51 +0000 UTC"},
|
{in: "Thu May 8 17:57:51 PST 2009", out: "2009-05-08 17:57:51 +0000 UTC"},
|
||||||
{in: "Thu May 08 17:57:51 PST 2009", out: "2009-05-08 17:57:51 +0000 UTC"},
|
{in: "Thu May 08 17:57:51 PST 2009", out: "2009-05-08 17:57:51 +0000 UTC"},
|
||||||
|
{in: "Thu May 08 17:57:51 CEST 2009", out: "2009-05-08 17:57:51 +0000 UTC"},
|
||||||
{in: "Thu May 08 05:05:07 PST 2009", out: "2009-05-08 05:05:07 +0000 UTC"},
|
{in: "Thu May 08 05:05:07 PST 2009", out: "2009-05-08 05:05:07 +0000 UTC"},
|
||||||
{in: "Thu May 08 5:5:7 PST 2009", out: "2009-05-08 05:05:07 +0000 UTC"},
|
{in: "Thu May 08 5:5:7 PST 2009", out: "2009-05-08 05:05:07 +0000 UTC"},
|
||||||
// ??
|
// ??
|
||||||
@ -327,6 +328,8 @@ var testInputs = []dateTest{
|
|||||||
// yyyymmdd and similar
|
// yyyymmdd and similar
|
||||||
{in: "2014", out: "2014-01-01 00:00:00 +0000 UTC"},
|
{in: "2014", out: "2014-01-01 00:00:00 +0000 UTC"},
|
||||||
{in: "20140601", out: "2014-06-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
|
// 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"},
|
||||||
{in: "1332151919", out: "2012-03-19 10:11:59 +0000 UTC", loc: "America/Denver"},
|
{in: "1332151919", out: "2012-03-19 10:11:59 +0000 UTC", loc: "America/Denver"},
|
||||||
|
Loading…
Reference in New Issue
Block a user