Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
miniflux
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TeDomum
miniflux
Commits
e7afec7e
Commit
e7afec7e
authored
7 years ago
by
Frédéric Guillot
Browse files
Options
Downloads
Patches
Plain Diff
Handle more date formats
parent
e4b4beab
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
reader/date/parser.go
+7
-2
7 additions, 2 deletions
reader/date/parser.go
reader/date/parser_test.go
+56
-0
56 additions, 0 deletions
reader/date/parser_test.go
with
63 additions
and
2 deletions
reader/date/parser.go
+
7
−
2
View file @
e7afec7e
...
...
@@ -5,6 +5,7 @@
package
date
import
(
"errors"
"fmt"
"strings"
"time"
...
...
@@ -45,6 +46,8 @@ var dateFormats = []string{
"Monday, 02 January 2006 15:04:05 MST"
,
"Monday, 02 January 2006 15:04:05 -0700"
,
"Monday, 02 January 2006 15:04:05"
,
"Monday, January 02, 2006 - 3:04pm"
,
"Monday, January 2, 2006 - 3:04pm"
,
"Mon, 2 January 2006 15:04 MST"
,
"Mon, 2 January 2006, 15:04 -0700"
,
"Mon, 2 January 2006, 15:04:05 MST"
,
...
...
@@ -100,6 +103,7 @@ var dateFormats = []string{
"Mon, 02 Jan 2006 15:04:05"
,
"Mon, 02 Jan 2006"
,
"Mon, 02 Jan 06 15:04:05 MST"
,
"Mon, 02 Jan 2006 3:04 PM MST"
,
"January 2, 2006 3:04 PM"
,
"January 2, 2006, 3:04 p.m."
,
"January 2, 2006 15:04:05 MST"
,
...
...
@@ -125,6 +129,7 @@ var dateFormats = []string{
"2 Jan 2006 15:04:05 MST"
,
"2 Jan 2006 15:04:05 -0700"
,
"2 Jan 2006"
,
"2 Jan 2006 15:04 MST"
,
"2.1.2006 15:04:05"
,
"2/1/2006"
,
"2-1-2006"
,
...
...
@@ -189,7 +194,7 @@ var dateFormats = []string{
func
Parse
(
ds
string
)
(
t
time
.
Time
,
err
error
)
{
d
:=
strings
.
TrimSpace
(
ds
)
if
d
==
""
{
return
t
,
fmt
.
Errorf
(
"
D
ate
string is empty
"
)
return
t
,
errors
.
New
(
"
d
ate
parser: empty value
"
)
}
for
_
,
f
:=
range
dateFormats
{
...
...
@@ -198,6 +203,6 @@ func Parse(ds string) (t time.Time, err error) {
}
}
err
=
fmt
.
Errorf
(
"F
ailed to parse date
:
%s"
,
ds
)
err
=
fmt
.
Errorf
(
`date parser: f
ailed to parse date
"
%s"
`
,
ds
)
return
}
This diff is collapsed.
Click to expand it.
reader/date/parser_test.go
0 → 100644
+
56
−
0
View file @
e7afec7e
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package
date
import
"testing"
func
TestParseEmptyDate
(
t
*
testing
.
T
)
{
if
_
,
err
:=
Parse
(
" "
);
err
==
nil
{
t
.
Fatalf
(
`Empty dates should return an error`
)
}
}
func
TestParseInvalidDate
(
t
*
testing
.
T
)
{
if
_
,
err
:=
Parse
(
"invalid"
);
err
==
nil
{
t
.
Fatalf
(
`Invalid dates should return an error`
)
}
}
func
TestParseAtomDate
(
t
*
testing
.
T
)
{
date
,
err
:=
Parse
(
"2017-12-22T22:09:49+00:00"
)
if
err
!=
nil
{
t
.
Fatalf
(
`Atom dates should be parsed correctly`
)
}
if
date
.
Unix
()
!=
1513980589
{
t
.
Fatal
(
`Invalid date parsed`
)
}
}
func
TestParseRSSDate
(
t
*
testing
.
T
)
{
date
,
err
:=
Parse
(
"Tue, 03 Jun 2003 09:39:21 GMT"
)
if
err
!=
nil
{
t
.
Fatalf
(
`RSS dates should be parsed correctly`
)
}
if
date
.
Unix
()
!=
1054633161
{
t
.
Fatal
(
`Invalid date parsed`
)
}
}
func
TestParseWeirdDateFormat
(
t
*
testing
.
T
)
{
dates
:=
[]
string
{
"Sun, 17 Dec 2017 1:55 PM EST"
,
"9 Dec 2016 12:00 GMT"
,
"Friday, December 22, 2017 - 3:09pm"
,
"Friday, December 8, 2017 - 3:07pm"
,
}
for
_
,
date
:=
range
dates
{
if
_
,
err
:=
Parse
(
date
);
err
!=
nil
{
t
.
Fatalf
(
`Unable to parse date: "%s"`
,
date
)
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment