1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

time: add 'i', 'ii' in custom_format() for 12-hours clock(0-12-1-11) (#19083)

This commit is contained in:
shove 2023-08-08 17:25:39 +08:00 committed by GitHub
parent 68f18fcb8e
commit 10df697d32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 28 deletions

View File

@ -5,34 +5,34 @@ fn test_custom_format() {
assert date.custom_format('YYYY-MM-DD HH:mm') == date.format() assert date.custom_format('YYYY-MM-DD HH:mm') == date.format()
assert date.custom_format('MMM') == date.smonth() 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)) println(date.custom_format(test_str))
} }
fn test_hours() { 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 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('hh A h a') == '01 AM 1 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('hh A h a') == '02 AM 2 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('hh A h a') == '03 AM 3 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('hh A h a') == '04 AM 4 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('hh A h a') == '05 AM 5 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('hh A h a') == '06 AM 6 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('hh A h a') == '07 AM 7 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('hh A h a') == '08 AM 8 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('hh A h a') == '09 AM 9 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('hh A h a') == '10 AM 10 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('hh A h a') == '11 AM 11 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('hh A h a') == '12 PM 12 pm' 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('hh A h a') == '01 PM 1 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('hh A h a') == '02 PM 2 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('hh A h a') == '03 PM 3 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('hh A h a') == '04 PM 4 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('hh A h a') == '05 PM 5 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('hh A h a') == '06 PM 6 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('hh A h a') == '07 PM 7 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('hh A h a') == '08 PM 8 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('hh A h a') == '09 PM 9 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('hh A h a') == '10 PM 10 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('hh A h a') == '11 PM 11 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'
} }

View File

@ -96,8 +96,8 @@ fn ordinal_suffix(n int) string {
} }
const ( const (
tokens_2 = ['MM', 'Mo', 'DD', 'Do', 'YY', 'ss', 'kk', 'NN', 'mm', 'hh', 'HH', 'ZZ', 'dd', 'Qo', tokens_2 = ['MM', 'Mo', 'DD', 'Do', 'YY', 'ss', 'kk', 'NN', 'mm', 'hh', 'HH', 'ii', 'ZZ', 'dd',
'QQ', 'wo', 'ww'] 'Qo', 'QQ', 'wo', 'ww']
tokens_3 = ['MMM', 'DDD', 'ZZZ', 'ddd'] tokens_3 = ['MMM', 'DDD', 'ZZZ', 'ddd']
tokens_4 = ['MMMM', 'DDDD', 'DDDo', 'dddd', 'YYYY'] tokens_4 = ['MMMM', 'DDDD', 'DDDo', 'dddd', 'YYYY']
) )
@ -138,6 +138,8 @@ const (
// | | HH | 00 01 ... 22 23 | // | | HH | 00 01 ... 22 23 |
// | | h | 1 2 ... 11 12 | // | | h | 1 2 ... 11 12 |
// | | hh | 01 02 ... 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 | // | | k | 1 2 ... 23 24 |
// | | kk | 01 02 ... 23 24 | // | | kk | 01 02 ... 23 24 |
// | **Minute** | m | 0 1 ... 58 59 | // | **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}') sb.write_string('${t.hour:02}')
} }
'h' { 'h' {
h := if t.hour > 12 { t.hour - 12 } else { t.hour } h := (t.hour + 11) % 12 + 1
sb.write_string(h.str()) sb.write_string(h.str())
} }
'hh' { '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 } h := if t.hour > 12 { t.hour - 12 } else { t.hour }
sb.write_string('${h:02}') sb.write_string('${h:02}')
} }