mirror of
https://gitee.com/dromara/carbon.git
synced 2024-11-29 18:57:37 +08:00
增加 parse() 方法 对 now、yesterday和tomorrow 的解析
This commit is contained in:
parent
8a385b2d4c
commit
6afbf0be9a
@ -40,6 +40,14 @@ func (c Carbon) Parse(value string, timezone ...string) Carbon {
|
||||
if len(timezone) > 0 {
|
||||
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
|
||||
}
|
||||
switch value {
|
||||
case "now":
|
||||
return c.Now(timezone...)
|
||||
case "yesterday":
|
||||
return c.Yesterday(timezone...)
|
||||
case "tomorrow":
|
||||
return c.Tomorrow(timezone...)
|
||||
}
|
||||
for _, layout := range timeLayouts {
|
||||
t, err := time.ParseInLocation(layout, value, c.loc)
|
||||
if err == nil {
|
||||
|
@ -20,6 +20,10 @@ func TestCarbon_Parse(t *testing.T) {
|
||||
{"00:00:00", ""},
|
||||
{"0000-00-00 00:00:00", ""},
|
||||
|
||||
{Parse("now").ToDateTimeString(), Now().ToDateTimeString() + " +0800 CST"},
|
||||
{Parse("yesterday").ToDateTimeString(), Yesterday().ToDateTimeString() + " +0800 CST"},
|
||||
{Parse("tomorrow").ToDateTimeString(), Tomorrow().ToDateTimeString() + " +0800 CST"},
|
||||
|
||||
{"2020-8-5", "2020-08-05 00:00:00 +0800 CST"},
|
||||
{"2020-8-05", "2020-08-05 00:00:00 +0800 CST"},
|
||||
{"2020-08-05", "2020-08-05 00:00:00 +0800 CST"},
|
||||
|
Loading…
Reference in New Issue
Block a user