Date formatting fix.

This commit is contained in:
Nuno Cruces 2022-03-30 19:27:54 +01:00
parent 0086cdcbf0
commit 81c6f396fe
2 changed files with 20 additions and 13 deletions

View file

@ -32,18 +32,18 @@ func (p *parser) parse() error {
state = nopadding state = nopadding
continue continue
} }
if s, ok := p.specs[b]; ok { if fmt, ok := p.specs[b]; ok {
err = p.writeFmt(s) err = p.writeFmt(fmt)
} else { } else {
err = p.fallback(b, true) err = p.fallback(b, true)
} }
state = initial state = initial
case nopadding: case nopadding:
if s, ok := p.unpadded[b]; ok { if fmt, ok := p.unpadded[b]; ok {
err = p.writeFmt(s) err = p.writeFmt(fmt)
} else if s, ok := p.specs[b]; ok { } else if fmt, ok := p.specs[b]; ok {
err = p.writeFmt(s) err = p.writeFmt(fmt)
} else { } else {
err = p.fallback(b, false) err = p.fallback(b, false)
} }

View file

@ -19,10 +19,10 @@ func Format(fmt string, t time.Time) string {
parser.writeFmt = func(fmt string) error { parser.writeFmt = func(fmt string) error {
switch fmt { switch fmt {
default:
res.WriteString(t.Format(fmt))
case "000", "000000", "000000000": case "000", "000000", "000000000":
res.WriteString(t.Format("." + fmt)[1:]) res.WriteString(t.Format("." + fmt)[1:])
default:
res.WriteString(t.Format(fmt))
} }
return nil return nil
} }
@ -80,14 +80,21 @@ func Layout(fmt string) (string, error) {
parser.writeLit = func(b byte) error { parser.writeLit = func(b byte) error {
if bytes.IndexByte([]byte("MonJan_0123456789"), b) >= 0 { if bytes.IndexByte([]byte("MonJan_0123456789"), b) >= 0 {
return errors.New("strftime: unsupported literal: " + string(b)) return errors.New("strftime: unsupported literal: '" + string(b) + "'")
} }
res.WriteByte(b) res.WriteByte(b)
return nil return nil
} }
parser.writeFmt = func(s string) error { parser.writeFmt = func(fmt string) error {
res.WriteString(s) switch fmt {
case "000", "000000", "000000000":
res := res.String()
if len := len(res); len <= 0 || res[len-1] != '.' && res[len-1] != ',' {
return errors.New("strftime: unsupported specifier: fractional seconds must follow '.' or ','")
}
}
res.WriteString(fmt)
return nil return nil
} }
@ -126,12 +133,12 @@ func UTS35(fmt string) (string, error) {
return nil return nil
} }
parser.writeFmt = func(s string) error { parser.writeFmt = func(fmt string) error {
if literal { if literal {
literal = false literal = false
res.WriteByte(quote) res.WriteByte(quote)
} }
res.WriteString(s) res.WriteString(fmt)
return nil return nil
} }