Skip to content

Commit

Permalink
Add parse formats
Browse files Browse the repository at this point in the history
  • Loading branch information
gouguoyin committed Nov 2, 2022
1 parent e55ab98 commit d9a03bd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func (c Carbon) Parse(value string, timezone ...string) Carbon {
"2006-1", "2006-1-2", "2006-1-2 15", "2006-1-2 15:4", "2006-1-2 15:4:5", "2006-1-2 15:4:5.999999999",
"2006.1", "2006.1.2", "2006.1.2 15", "2006.1.2 15:4", "2006.1.2 15:4:5", "2006.1.2 15:4:5.999999999",
"2006/1", "2006/1/2", "2006/1/2 15", "2006/1/2 15:4", "2006/1/2 15:4:5", "2006/1/2 15:4:5.999999999",
"2006-01-02 15:04:05PM MST", "2006-01-02 15:04:05.999999999PM MST", "2006-1-2 15:4:5PM MST", "2006-1-2 15:4:5.999999999PM MST",
"2006-01-02 15:04:05 PM MST", "2006-01-02 15:04:05.999999999 PM MST", "2006-1-2 15:4:5 PM MST", "2006-1-2 15:4:5.999999999 PM MST",
"1/2/2006", "1/2/2006 15", "1/2/2006 15:4", "1/2/2006 15:4:5", "1/2/2006 15:4:5.999999999",
"2006-1-2 15:4:5 -0700 MST", "2006-1-2 15:4:5.999999999 -0700 MST",
"2006-1-2T15:4:5Z07", "2006-1-2T15:4:5.999999999Z07",
Expand All @@ -35,6 +37,7 @@ func (c Carbon) Parse(value string, timezone ...string) Carbon {
"20060102150405Z07", "20060102150405.999999999Z07",
"20060102150405Z07:00", "20060102150405.999999999Z07:00",
}

if value == "" || value == "0" || value == "0000-00-00 00:00:00" || value == "0000-00-00" || value == "00:00:00" {
return c
}
Expand Down
18 changes: 18 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ func TestCarbon_Parse(t *testing.T) {
{"2020-08-05 1:02:03", "2020-08-05 01:02:03 +0800 CST"},
{"2020-08-05 01:02:03", "2020-08-05 01:02:03 +0800 CST"},

{"2020-8-5 13:14:15PM EST", "2020-08-05 21:14:15 +0800 CST"},
{"2020-8-05 13:14:15PM EST", "2020-08-05 21:14:15 +0800 CST"},
{"2020-08-05 13:14:15PM EST", "2020-08-05 21:14:15 +0800 CST"},
{"2020-08-05 13:14:15.999999999PM EST", "2020-08-05 21:14:15.999999999 +0800 CST"},

{"2020-8-5 13:14:15 PM EST", "2020-08-05 21:14:15 +0800 CST"},
{"2020-8-05 13:14:15 PM EST", "2020-08-05 21:14:15 +0800 CST"},
{"2020-08-05 13:14:15 PM EST", "2020-08-05 21:14:15 +0800 CST"},
{"2020-08-05 13:14:15.999999999 PM EST", "2020-08-05 21:14:15.999999999 +0800 CST"},

{"2020-8-5 13:14:15 PM EST", "2020-08-05 21:14:15 +0800 CST"},
{"2020-8-05 13:14:15 PM EST", "2020-08-05 21:14:15 +0800 CST"},
{"2020-08-05 13:14:15 PM EST", "2020-08-05 21:14:15 +0800 CST"},
{"2020-08-05 13:14:15.999999999 PM EST", "2020-08-05 21:14:15.999999999 +0800 CST"},

{"2020-08-05 1:2:3.999", "2020-08-05 01:02:03.999 +0800 CST"},
{"2020-08-05 1:2:3.999999", "2020-08-05 01:02:03.999999 +0800 CST"},
{"2020-08-05 1:2:3.999999999", "2020-08-05 01:02:03.999999999 +0800 CST"},
Expand All @@ -54,6 +69,9 @@ func TestCarbon_Parse(t *testing.T) {
{"2020/8/5 13:14:15", "2020-08-05 13:14:15 +0800 CST"},
{"2020/8/05 13:14:15", "2020-08-05 13:14:15 +0800 CST"},
{"2020/08/05 13:14:15", "2020-08-05 13:14:15 +0800 CST"},
{"2020/8/5 13:14:15.999", "2020-08-05 13:14:15.999 +0800 CST"},
{"2020/8/05 13:14:15.999999", "2020-08-05 13:14:15.999999 +0800 CST"},
{"2020/08/5 13:14:15.999999999", "2020-08-05 13:14:15.999999999 +0800 CST"},

{"2020-8-5T13:14:15+08:00", "2020-08-05 13:14:15 +0800 CST"},
{"2020-8-05T13:14:15+08:00", "2020-08-05 13:14:15 +0800 CST"},
Expand Down

0 comments on commit d9a03bd

Please sign in to comment.