Skip to content

Commit dc2a7b2

Browse files
authored
time: fix custom_format panic (fix #24977) (#24987)
1 parent f719c2c commit dc2a7b2

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

vlib/time/format.v

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,10 +401,20 @@ pub fn (t Time) custom_format(s string) string {
401401
sb.write_string(ordinal_suffix(t.month))
402402
}
403403
'MMM' {
404-
sb.write_string(long_months[t.month - 1][0..3])
404+
m := if t.month >= 1 && t.month <= 12 {
405+
long_months[t.month - 1][0..3]
406+
} else {
407+
long_months[0][0..3]
408+
}
409+
sb.write_string(m)
405410
}
406411
'MMMM' {
407-
sb.write_string(long_months[t.month - 1])
412+
m := if t.month >= 1 && t.month <= 12 {
413+
long_months[t.month - 1]
414+
} else {
415+
long_months[0]
416+
}
417+
sb.write_string(m)
408418
}
409419
'D' {
410420
sb.write_string(t.day.str())

vlib/time/time_test.v

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,3 +476,9 @@ fn test_parse_weekday() {
476476
tm_s := tm.custom_format(format)
477477
assert tm_s == 'Thursday Jan 01 00:00:00 1970'
478478
}
479+
480+
fn test_empty_time() {
481+
t := time.Time{}
482+
assert t.unix() == -62169984000
483+
assert t.custom_format('MMMM YYYY') == 'January 0'
484+
}

0 commit comments

Comments
 (0)