feat(template/timeformat): backend time render synchronous with frontend

This commit is contained in:
kumfo 2022-12-05 15:20:51 +08:00
parent 23bb488731
commit 892d76928f
1 changed files with 23 additions and 19 deletions

View File

@ -32,10 +32,11 @@ func Format(unix int64, format, tz string) (formatted string) {
format = strings.ReplaceAll(format, placeholders[i].old, placeholders[i].new) format = strings.ReplaceAll(format, placeholders[i].old, placeholders[i].new)
}*/ }*/
toFormat := "" toFormat := ""
for len(format) > 0 { from := []rune(format)
to, suffix := nextStdChunk(format) for len(from) > 0 {
toFormat += to to, suffix := nextStdChunk(from)
format = suffix toFormat += string(to)
from = suffix
} }
_, _ = time.LoadLocation(tz) _, _ = time.LoadLocation(tz)
@ -43,26 +44,27 @@ func Format(unix int64, format, tz string) (formatted string) {
return return
} }
func nextStdChunk(from string) (to, suffix string) { func nextStdChunk(from []rune) (to, suffix []rune) {
if len(from) == 0 { if len(from) == 0 {
to = "" to = []rune{}
suffix = "" suffix = []rune{}
return return
} }
s := string(from[0]) s := string(from[0])
old := "" old := ""
switch s { switch s {
case "Y": case "Y":
if len(from) >= 4 && from[:4] == "YYYY" { if len(from) >= 4 && string(from[:4]) == "YYYY" {
old = "YYYY" old = "YYYY"
} else if len(from) >= 2 && from[:2] == "YY" { } else if len(from) >= 2 && string(from[:2]) == "YY" {
old = "YY" old = "YY"
} }
case "M": case "M":
for i := 4; i > 0; i-- { for i := 4; i > 0; i-- {
if len(from) >= i { if len(from) >= i {
switch from[:i] { switch string(from[:i]) {
case "MMMM": case "MMMM":
old = "MMMM" old = "MMMM"
case "MMM": case "MMM":
@ -80,7 +82,7 @@ func nextStdChunk(from string) (to, suffix string) {
case "D": case "D":
for i := 2; i >= 0; i-- { for i := 2; i >= 0; i-- {
if len(from) >= i { if len(from) >= i {
switch from[:i] { switch string(from[:i]) {
case "DD": case "DD":
old = "DD" old = "DD"
case "D": case "D":
@ -94,7 +96,7 @@ func nextStdChunk(from string) (to, suffix string) {
case "H": case "H":
for i := 2; i >= 0; i-- { for i := 2; i >= 0; i-- {
if len(from) >= i { if len(from) >= i {
switch from[:i] { switch string(from[:i]) {
case "HH": case "HH":
old = "HH" old = "HH"
case "H": case "H":
@ -108,7 +110,7 @@ func nextStdChunk(from string) (to, suffix string) {
case "h": case "h":
for i := 2; i >= 0; i-- { for i := 2; i >= 0; i-- {
if len(from) >= i { if len(from) >= i {
switch from[:i] { switch string(from[:i]) {
case "hh": case "hh":
old = "hh" old = "hh"
case "h": case "h":
@ -122,7 +124,7 @@ func nextStdChunk(from string) (to, suffix string) {
case "m": case "m":
for i := 2; i >= 0; i-- { for i := 2; i >= 0; i-- {
if len(from) >= i { if len(from) >= i {
switch from[:i] { switch string(from[:i]) {
case "mm": case "mm":
old = "mm" old = "mm"
case "m": case "m":
@ -136,7 +138,7 @@ func nextStdChunk(from string) (to, suffix string) {
case "s": case "s":
for i := 2; i >= 0; i-- { for i := 2; i >= 0; i-- {
if len(from) >= i { if len(from) >= i {
switch from[:i] { switch string(from[:i]) {
case "ss": case "ss":
old = "ss" old = "ss"
case "s": case "s":
@ -152,18 +154,20 @@ func nextStdChunk(from string) (to, suffix string) {
case "a": case "a":
old = "a" old = "a"
case "[": case "[":
if len(from) >= 4 && from[:4] == "[at]" { if len(from) >= 4 && string(from[:4]) == "[at]" {
old = "[at]" old = "[at]"
} }
default: default:
old = s old = s
} }
to, ok := placeholder[old] tos, ok := placeholder[old]
if !ok { if !ok {
to = old to = []rune(old)
} else {
to = []rune(tos)
} }
suffix = from[len(old):] suffix = from[len([]rune(old)):]
return return
} }