diff --git a/vlib/time/format.v b/vlib/time/format.v index 691640d997..8922d615e8 100644 --- a/vlib/time/format.v +++ b/vlib/time/format.v @@ -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()) diff --git a/vlib/time/time_test.v b/vlib/time/time_test.v index e029e62ebf..9531a41183 100644 --- a/vlib/time/time_test.v +++ b/vlib/time/time_test.v @@ -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' +}