Skip to content

Commit

Permalink
time: add MMM support for parse_format() (#19284)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffangelion committed Sep 6, 2023
1 parent 3739175 commit c790afa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
16 changes: 16 additions & 0 deletions vlib/time/date_time_parser.v
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 }
}
Expand Down
8 changes: 8 additions & 0 deletions vlib/time/time_test.v
Expand Up @@ -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
}

0 comments on commit c790afa

Please sign in to comment.