From a69934a7e37e7ad5e9ec716e18fbd68c7b5d1853 Mon Sep 17 00:00:00 2001 From: jroam Date: Sun, 21 Apr 2019 19:36:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=98=9F=E6=9C=9F=E5=80=BC?= =?UTF-8?q?=E7=9A=84int=E5=BD=A2=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/os/gtime/gtime_format.go | 20 ++++++++++++++++++++ g/os/gtime/gtime_format_test.go | 1 + g/os/gtime/gtime_z_unit_format_test.go | 12 ++++++++++-- 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 g/os/gtime/gtime_format_test.go diff --git a/g/os/gtime/gtime_format.go b/g/os/gtime/gtime_format.go index 41622c915..3fff61f8a 100644 --- a/g/os/gtime/gtime_format.go +++ b/g/os/gtime/gtime_format.go @@ -6,6 +6,8 @@ package gtime + + import ( "bytes" "github.com/gogf/gf/g/text/gregex" @@ -19,6 +21,7 @@ var ( // ================== 日 ================== 'd' : "02", // 月份中的第几天,有前导零的 2 位数字(01 到 31) 'D' : "Mon", // 星期中的第几天,文本表示,3 个字母(Mon 到 Sun) + 'w' : "Monday", // 星期中的第几天,数字表示 0为星期天 6为星期六 'j' : "=j=02", // 月份中的第几天,没有前导零(1 到 31) 'l' : "Monday", // ("L"的小写字母)星期几,完整的文本格式(Sunday 到 Saturday) @@ -130,6 +133,7 @@ func (t *Time) Format(format string) string { case 'j': buffer.WriteString(gstr.ReplaceByArray(result, []string{"=j=0", "", "=j=", ""})) case 'G': buffer.WriteString(gstr.ReplaceByArray(result, []string{"=G=0", "", "=G=", ""})) case 'u': buffer.WriteString(strings.Replace(result, "=u=.", "", -1)) + case 'w': buffer.WriteString(weekstoint(result)) default: buffer.WriteString(result) } @@ -145,4 +149,20 @@ func (t *Time) Format(format string) string { // 格式化,使用标准库格式 func (t *Time) Layout(layout string) string { return t.Time.Format(layout) +} + +// 将字母的星期数字转为数字:0为星期天 6为星期六 +func weekstoint(week string) string{ + + weeks:=map[string]string{ + "Monday":"1", + "Tuesday":"2", + "Wednesday":"3", + "Thursday":"4", + "Friday":"5", + "Saturday ":"6", + "Sunday":"0", + } + + return weeks[week] } \ No newline at end of file diff --git a/g/os/gtime/gtime_format_test.go b/g/os/gtime/gtime_format_test.go new file mode 100644 index 000000000..e3b78cbcc --- /dev/null +++ b/g/os/gtime/gtime_format_test.go @@ -0,0 +1 @@ +package gtime_test diff --git a/g/os/gtime/gtime_z_unit_format_test.go b/g/os/gtime/gtime_z_unit_format_test.go index 9d6fb6750..a4c1162c7 100644 --- a/g/os/gtime/gtime_z_unit_format_test.go +++ b/g/os/gtime/gtime_z_unit_format_test.go @@ -1,10 +1,9 @@ package gtime_test import ( - "testing" - "github.com/gogf/gf/g/os/gtime" "github.com/gogf/gf/g/test/gtest" + "testing" ) func Test_Format(t *testing.T) { @@ -30,6 +29,8 @@ func Test_Format(t *testing.T) { gtest.Assert(timeTemp.Format("c"), "2006-01-11T15:04:05+08:00") + + //补零 timeTemp1, err := gtime.StrToTime("2006-01-02 03:04:05", "Y-m-d H:i:s") if err != nil { @@ -43,6 +44,13 @@ func Test_Format(t *testing.T) { } gtest.Assert(timeTemp2.Format("Y-n-j G:i:s"), "2006-1-2 3:04:05") + // 测试星期值 + //time1:=time.Friday() + + + + + }) }