araddon / dateparse Goto Github PK
View Code? Open in Web Editor NEWGoLang Parse many date strings without knowing format in advance.
License: MIT License
GoLang Parse many date strings without knowing format in advance.
License: MIT License
Attempted to parse a date from an RSS feed and got the following error:
Error: parsing time "Thu, 4 Jan 2018 23:17:00 +0000" as "Mon, 02 Jan 2006 15:04:05 -0700": cannot parse "4 Jan 2018 23:17:00 +0000" as "02"
Looks like it doesn't like the single digit day representation.
Thanks.
This surprising formats comes from Excel ๐ฉ
9/22/88 12:00
6/2/08 12:00
For june 1, 2018
, fails with following message:
parsing time "june 1, 2018" as "Jane 2, 2006": cannot parse "june 1, 2018" as "Jane "
dateparse.ParseAny("18 January 2018")
gives me Could not find date format for 18 January 2018
It seems like a reasonably common format - any plans on supporting it in the near future?
Can't parse "2018.09.30":
$ dateparse "2018.09.30"
fatal: parsing time "2018.09.30": hour out of range
$ dateparse "2018.09.30 00:37:43.234"
fatal: parsing time "2018.09.30 00:37:43.234" as "2006.09.20060:37:43.234": cannot parse "0:37:43.234" as "2006"
go version go1.9.2 darwin/amd64
$ ./dateparse "Fri, 5 Jan 2018 14:09:32 -0700 (MST)"
Your Current time.Local zone is PST
+------------+-----------------------+-----------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
| method | Zone Source | Parsed | Parsed: t.In(time.UTC) |
+------------+-----------------------+-----------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
| ParseAny | time.Local = nil | parsing time "Fri, 5 Jan 2018 14:09:32 -0700 (MST)" as "Mon, _2 Jan 2006 15:04:05 -0700 (CEST)": cannot parse "MST)" as " (CEST)" | parsing time "Fri, 5 Jan 2018 14:09:32 -0700 (MST)" as "Mon, _2 Jan 2006 15:04:05 -0700 (CEST)": cannot parse "MST)" as " (CEST)" |
| ParseAny | time.Local = time.UTC | parsing time "Fri, 5 Jan 2018 14:09:32 -0700 (MST)" as "Mon, _2 Jan 2006 15:04:05 -0700 (CEST)": cannot parse "MST)" as " (CEST)" | parsing time "Fri, 5 Jan 2018 14:09:32 -0700 (MST)" as "Mon, _2 Jan 2006 15:04:05 -0700 (CEST)": cannot parse "MST)" as " (CEST)" |
| ParseIn | time.Local = nil | parsing time "Fri, 5 Jan 2018 14:09:32 -0700 (MST)" as "Mon, _2 Jan 2006 15:04:05 -0700 (CEST)": cannot parse "MST)" as " (CEST)" | parsing time "Fri, 5 Jan 2018 14:09:32 -0700 (MST)" as "Mon, _2 Jan 2006 15:04:05 -0700 (CEST)": cannot parse "MST)" as " (CEST)" |
| ParseIn | time.Local = time.UTC | parsing time "Fri, 5 Jan 2018 14:09:32 -0700 (MST)" as "Mon, _2 Jan 2006 15:04:05 -0700 (CEST)": cannot parse "MST)" as " (CEST)" | parsing time "Fri, 5 Jan 2018 14:09:32 -0700 (MST)" as "Mon, _2 Jan 2006 15:04:05 -0700 (CEST)": cannot parse "MST)" as " (CEST)" |
| ParseLocal | time.Local = nil | parsing time "Fri, 5 Jan 2018 14:09:32 -0700 (MST)" as "Mon, _2 Jan 2006 15:04:05 -0700 (CEST)": cannot parse "MST)" as " (CEST)" | parsing time "Fri, 5 Jan 2018 14:09:32 -0700 (MST)" as "Mon, _2 Jan 2006 15:04:05 -0700 (CEST)": cannot parse "MST)" as " (CEST)" |
| ParseLocal | time.Local = time.UTC | parsing time "Fri, 5 Jan 2018 14:09:32 -0700 (MST)" as "Mon, _2 Jan 2006 15:04:05 -0700 (CEST)": cannot parse "MST)" as " (CEST)" | parsing time "Fri, 5 Jan 2018 14:09:32 -0700 (MST)" as "Mon, _2 Jan 2006 15:04:05 -0700 (CEST)": cannot parse "MST)" as " (CEST)" |
+------------+-----------------------+-----------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
$
Regarding fix in #61, how about sept. 28, 2017
?
dateparse.ParseAny(sept. 28, 2017)
> cannot parse "sept. 28, 2017" as "Jant. "
Currently seeing the following:
2018/10/09 12:04:23 parsing time "29-Jun-2016 18:09:59" as "29-Jan-2016 18:09:59": cannot parse "-Jun-2016 18:09:59" as "9-"
Is there any way for us to pass in our own string formats so when we detect a bad one like this (that has an easy interpretation) we can ensure it gets parsed correctly?
parsing time "april 3, 2018" as "January 02, 2006 at 3:04pm MST-07": cannot parse "3, 2018" as "02"
Apr 3, 2018
is possible but not this one.
Get the error Could not find date format for "2017-12-02"
when doingdateparse.ParseAny("2017-12-02")
Golang: 1.9
Hi, thanks for this great library! I've noticed that the Parse
function seems to have different rules for time zones than time.Parse
though, contradicting the docs. For example, try this program:
package main
import (
"fmt"
"time"
"github.com/araddon/dateparse"
)
func main() {
timeString := "Fri Jul 27 13:46:40 PDT 2018"
fmt.Printf("original: %s\n", timeString)
t, err := dateparse.ParseAny(timeString)
if err != nil {
panic(err)
}
fmt.Printf("dateparse: %s\n", t)
tt, err := time.Parse("Mon Jan 2 15:04:05 MST 2006", timeString)
if err != nil {
panic(err)
}
fmt.Printf("time.Time: %s\n", tt)
fmt.Printf("Local: %s\n", tt.Local())
}
The output is:
$ ./time
original: Fri Jul 27 13:46:40 PDT 2018
dateparse: 2018-07-27 13:46:40 +0000 UTC
time.Time: 2018-07-27 13:46:40 -0700 PDT
Local: 2018-07-27 13:46:40 -0700 PDT
dateparse
returns a time with a UTC timezone, but without adjusting the time for UTC according to the PDT time zone. "PST" gives similar results, but "MTC" and "UTC" both work as expected.
Am I doing something wrong, or is this a bug? Thanks!
Fails for
oct 1, 1970
oct 1, '70
1 oct 70
1 oct 1970
etc...
fmt.Println(dateparse.ParseAny("2018-05"))
// 2018-05-01 00:00:00 +0000 UTC <nil>
fmt.Println(dateparse.ParseAny("2018.05"))
// 0001-01-01 00:00:00 +0000 UTC Could not find date format for 2018.05
Would expect them to behave similarly.
const s = "13/08/2018"
fmt.Println(dateparse.ParseAny(s))
// 0001-01-01 00:00:00 +0000 UTC parsing time "13/08/2018": month out of range
I've just run into a few more date formats that I think would be good to have:
January 02, 2006, 15:04:05
January 02, 2006 15:04:05
January 02, 2006 03:04:05 pm
Basically, the same as "September 17, 2012 at 10:09am PST-08" but without the 'at' and a couple variations.
Thanks!
Does not seem to parse no seconds with the date format having dashes and not slashes.
AND... fantastic library, thank you!
parsing time "September 17, 2012 at 5:00pm UTC-05" as "September Jan 2006 at 5:00pm UTC-05"
Seems like it should be Jan 2 2006 at 3:04pm UTC-05
Hi,
if we will do something like:
a := time.Now().Local().Add(time.Hour*time.Duration(0) +
time.Minute*time.Duration(30) +
time.Second*time.Duration(0))
(it's just offset for 30 mins from now), we will get:
2018-02-11 15:55:38.194708 +0300 MSK
in a
variable, for example. Let's place this value like a string type:
a := "2018-02-11 15:55:38.194708 +0300 MSK"
and try to use ParseLocal() for this string variable:
t, _ := dateparse.ParseLocal(a)
we will get 0001-01-01 00:00:00 +0000 UTC
.
input: 2017-12-31T16:00:00Z
Z represent UTC time,
result:
2017-12-31 16:00:00 +0800
Currently, trying to parse a time of layout "hh:mm:ss" using the ParseAny() returns the following error:
parsing time "16:14:43" as "20060102": cannot parse "4:43" as "2006"
It will be nice if ParseAny() handles a layout without the date format. An equivalent implementation in the standard library:
layout := "15:04:05"
str := "16:14:43"
t, err := time.Parse(layout, str)
How can I use time.parse with this date return by this API.
I need to define format after your lig as :
`
time.Local = dateLocation
date, err := dateparse.ParseIn(myDate, time.Local)
if err != nil {
return query, err
}
query = elastic.NewBoolQuery().Filter(
elastic.NewRangeQuery("updatedAt").
From(date).TimeZone("Europe/Paris").Format("yyyy-MM-dd HH:mm:ss.000000 PM"))
`
then
yyyy-MM-dd HH:mm:ss.000000 PM
must not be the right format.
Can you help me to set it ?
Thx
fmt.Println(dateparse.ParseAny("jan 2, 2018"))
> 2018-01-02 00:00:00 +0000 UTC <nil>
fmt.Println(dateparse.ParseAny("jan. 2, 2018"))
> 0001-01-01 00:00:00 +0000 UTC parsing time "jan. 2, 2018" as "January 02, 2006 at 3:04pm MST-07": cannot parse "jan. 2, 2018" as "January"
Is it possible to support this extension ?
Cannot parse Tue, 8 Dec 2015 16:00:01 +0600 (NOVT)
parsing time "Tue, 8 Dec 2015 16:00:01 +0600 (NOVT)" as "Mon, Janec 2015 16:00:01 +0600 (NOVT)": cannot parse "8 Dec 2015 16:00:01 +0600 (NOVT)" as ", Janec "
fmt.Println(dateparse.ParseFormat("3")) // 3 nil
I have a date formatted as November 7th, 2018
and the parsing fails at the 7th
part. Should suffixes be allowed?
The following two formats exist in the wild, e.g. go-nuts raw emails:
panic: parsing time "Tue, 11 Jul 2017 16:28:13 +0200 (CEST)": extra text: (CEST)
I am not able to parse strings which don't have seconds, but contain timezone immediately after minutes.
eg:
package main
import (
"fmt"
"github.com/araddon/dateparse"
)
func main() {
errStrings := []string{
"2018-04-02T22:18+0530",
"2018-04-02 22:18+0530",
}
for i, str := range errStrings {
if _, err := dateparse.ParseAny(str); err != nil {
fmt.Printf("#%d %s: %v\n", i, str, err)
}
}
}
Output:
#0 2018-04-02T22:18+0530: parsing time "2018-04-02T22:18+0530" as "2006-01-02T15:48-0700": cannot parse "+0530" as "8"
#1 2018-04-02 22:18+0530: parsing time "2018-04-02 22:18+0530" as "2006-01-02 15:48-0700": cannot parse "+0530" as "8"
If I put a space after minutes(eg: "2018-04-02T22:18 +0530"
), the strings are parsed correctly.
Go 1.9 introduced a monotonic clock reading to its string representation: https://golang.org/doc/go1.9#time
The package will need to add support for formats including the clock output, which currently isn't attempted in parseTime
.
Hi, I'm getting this error:
panic: parsing time "Thu, 13 Jul 2017 08:58:40 +0100" as "Mon, 02 Jan 2006 15:04:05 MST": cannot parse "+0100" as "MST"
with this string Thu, 13 Jul 2017 08:58:40 +0100
How can I solve?
A followup on #30 for excel.
43161.0: 2 March 2018
43186.0: 27 March 2018
Hello!
I noticed dateparse doesn't currently have support for UK based dates, is this within scope?
Flipped examples from mm/dd/yyyy:
// dd/mm/yyyy
"31/3/2014",
"31/03/2014",
"21/08/71",
"1/8/71",
"8/4/2014 22:05",
"08/04/2014 22:05",
"2/04/2014 03:00:51",
"8/8/1965 12:00:00 AM",
"8/8/1965 01:00:01 PM",
"8/8/1965 01:00 PM",
"8/8/1965 1:00 PM",
"8/8/1965 12:00 AM",
"4/02/2014 03:00:51",
"19/03/2012 10:11:59",
"19/03/2012 10:11:59.3186369",
Would parsing thing like "yesterday" and "previous month" be something that's in the scope of this project?
Is there a way to convert a string like this Thu, 13 Jul 2017 08:16:43 -0700
in my local time ?
After parsing I obtain an time.Time object 2017-07-13 08:16:43 -0700 -0700
but seem to be 9 hours back
Fatal Error: parsing time "Jan 20 21:17:01": month out of range
This format is used in Debian's auth.log
Could you please add this format?
bin/dateparse "02 Jan 2018 23:59"
gives
parsing time "02 Jan 2018 23:59" as "02 January 2006": cannot parse "Jan 2018 23:59" as "January"
While the dot format is currently supported for MM.DD.YYYY
, this format does not set the ambiguousMMDD
flag. This leads to issues mainly in fixes for #28 (such as #89), where ambiguousMMDD
needs to be set.
The way I see it, ambiguousMMDD
needs to be set for all ambiguous date formats (regardless the separator).
(FWIW, I'm pretty sure the dot separator is mostly used in DD.MM.YY[YY]
formats anyway.)
input: 2017-12-31T16:00:00Z
result:
2017-12-31 16:00:00 +0800
Expected:
2018-01-01 00:00:00 +0800
Since the time component of a datetime has common patterns across different date formats. Refactor so the Time state machine is different from the date so it can be re-used
github.com/apcera/termtables is no longer available, so go get -t -v ./...
fails.
It would be nice to support dotted-formats like 12.12.2014
It appears that any date with two-digits years in the 1940s and 1950s is getting parsed as 2040s and 2050s.
I am unable to find the code for setYear()
to look at the rules because the parsing looks correct beginning in the 1970s.
What am I doing wrong?
package main
import (
"fmt"
"github.com/araddon/dateparse"
)
func main() {
a := "2018-06-29 19:09:57.77297118 +0300 +03" // result of time.Now().String()
fmt.Println(a)
t, err := dateparse.ParseLocal(a)
fmt.Println(t, err)
t, err = dateparse.ParseAny(a)
fmt.Println(t, err)
t, err = dateparse.ParseStrict(a)
fmt.Println(t, err)
}
Result:
2018-06-29 19:09:57.77297118 +0300 +03
2018-06-29 03:09:57.77297118 +0300 +03 <nil>
2018-06-29 03:09:57.77297118 +0300 +03 <nil>
2018-06-29 03:09:57.77297118 +0300 +03 <nil>
Why only the hour is mangled?
go version go1.10.2 darwin/amd64
Hey, thanks for this library, it looks great!
Just one problem I've spotted so far, it works fine with an ISO 8601 like this 2009-08-12T22:15:09Z
(from your examples)
But removing the seconds field to make it 2009-08-12T22:15Z
gives this error:
parsing time "2009-08-12T22:15Z" as "2006-01-02T15:04:05Z": cannot parse "Z" as ":"
I believe this is a valid string per the 8601 spec (but don't quote me!), is this a bug?
It would be nice to have a version that outputs a date format string for Go, so that once a format is known it could be used on subsequent parsing more efficiently.
$./dateparse "1 July 2013"
fatal: parsing time "1 July 2013" as "2 Jany 2006": cannot parse "uly 2013" as " Jany "
Parses string without error
I also observe this behavior on the latest commit with "9 June 2013" but not with "19 June 2013"
This is for the date format yyyyMMddhhmmss
.
Currently, if I have a date as so: 20180722105203
, which should format to 22 July 2018 10:52:03
, it will format as if it is a millisecond or second timestamp. Specifically, it will format this date to 22 Aug 1970 07:45:22
.
This means that it is either trimming 20180722105203
down to 20180722
seconds or 20180722105
milliseconds. If there are 14 digits, I think it should be assumed to be the date format yyyyMMddhhmmss
, as 14 digits of milliseconds is around the year 2500 and 14 digits of seconds is off the charts.
what license is this code using? can it be used freely?
This isn't an issue, but more of an implementation question. Apologies in advance if this is not the right place for a question.
I have text pages with multiple dates on each page, sometimes with different dates on the same line.
I was thinking about passing dateparse each line in a file, but from the examples it looks like it only works with single strings. So what is the behavior of dateparse when multiple dates are included within a string?
I am assuming I need to figure out some method of tokenization of each line in these text files in order to hand dateparse only a single date candidate to convert?
Thanks in advance
Revision: 21df004e09ca46e07ce99bbba1e8c2422ba4ecf9
Example link: https://www.e-marketing.fr/Thematique/social-media-1096/Breves/Banque-Postale-mise-marketing-temps-reel-impression-255997.htm#WQ17awvif7mWRLVy.97
Test Case
func TestDateParse(t *testing.T) {
_, err := dateparse.ParseAny("2015-06-10 00:00:00 GMT+02:00")
assert.NoError(t, err)
}
Result
Error Trace: date_extractor_test.go:80
Error: Received unexpected error:
parsing time "2015-06-10 00:00:00 GMT+02:00" as "2006-01-02 15:04:05 MST-07000": cannot parse ":00" as "-0700"
Test: TestDateParse
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.