From a33ea3d0139983635a2ef799c92a10d430b2fe51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=87=95=E5=BD=92=E6=9D=A5?= Date: Mon, 29 May 2023 20:08:17 +0800 Subject: [PATCH] fix: timeFormat["yyyy-mm-dd hh"] should be "2006-01-02 15" (#99) (#100) * fix: timeFormat["yyyy-mm-dd hh"] should be "2006-01-02 15" (#99) * test: add use cases for FormatTimeToStr --- datetime/datetime.go | 2 +- datetime/datetime_example_test.go | 3 +++ datetime/datetime_test.go | 8 ++++++-- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/datetime/datetime.go b/datetime/datetime.go index 9cd163e..2f8d51e 100644 --- a/datetime/datetime.go +++ b/datetime/datetime.go @@ -35,7 +35,7 @@ func init() { timeFormat = map[string]string{ "yyyy-mm-dd hh:mm:ss": "2006-01-02 15:04:05", "yyyy-mm-dd hh:mm": "2006-01-02 15:04", - "yyyy-mm-dd hh": "2006-01-02 15:04", + "yyyy-mm-dd hh": "2006-01-02 15", "yyyy-mm-dd": "2006-01-02", "yyyy-mm": "2006-01", "mm-dd": "01-02", diff --git a/datetime/datetime_example_test.go b/datetime/datetime_example_test.go index d789886..1d7ba6c 100644 --- a/datetime/datetime_example_test.go +++ b/datetime/datetime_example_test.go @@ -131,15 +131,18 @@ func ExampleFormatTimeToStr() { result1 := FormatTimeToStr(datetime, "yyyy-mm-dd hh:mm:ss") result2 := FormatTimeToStr(datetime, "yyyy-mm-dd") result3 := FormatTimeToStr(datetime, "dd-mm-yy hh:mm:ss") + result4 := FormatTimeToStr(datetime, "yyyy-mm-dd hh") fmt.Println(result1) fmt.Println(result2) fmt.Println(result3) + fmt.Println(result4) // Output: // 2021-01-02 16:04:08 // 2021-01-02 // 02-01-21 16:04:08 + // 2021-01-02 16 } func ExampleFormatStrToTime() { diff --git a/datetime/datetime_test.go b/datetime/datetime_test.go index cee993b..3d53da6 100644 --- a/datetime/datetime_test.go +++ b/datetime/datetime_test.go @@ -84,12 +84,16 @@ func TestFormatTimeToStr(t *testing.T) { cases := []string{ "yyyy-mm-dd hh:mm:ss", "yyyy-mm-dd", "dd-mm-yy hh:mm:ss", "yyyy/mm/dd hh:mm:ss", - "hh:mm:ss", "yyyy/mm"} + "hh:mm:ss", "yyyy/mm", + "yyyy-mm-dd hh", + } expected := []string{ "2021-01-02 16:04:08", "2021-01-02", "02-01-21 16:04:08", "2021/01/02 16:04:08", - "16:04:08", "2021/01"} + "16:04:08", "2021/01", + "2021-01-02 16", + } for i := 0; i < len(cases); i++ { actual := FormatTimeToStr(datetime, cases[i])