添加星期值的int形式

This commit is contained in:
jroam
2019-04-21 19:36:06 +08:00
parent 90e6f685b7
commit a69934a7e3
3 changed files with 31 additions and 2 deletions

View File

@ -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]
}

View File

@ -0,0 +1 @@
package gtime_test

View File

@ -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()
})
}