time: add MMM support for parse_format() (#19284)

This commit is contained in:
Ivan Vatlin 2023-09-06 18:46:57 +03:00 committed by GitHub
parent 3739175fdb
commit c790afabdc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View File

@ -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 }
}

View File

@ -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
}