time: fix custom_format panic (fix #24977) (#24987)

This commit is contained in:
kbkpbot 2025-07-27 20:46:56 +08:00 committed by GitHub
parent f719c2cbe3
commit dc2a7b2b7c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 2 deletions

View File

@ -401,10 +401,20 @@ pub fn (t Time) custom_format(s string) string {
sb.write_string(ordinal_suffix(t.month))
}
'MMM' {
sb.write_string(long_months[t.month - 1][0..3])
m := if t.month >= 1 && t.month <= 12 {
long_months[t.month - 1][0..3]
} else {
long_months[0][0..3]
}
sb.write_string(m)
}
'MMMM' {
sb.write_string(long_months[t.month - 1])
m := if t.month >= 1 && t.month <= 12 {
long_months[t.month - 1]
} else {
long_months[0]
}
sb.write_string(m)
}
'D' {
sb.write_string(t.day.str())

View File

@ -476,3 +476,9 @@ fn test_parse_weekday() {
tm_s := tm.custom_format(format)
assert tm_s == 'Thursday Jan 01 00:00:00 1970'
}
fn test_empty_time() {
t := time.Time{}
assert t.unix() == -62169984000
assert t.custom_format('MMMM YYYY') == 'January 0'
}