From 10df697d323bc73f75779345bab262a6a483b503 Mon Sep 17 00:00:00 2001 From: shove Date: Tue, 8 Aug 2023 17:25:39 +0800 Subject: [PATCH] time: add 'i', 'ii' in custom_format() for 12-hours clock(0-12-1-11) (#19083) --- vlib/time/custom_format_test.v | 50 +++++++++++++++++----------------- vlib/time/format.v | 16 +++++++++-- 2 files changed, 38 insertions(+), 28 deletions(-) diff --git a/vlib/time/custom_format_test.v b/vlib/time/custom_format_test.v index 6ce238f634..de6bf26c84 100644 --- a/vlib/time/custom_format_test.v +++ b/vlib/time/custom_format_test.v @@ -5,34 +5,34 @@ fn test_custom_format() { assert date.custom_format('YYYY-MM-DD HH:mm') == date.format() assert date.custom_format('MMM') == date.smonth() - test_str := 'M MM Mo MMM MMMM\nD DD DDD DDDD\nd dd ddd dddd\nYY YYYY a A\nH HH h hh k kk e\nm mm s ss Z ZZ ZZZ\nDo DDDo Q Qo QQ\nN NN w wo ww\nM/D/YYYY N-HH:mm:ss Qo?a' + test_str := 'M MM Mo MMM MMMM\nD DD DDD DDDD\nd dd ddd dddd\nYY YYYY a A\nH HH h hh k kk i ii e\nm mm s ss Z ZZ ZZZ\nDo DDDo Q Qo QQ\nN NN w wo ww\nM/D/YYYY N-HH:mm:ss Qo?a' println(date.custom_format(test_str)) } fn test_hours() { - assert time.parse('2023-08-04 00:00:45')!.custom_format('hh A h a') == '00 AM 0 am' - assert time.parse('2023-08-04 01:00:45')!.custom_format('hh A h a') == '01 AM 1 am' - assert time.parse('2023-08-04 02:00:45')!.custom_format('hh A h a') == '02 AM 2 am' - assert time.parse('2023-08-04 03:00:45')!.custom_format('hh A h a') == '03 AM 3 am' - assert time.parse('2023-08-04 04:00:45')!.custom_format('hh A h a') == '04 AM 4 am' - assert time.parse('2023-08-04 05:00:45')!.custom_format('hh A h a') == '05 AM 5 am' - assert time.parse('2023-08-04 06:00:45')!.custom_format('hh A h a') == '06 AM 6 am' - assert time.parse('2023-08-04 07:00:45')!.custom_format('hh A h a') == '07 AM 7 am' - assert time.parse('2023-08-04 08:00:45')!.custom_format('hh A h a') == '08 AM 8 am' - assert time.parse('2023-08-04 09:00:45')!.custom_format('hh A h a') == '09 AM 9 am' - assert time.parse('2023-08-04 10:00:45')!.custom_format('hh A h a') == '10 AM 10 am' - assert time.parse('2023-08-04 11:00:45')!.custom_format('hh A h a') == '11 AM 11 am' - assert time.parse('2023-08-04 12:00:45')!.custom_format('hh A h a') == '12 PM 12 pm' - assert time.parse('2023-08-04 13:00:45')!.custom_format('hh A h a') == '01 PM 1 pm' - assert time.parse('2023-08-04 14:00:45')!.custom_format('hh A h a') == '02 PM 2 pm' - assert time.parse('2023-08-04 15:00:45')!.custom_format('hh A h a') == '03 PM 3 pm' - assert time.parse('2023-08-04 16:00:45')!.custom_format('hh A h a') == '04 PM 4 pm' - assert time.parse('2023-08-04 17:00:45')!.custom_format('hh A h a') == '05 PM 5 pm' - assert time.parse('2023-08-04 18:00:45')!.custom_format('hh A h a') == '06 PM 6 pm' - assert time.parse('2023-08-04 19:00:45')!.custom_format('hh A h a') == '07 PM 7 pm' - assert time.parse('2023-08-04 20:00:45')!.custom_format('hh A h a') == '08 PM 8 pm' - assert time.parse('2023-08-04 21:00:45')!.custom_format('hh A h a') == '09 PM 9 pm' - assert time.parse('2023-08-04 22:00:45')!.custom_format('hh A h a') == '10 PM 10 pm' - assert time.parse('2023-08-04 23:00:45')!.custom_format('hh A h a') == '11 PM 11 pm' + assert time.parse('2023-08-04 00:00:45')!.custom_format('ii A i a hh A h a') == '00 AM 0 am 12 AM 12 am' + assert time.parse('2023-08-04 01:00:45')!.custom_format('ii A i a hh A h a') == '01 AM 1 am 01 AM 1 am' + assert time.parse('2023-08-04 02:00:45')!.custom_format('ii A i a hh A h a') == '02 AM 2 am 02 AM 2 am' + assert time.parse('2023-08-04 03:00:45')!.custom_format('ii A i a hh A h a') == '03 AM 3 am 03 AM 3 am' + assert time.parse('2023-08-04 04:00:45')!.custom_format('ii A i a hh A h a') == '04 AM 4 am 04 AM 4 am' + assert time.parse('2023-08-04 05:00:45')!.custom_format('ii A i a hh A h a') == '05 AM 5 am 05 AM 5 am' + assert time.parse('2023-08-04 06:00:45')!.custom_format('ii A i a hh A h a') == '06 AM 6 am 06 AM 6 am' + assert time.parse('2023-08-04 07:00:45')!.custom_format('ii A i a hh A h a') == '07 AM 7 am 07 AM 7 am' + assert time.parse('2023-08-04 08:00:45')!.custom_format('ii A i a hh A h a') == '08 AM 8 am 08 AM 8 am' + assert time.parse('2023-08-04 09:00:45')!.custom_format('ii A i a hh A h a') == '09 AM 9 am 09 AM 9 am' + assert time.parse('2023-08-04 10:00:45')!.custom_format('ii A i a hh A h a') == '10 AM 10 am 10 AM 10 am' + assert time.parse('2023-08-04 11:00:45')!.custom_format('ii A i a hh A h a') == '11 AM 11 am 11 AM 11 am' + assert time.parse('2023-08-04 12:00:45')!.custom_format('ii A i a hh A h a') == '12 PM 12 pm 12 PM 12 pm' + assert time.parse('2023-08-04 13:00:45')!.custom_format('ii A i a hh A h a') == '01 PM 1 pm 01 PM 1 pm' + assert time.parse('2023-08-04 14:00:45')!.custom_format('ii A i a hh A h a') == '02 PM 2 pm 02 PM 2 pm' + assert time.parse('2023-08-04 15:00:45')!.custom_format('ii A i a hh A h a') == '03 PM 3 pm 03 PM 3 pm' + assert time.parse('2023-08-04 16:00:45')!.custom_format('ii A i a hh A h a') == '04 PM 4 pm 04 PM 4 pm' + assert time.parse('2023-08-04 17:00:45')!.custom_format('ii A i a hh A h a') == '05 PM 5 pm 05 PM 5 pm' + assert time.parse('2023-08-04 18:00:45')!.custom_format('ii A i a hh A h a') == '06 PM 6 pm 06 PM 6 pm' + assert time.parse('2023-08-04 19:00:45')!.custom_format('ii A i a hh A h a') == '07 PM 7 pm 07 PM 7 pm' + assert time.parse('2023-08-04 20:00:45')!.custom_format('ii A i a hh A h a') == '08 PM 8 pm 08 PM 8 pm' + assert time.parse('2023-08-04 21:00:45')!.custom_format('ii A i a hh A h a') == '09 PM 9 pm 09 PM 9 pm' + assert time.parse('2023-08-04 22:00:45')!.custom_format('ii A i a hh A h a') == '10 PM 10 pm 10 PM 10 pm' + assert time.parse('2023-08-04 23:00:45')!.custom_format('ii A i a hh A h a') == '11 PM 11 pm 11 PM 11 pm' } diff --git a/vlib/time/format.v b/vlib/time/format.v index 79f913610d..1571ed127b 100644 --- a/vlib/time/format.v +++ b/vlib/time/format.v @@ -96,8 +96,8 @@ fn ordinal_suffix(n int) string { } const ( - tokens_2 = ['MM', 'Mo', 'DD', 'Do', 'YY', 'ss', 'kk', 'NN', 'mm', 'hh', 'HH', 'ZZ', 'dd', 'Qo', - 'QQ', 'wo', 'ww'] + tokens_2 = ['MM', 'Mo', 'DD', 'Do', 'YY', 'ss', 'kk', 'NN', 'mm', 'hh', 'HH', 'ii', 'ZZ', 'dd', + 'Qo', 'QQ', 'wo', 'ww'] tokens_3 = ['MMM', 'DDD', 'ZZZ', 'ddd'] tokens_4 = ['MMMM', 'DDDD', 'DDDo', 'dddd', 'YYYY'] ) @@ -138,6 +138,8 @@ const ( // | | HH | 00 01 ... 22 23 | // | | h | 1 2 ... 11 12 | // | | hh | 01 02 ... 11 12 | +// | | i | 0 1 ... 11 12 1 ... 11 | +// | | ii | 00 01 ... 11 12 01 ... 11 | // | | k | 1 2 ... 23 24 | // | | kk | 01 02 ... 23 24 | // | **Minute** | m | 0 1 ... 58 59 | @@ -232,10 +234,18 @@ pub fn (t Time) custom_format(s string) string { sb.write_string('${t.hour:02}') } 'h' { - h := if t.hour > 12 { t.hour - 12 } else { t.hour } + h := (t.hour + 11) % 12 + 1 sb.write_string(h.str()) } 'hh' { + h := (t.hour + 11) % 12 + 1 + sb.write_string('${h:02}') + } + 'i' { + h := if t.hour > 12 { t.hour - 12 } else { t.hour } + sb.write_string(h.str()) + } + 'ii' { h := if t.hour > 12 { t.hour - 12 } else { t.hour } sb.write_string('${h:02}') }