From 35f78a7f8a9ddc2aa98a0eaf2258d7ea5d0d2a25 Mon Sep 17 00:00:00 2001 From: gouguoyin <245629560@qq.com> Date: Sun, 17 Apr 2022 22:45:10 +0800 Subject: [PATCH] =?UTF-8?q?Parse()=E6=96=B9=E6=B3=95=E6=94=AF=E6=8C=81=20S?= =?UTF-8?q?hortDateTimeMilliFormat=E3=80=81ShortDateTimeMicroFormat?= =?UTF-8?q?=E3=80=81ShortDateTimeNanoFormat=20=E6=A0=BC=E5=BC=8F=E7=9A=84?= =?UTF-8?q?=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.cn.md | 7 +++++-- README.jp.md | 7 +++++-- README.md | 7 +++++-- parser.go | 12 +++++++++--- parser_test.go | 7 +++++-- 5 files changed, 29 insertions(+), 11 deletions(-) diff --git a/README.cn.md b/README.cn.md index dc5af29..e0cb597 100644 --- a/README.cn.md +++ b/README.cn.md @@ -160,6 +160,8 @@ carbon.Parse("0000-00-00 00:00:00").ToDateTimeString() // 空字符串 carbon.Parse("0000-00-00").ToDateTimeString() // 空字符串 carbon.Parse("00:00:00").ToDateTimeString() // 空字符串 +carbon.Parse("2020-08-05").ToString() // 2020-08-05 00:00:00 +0800 CST +carbon.Parse("20200805").ToString() // 2020-08-05 00:00:00 +0800 CST carbon.Parse("2020-08-05 13:14:15").ToString() // 2020-08-05 13:14:15 +0800 CST carbon.Parse("2020-08-05 13:14:15.999").ToString() // 2020-08-05 13:14:15.999 +0800 CST carbon.Parse("2020-08-05 13:14:15.999999").ToString() // 2020-08-05 13:14:15.999999 +0800 CST @@ -168,9 +170,10 @@ carbon.Parse("2020-08-05T13:14:15+08:00").ToString() // 2020-08-05 13:14:15 +080 carbon.Parse("2020-08-05T13:14:15.999+08:00").ToString() // 2020-08-05 13:14:15.999 +0800 CST carbon.Parse("2020-08-05T13:14:15.999999+08:00").ToString() // 2020-08-05 13:14:15.999999 +0800 CST carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToString() // 2020-08-05 13:14:15.999999999 +0800 CST -carbon.Parse("2020-08-05").ToString() // 2020-08-05 00:00:00 +0800 CST carbon.Parse("20200805131415").ToString() // 2020-08-05 13:14:15 +0800 CST -carbon.Parse("20200805").ToString() // 2020-08-05 00:00:00 +0800 CST +carbon.Parse("20200805131415.999").ToString() // 2020-08-05 13:14:15.999 +0800 CST +carbon.Parse("20200805131415.999999").ToString() // 2020-08-05 13:14:15.999999 +0800 CST +carbon.Parse("20200805131415.999999999").ToString() // 2020-08-05 13:14:15.999999999 +0800 CST ``` ##### 通过格式化字符将字符串解析成 Carbon 实例 diff --git a/README.jp.md b/README.jp.md index c4df518..8a6bf1d 100644 --- a/README.jp.md +++ b/README.jp.md @@ -160,6 +160,8 @@ carbon.Parse("0000-00-00 00:00:00").ToDateTimeString() // 空の文字列 carbon.Parse("0000-00-00").ToDateTimeString() // 空の文字列 carbon.Parse("00:00:00").ToDateTimeString() // 空の文字列 +carbon.Parse("2020-08-05").ToString() // 2020-08-05 00:00:00 +0800 CST +carbon.Parse("20200805").ToString() // 2020-08-05 00:00:00 +0800 CST carbon.Parse("2020-08-05 13:14:15").ToString() // 2020-08-05 13:14:15 +0800 CST carbon.Parse("2020-08-05 13:14:15.999").ToString() // 2020-08-05 13:14:15.999 +0800 CST carbon.Parse("2020-08-05 13:14:15.999999").ToString() // 2020-08-05 13:14:15.999999 +0800 CST @@ -168,9 +170,10 @@ carbon.Parse("2020-08-05T13:14:15+08:00").ToString() // 2020-08-05 13:14:15 +080 carbon.Parse("2020-08-05T13:14:15.999+08:00").ToString() // 2020-08-05 13:14:15.999 +0800 CST carbon.Parse("2020-08-05T13:14:15.999999+08:00").ToString() // 2020-08-05 13:14:15.999999 +0800 CST carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToString() // 2020-08-05 13:14:15.999999999 +0800 CST -carbon.Parse("2020-08-05").ToString() // 2020-08-05 00:00:00 +0800 CST carbon.Parse("20200805131415").ToString() // 2020-08-05 13:14:15 +0800 CST -carbon.Parse("20200805").ToString() // 2020-08-05 00:00:00 +0800 CST +carbon.Parse("20200805131415.999").ToString() // 2020-08-05 13:14:15.999 +0800 CST +carbon.Parse("20200805131415.999999").ToString() // 2020-08-05 13:14:15.999999 +0800 CST +carbon.Parse("20200805131415.999999999").ToString() // 2020-08-05 13:14:15.999999999 +0800 CST ``` ##### 文字をフォーマットして文字列を Carbon オブジェクトに解析します diff --git a/README.md b/README.md index b866c3c..9ed208a 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,8 @@ carbon.Parse("0000-00-00 00:00:00").ToDateTimeString() // empty string carbon.Parse("0000-00-00").ToDateTimeString() // empty string carbon.Parse("00:00:00").ToDateTimeString() // empty string +carbon.Parse("2020-08-05").ToString() // 2020-08-05 00:00:00 +0800 CST +carbon.Parse("20200805").ToString() // 2020-08-05 00:00:00 +0800 CST carbon.Parse("2020-08-05 13:14:15").ToString() // 2020-08-05 13:14:15 +0800 CST carbon.Parse("2020-08-05 13:14:15.999").ToString() // 2020-08-05 13:14:15.999 +0800 CST carbon.Parse("2020-08-05 13:14:15.999999").ToString() // 2020-08-05 13:14:15.999999 +0800 CST @@ -171,9 +173,10 @@ carbon.Parse("2020-08-05T13:14:15+08:00").ToString() // 2020-08-05 13:14:15 +080 carbon.Parse("2020-08-05T13:14:15.999+08:00").ToString() // 2020-08-05 13:14:15.999 +0800 CST carbon.Parse("2020-08-05T13:14:15.999999+08:00").ToString() // 2020-08-05 13:14:15.999999 +0800 CST carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToString() // 2020-08-05 13:14:15.999999999 +0800 CST -carbon.Parse("2020-08-05").ToString() // 2020-08-05 00:00:00 +0800 CST carbon.Parse("20200805131415").ToString() // 2020-08-05 13:14:15 +0800 CST -carbon.Parse("20200805").ToString() // 2020-08-05 00:00:00 +0800 CST +carbon.Parse("20200805131415.999").ToString() // 2020-08-05 13:14:15.999 +0800 CST +carbon.Parse("20200805131415.999999").ToString() // 2020-08-05 13:14:15.999999 +0800 CST +carbon.Parse("20200805131415.999999999").ToString() // 2020-08-05 13:14:15.999999999 +0800 CST ``` ##### Parse a string as a Carbon instance by format diff --git a/parser.go b/parser.go index 48ea3da..00d40cf 100755 --- a/parser.go +++ b/parser.go @@ -11,16 +11,22 @@ import ( func (c Carbon) Parse(value string, timezone ...string) Carbon { layout := DateTimeFormat if _, err := strconv.ParseInt(value, 10, 64); err == nil { - switch len(value) { - case 8: + switch { + case len(value) == 8: layout = ShortDateFormat - case 14: + case len(value) == 14: layout = ShortDateTimeFormat } } else { switch { case len(value) == 10 && strings.Count(value, "-") == 2: layout = DateFormat + case len(value) == 18 && strings.Index(value, ".") == 14: + layout = ShortDateTimeMilliFormat + case len(value) == 21 && strings.Index(value, ".") == 14: + layout = ShortDateTimeMicroFormat + case len(value) == 24 && strings.Index(value, ".") == 14: + layout = ShortDateTimeNanoFormat case len(value) == 25 && strings.Index(value, "T") == 10: layout = RFC3339Format case len(value) == 29 && strings.Index(value, "T") == 10 && strings.Index(value, ".") == 19: diff --git a/parser_test.go b/parser_test.go index 504959a..fe82b26 100755 --- a/parser_test.go +++ b/parser_test.go @@ -21,13 +21,16 @@ func TestCarbon_Parse(t *testing.T) { {"0000-00-00 00:00:00", ""}, {"2020-08-05 13:14:15", "2020-08-05 13:14:15 +0800 CST"}, - {"20200805131415", "2020-08-05 13:14:15 +0800 CST"}, - {"20200805", "2020-08-05 00:00:00 +0800 CST"}, {"2020-08-05", "2020-08-05 00:00:00 +0800 CST"}, {"2020-08-05T13:14:15+08:00", "2020-08-05 13:14:15 +0800 CST"}, {"2020-08-05T13:14:15.999+08:00", "2020-08-05 13:14:15.999 +0800 CST"}, {"2020-08-05T13:14:15.999999+08:00", "2020-08-05 13:14:15.999999 +0800 CST"}, {"2020-08-05T13:14:15.999999999+08:00", "2020-08-05 13:14:15.999999999 +0800 CST"}, + {"20200805131415", "2020-08-05 13:14:15 +0800 CST"}, + {"20200805131415.999", "2020-08-05 13:14:15.999 +0800 CST"}, + {"20200805131415.999999", "2020-08-05 13:14:15.999999 +0800 CST"}, + {"20200805131415.999999999", "2020-08-05 13:14:15.999999999 +0800 CST"}, + {"20200805", "2020-08-05 00:00:00 +0800 CST"}, } for index, test := range tests {