-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support datetime format variants
- Loading branch information
Showing
11 changed files
with
133 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package ayd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
"time" | ||
) | ||
|
||
var ( | ||
dateformats = []string{ | ||
"2006-01-02T", | ||
"2006-01-02_", | ||
"2006-01-02 ", | ||
"20060102 ", | ||
"20060102T", | ||
"20060102_", | ||
} | ||
timeformats = []string{ | ||
"15:04:05", | ||
"15:04:05.999999999", | ||
"150405", | ||
"150405.999999999", | ||
} | ||
zoneformats = []string{ | ||
"Z07:00", | ||
"Z0700", | ||
"Z07", | ||
} | ||
|
||
ErrInvalidTime = errors.New("invalid format") | ||
) | ||
|
||
// ParseTime parses time string in Ayd way. | ||
// This function supports RFC3339 and some variant formats. | ||
func ParseTime(s string) (time.Time, error) { | ||
x := strings.ToUpper(strings.TrimSpace(s)) | ||
for _, df := range dateformats { | ||
for _, tf := range timeformats { | ||
for _, zf := range zoneformats { | ||
t, err := time.Parse(df+tf+zf, x) | ||
if err == nil { | ||
return t, nil | ||
} | ||
} | ||
} | ||
} | ||
return time.Time{}, fmt.Errorf("%w: %q", ErrInvalidTime, s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package ayd_test | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
"time" | ||
|
||
"github.com/macrat/ayd/lib-ayd" | ||
) | ||
|
||
func TestParseTime_valid(t *testing.T) { | ||
tests := []string{ | ||
"2000-01-02T00:03:04+00:00", | ||
"2000-01-02T00:03:04+0000", | ||
"2000-01-02T00:03:04-00:00", | ||
"20000102T00:03:04-0000", | ||
"20000102T00:03:04.897+00:00", | ||
"20000102T00:03:04.897-00:00", | ||
"20000102T00:03:04.897010Z", | ||
"20000102T000304Z", | ||
"20000102T00:03:04z", | ||
"2000-01-02T08:48:04+08:45", | ||
"2000-01-02T08:48:04+0845", | ||
"2000-01-02T09:03:04+09:00", | ||
"2000-01-02T09:03:04+0900", | ||
"2000-01-02T090304.897+09:00", | ||
"2000-01-02T090304.897010+09:00", | ||
"2000-01-02T090304.897010+0900", | ||
"2000-01-02t000304Z", | ||
"2000-01-02t000304z", | ||
"2000-01-02_00:03:04.897010Z", | ||
"2000-01-02_00:03:04.897010z", | ||
"2000-01-02_00:03:04.897z", | ||
"20000102_00:03:04Z", | ||
"20000102_00:03:04z", | ||
"20000102 00:03:04+00:00", | ||
"20000102 00:03:04-0000", | ||
"2000-01-02 00:03:04.897-00:00", | ||
"2000-01-02 00:03:04.897010z", | ||
"2000-01-02 00:03:04.897Z", | ||
"2000-01-02 00:03:04Z", | ||
"2000-01-02 000304z", | ||
"2000-01-02 090304+09", | ||
"2000-01-02 09:03:04.897010+09", | ||
} | ||
|
||
want := time.Date(2000, 1, 2, 0, 3, 4, 0, time.UTC) | ||
|
||
for _, tt := range tests { | ||
actual, err := ayd.ParseTime(tt) | ||
if err != nil { | ||
t.Errorf("failed to parse %q: %s", tt, err) | ||
} else if want.Unix() != actual.Truncate(time.Second).Unix() { | ||
t.Errorf("unexpected result from %q: %s", tt, actual) | ||
} | ||
} | ||
} | ||
|
||
func TestParseTime_invalid(t *testing.T) { | ||
tests := []string{ | ||
"2000/01/02 00:03:04", | ||
} | ||
|
||
for _, tt := range tests { | ||
_, err := ayd.ParseTime(tt) | ||
if !errors.Is(err, ayd.ErrInvalidTime) { | ||
t.Errorf("unexpected error from %q: %s", tt, err) | ||
} | ||
} | ||
} |