Skip to content

Commit c790afa

Browse files
authored
time: add MMM support for parse_format() (#19284)
1 parent 3739175 commit c790afa

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

vlib/time/date_time_parser.v

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,19 @@ fn (mut p DateTimeParser) must_be_valid_month() !int {
8888
return error_invalid_time(0, 'invalid month name')
8989
}
9090

91+
fn (mut p DateTimeParser) must_be_valid_three_letter_month() !int {
92+
for month_number := 1; month_number < long_months.len; month_number++ {
93+
if p.current_pos_datetime + 3 < p.datetime.len {
94+
month_three_letters := p.datetime[p.current_pos_datetime..p.current_pos_datetime + 3]
95+
if months_string[(month_number - 1) * 3..month_number * 3] == month_three_letters {
96+
p.current_pos_datetime += 3
97+
return month_number
98+
}
99+
}
100+
}
101+
return error_invalid_time(0, 'invalid month three letters')
102+
}
103+
91104
fn (mut p DateTimeParser) must_be_valid_week_day(letters int) !string {
92105
val := p.next(letters)!
93106
for v in long_days {
@@ -171,6 +184,9 @@ fn (mut p DateTimeParser) parse() !Time {
171184
return error_invalid_time(0, 'month must be between 01 and 12')
172185
}
173186
}
187+
'MMM' {
188+
month_ = p.must_be_valid_three_letter_month() or { return err }
189+
}
174190
'MMMM' {
175191
month_ = p.must_be_valid_month() or { return err }
176192
}

vlib/time/time_test.v

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,3 +336,11 @@ fn test_plus_equals_duration() {
336336
d += time.second
337337
assert d == 2 * time.second
338338
}
339+
340+
fn test_parse_three_letters_month() {
341+
tm := time.now()
342+
format := 'MMM DD HH:mm:ss YYYY'
343+
tm_s := tm.custom_format(format)
344+
tm_tm := time.parse_format(tm_s, format)!
345+
assert tm_tm.month == tm.month
346+
}

0 commit comments

Comments
 (0)