GithubHelp home page GithubHelp logo

araddon / dateparse Goto Github PK

View Code? Open in Web Editor NEW
2.0K 2.0K 165.0 474 KB

GoLang Parse many date strings without knowing format in advance.

License: MIT License

Go 100.00%
date dates datetime golang time

dateparse's People

Contributors

allisonmorgan avatar araddon avatar charles-dyfis-net avatar creekorful avatar drewlanenga avatar egonelbre avatar ferhatelmas avatar icholy avatar ifreddyrondon avatar junichif avatar mikecamilleri avatar nemith avatar phorne-uncharted avatar radaiming avatar rambatino avatar solidmetanoia avatar treuherz avatar troyspencer avatar zikaeroh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dateparse's Issues

Single Digit Parsing error "Thu, 4 Jan 2018 17:53:36 +0000"

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.

Another crazy format

This surprising formats comes from Excel ๐Ÿ’ฉ

  • 9/22/88 12:00
  • 6/2/08 12:00

Failed format

For june 1, 2018, fails with following message:

parsing time "june 1, 2018" as "Jane 2, 2006": cannot parse "june 1, 2018" as "Jane "

Format: 18 January 2018 does not work?

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?

yyyy.MM.dd

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"

Cannot parse "MST)" as " (CEST)"

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)" |
+------------+-----------------------+-----------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+

$

Small extension on a format

Regarding fix in #61, how about sept. 28, 2017 ?

dateparse.ParseAny(sept. 28, 2017)
> cannot parse "sept. 28, 2017" as "Jant. "

DD-Mon-YYYY

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?

april 3, 2018 missing format

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.

ignoring PDT time zone?

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!

Mismatch between formats

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.

Can't parse a good format

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

Another date format: 'January 02, 2006, 15:04:05'

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!

Could not find time offset

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.

Parsing time of format hh:mm:ss with ParseAny()

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 ?

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

Small adjustment on format

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 a string

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 "

Should suffixes be allowed?

I have a date formatted as November 7th, 2018 and the parsing fails at the 7th part. Should suffixes be allowed?

New format time?

panic: parsing time "Tue, 11 Jul 2017 16:28:13 +0200 (CEST)": extra text: (CEST)

Cannot parse strings which have timezone and skip seconds.

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.

Parsing problem

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?

Request: Support for DD/MM/YYYY

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",

Local time

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

Cannot parse "Jan 20 21:17:01"

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?

"02 Jan 2018 23:59" not parsed

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"

Better support for dot-format (e.g. 13.1.2009) with ambiguousMMDD

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.)

Not parsing older years well

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.

Can't parse golang default format

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

Parsing ISO 8601 datetime without seconds

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?

Request: ParseFormat

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.

Cannot parse string with 1 day digit and June/July

Observed Behavior

$./dateparse "1 July 2013" 
fatal: parsing time "1 July 2013" as "2 Jany 2006": cannot parse "uly 2013" as " Jany "

Expected Behavior

Parses string without error

Notes

I also observe this behavior on the latest commit with "9 June 2013" but not with "19 June 2013"

Once again, another format

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.

About License

what license is this code using? can it be used freely?

What happens with multiple dates within a string?

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

Cannot parse `2015-06-10 00:00:00 GMT+02:00`

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.