diff --git a/vlib/time/date_time_parser.v b/vlib/time/date_time_parser.v index d43f658448..e3431a92cc 100644 --- a/vlib/time/date_time_parser.v +++ b/vlib/time/date_time_parser.v @@ -88,6 +88,19 @@ fn (mut p DateTimeParser) must_be_valid_month() !int { return error_invalid_time(0, 'invalid month name') } +fn (mut p DateTimeParser) must_be_valid_three_letter_month() !int { + for month_number := 1; month_number < long_months.len; month_number++ { + if p.current_pos_datetime + 3 < p.datetime.len { + month_three_letters := p.datetime[p.current_pos_datetime..p.current_pos_datetime + 3] + if months_string[(month_number - 1) * 3..month_number * 3] == month_three_letters { + p.current_pos_datetime += 3 + return month_number + } + } + } + return error_invalid_time(0, 'invalid month three letters') +} + fn (mut p DateTimeParser) must_be_valid_week_day(letters int) !string { val := p.next(letters)! for v in long_days { @@ -171,6 +184,9 @@ fn (mut p DateTimeParser) parse() !Time { return error_invalid_time(0, 'month must be between 01 and 12') } } + 'MMM' { + month_ = p.must_be_valid_three_letter_month() or { return err } + } 'MMMM' { month_ = p.must_be_valid_month() or { return err } } diff --git a/vlib/time/time_test.v b/vlib/time/time_test.v index 6791f52e40..473c599c75 100644 --- a/vlib/time/time_test.v +++ b/vlib/time/time_test.v @@ -336,3 +336,11 @@ fn test_plus_equals_duration() { d += time.second assert d == 2 * time.second } + +fn test_parse_three_letters_month() { + tm := time.now() + format := 'MMM DD HH:mm:ss YYYY' + tm_s := tm.custom_format(format) + tm_tm := time.parse_format(tm_s, format)! + assert tm_tm.month == tm.month +}