增加 parse() 方法 对 now、yesterday和tomorrow 的解析

This commit is contained in:
gouguoyin 2022-11-04 10:56:50 +08:00
parent 8a385b2d4c
commit 6afbf0be9a
2 changed files with 12 additions and 0 deletions

View File

@ -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 {

View File

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