Skip to content

Commit 081ded0

Browse files
authored
time: fix quarter calculation in custom_format() (#25608)
1 parent 8198e9e commit 081ded0

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

vlib/time/custom_format_test.v

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,19 @@ fn test_zero_date() {
4242
res := zero_date.custom_format('M MM Mo MMM MMMM |1| D DD DDD DDDD |2| d dd ddd dddd |3| YY YYYY a A |4| H HH h hh k kk i ii e |5| m mm s ss |6| Do DDDo Q Qo QQ |7| N NN |8| M/D/YYYY N-HH:mm:ss Qo?a')
4343
assert res == '0 00 0th Jan January |1| 0 00 0 000 |2| -1 Mo Mon Monday |3| 0 am AM |4| 0 00 12 12 1 01 0 00 e |5| 0 00 0 00 |6| 0th 0th 1 1st 01 |7| AD Anno Domini |8| 0/0/0 AD-00:00:00 1st?am'
4444
}
45+
46+
fn test_quarter() {
47+
pattern := '2025-xx-27t12:34:56z'
48+
expect := [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]
49+
mut want := []int{len: 12}
50+
for n in 0 .. 12 {
51+
tt := time.parse_rfc3339(pattern.replace('xx', '${n + 1:02}'))!
52+
want[n] = tt.custom_format('Q').int()
53+
}
54+
assert want == expect
55+
56+
turing := time.parse_format('1912-06-23', 'YYYY-MM-DD')! // Alan Turing was born
57+
assert turing.custom_format('Qo') == '2nd'
58+
mccarthy := time.parse_format('1927-09-04', 'YYYY-MM-DD')! // John McCarthy was born
59+
assert mccarthy.custom_format('QQ') == '03'
60+
}

vlib/time/format.v

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -502,13 +502,13 @@ pub fn (t Time) custom_format(s string) string {
502502
sb.write_string(ordinal_suffix(t.week_of_year()))
503503
}
504504
'Q' {
505-
sb.write_string('${(t.month % 4) + 1}')
505+
sb.write_string('${(t.month - 1) / 3 + 1}')
506506
}
507507
'QQ' {
508-
sb.write_string('${(t.month % 4) + 1:02}')
508+
sb.write_string('${(t.month - 1) / 3 + 1:02}')
509509
}
510510
'Qo' {
511-
sb.write_string(ordinal_suffix((t.month % 4) + 1))
511+
sb.write_string(ordinal_suffix((t.month - 1) / 3 + 1))
512512
}
513513
'c' {
514514
sb.write_string('${t.day_of_week()}')

0 commit comments

Comments
 (0)