优化星期英文值和数字值的格式化功能

This commit is contained in:
jroam
2019-04-22 23:13:35 +08:00
parent e6fb41504c
commit 3efe511f42
2 changed files with 14 additions and 3 deletions

View File

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

View File

@ -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"},
}