From 3efe511f4287a1bf0bbcdef865525d6f083f2559 Mon Sep 17 00:00:00 2001 From: jroam Date: Mon, 22 Apr 2019 23:13:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=98=9F=E6=9C=9F=E8=8B=B1?= =?UTF-8?q?=E6=96=87=E5=80=BC=E5=92=8C=E6=95=B0=E5=AD=97=E5=80=BC=E7=9A=84?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/os/gtime/gtime_format.go | 16 +++++++++++++--- g/os/gtime/gtime_z_unit_format_test.go | 1 + 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/g/os/gtime/gtime_format.go b/g/os/gtime/gtime_format.go index 4df9db0a3..d94a90c08 100644 --- a/g/os/gtime/gtime_format.go +++ b/g/os/gtime/gtime_format.go @@ -10,7 +10,6 @@ import ( "bytes" "github.com/gogf/gf/g/text/gregex" "github.com/gogf/gf/g/text/gstr" - "strconv" "strings" ) @@ -20,7 +19,7 @@ var ( // ================== 日 ================== 'd': "02", // 月份中的第几天,有前导零的 2 位数字(01 到 31) 'D': "Mon", // 星期中的第几天,文本表示,3 个字母(Mon 到 Sun) - 'w': "", // 星期中的第几天,数字型式的文本表示 0为星期天 6为星期六 + 'w': "Monday", // 星期中的第几天,数字型式的文本表示 0为星期天 6为星期六 'j': "=j=02", // 月份中的第几天,没有前导零(1 到 31) 'l': "Monday", // ("L"的小写字母)星期几,完整的文本格式(Sunday 到 Saturday) @@ -54,6 +53,17 @@ var ( 'c': "2006-01-02T15:04:05-07:00", // ISO 8601 格式的日期,例如:2004-02-12T15:19:21+00:00 'r': "Mon, 02 Jan 06 15:04 MST", // RFC 822 格式的日期,例如:Thu, 21 Dec 2000 16:01:07 +0200 } + + // 星期的英文值和数字值对应map + weekentoi = map[string]string{ + "Sunday": "0", + "Monday": "1", + "Tuesday": "2", + "Wednesday": "3", + "Thursday": "4", + "Friday": "5", + "Saturday": "6", + } ) // 将自定义的格式转换为标准库时间格式 @@ -136,7 +146,7 @@ func (t *Time) Format(format string) string { case 'u': buffer.WriteString(strings.Replace(result, "=u=.", "", -1)) case 'w': - buffer.WriteString(strconv.Itoa(int(t.Weekday()))) + buffer.WriteString(weekentoi[result]) default: buffer.WriteString(result) } diff --git a/g/os/gtime/gtime_z_unit_format_test.go b/g/os/gtime/gtime_z_unit_format_test.go index 2103b3a0f..3d2028d29 100644 --- a/g/os/gtime/gtime_z_unit_format_test.go +++ b/g/os/gtime/gtime_z_unit_format_test.go @@ -46,6 +46,7 @@ func Test_Format(t *testing.T) { times := []map[string]string{ {"k": "2019-04-22", "f": "w", "r": "1"}, {"k": "2019-04-27", "f": "w", "r": "6"}, + {"k": "2019-03-10", "f": "w", "r": "0"}, {"k": "2019-03-10", "f": "Y-m-d 星期:w", "r": "2019-03-10 星期:0"}, }