| 1 | import time |
| 2 | |
| 3 | fn test_custom_format() { |
| 4 | date := time.now() |
| 5 | assert date.custom_format('YYYY-MM-DD HH:mm') == date.format() |
| 6 | assert date.custom_format('MMM') == date.smonth() |
| 7 | |
| 8 | 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' |
| 9 | |
| 10 | println(date.custom_format(test_str)) |
| 11 | } |
| 12 | |
| 13 | fn test_hours() { |
| 14 | 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' |
| 15 | 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' |
| 16 | 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' |
| 17 | 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' |
| 18 | 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' |
| 19 | 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' |
| 20 | 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' |
| 21 | 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' |
| 22 | 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' |
| 23 | 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' |
| 24 | 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' |
| 25 | 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' |
| 26 | 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' |
| 27 | 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' |
| 28 | 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' |
| 29 | 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' |
| 30 | 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' |
| 31 | 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' |
| 32 | 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' |
| 33 | 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' |
| 34 | 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' |
| 35 | 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' |
| 36 | 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' |
| 37 | 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' |
| 38 | } |
| 39 | |
| 40 | fn test_hours_k_and_kk() { |
| 41 | test_time := time.Time{ |
| 42 | year: 2024 |
| 43 | month: 7 |
| 44 | day: 15 |
| 45 | hour: 14 |
| 46 | minute: 30 |
| 47 | second: 45 |
| 48 | nanosecond: 123456789 |
| 49 | } |
| 50 | assert test_time.custom_format('MM.DD.YYYY k:mm:ss') == '07.15.2024 14:30:45' |
| 51 | assert test_time.custom_format('MM.DD.YYYY kk:mm:ss') == '07.15.2024 14:30:45' |
| 52 | } |
| 53 | |
| 54 | fn test_zero_date() { |
| 55 | zero_date := time.Time{} |
| 56 | res := |
| 57 | 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') |
| 58 | 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 24 24 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' |
| 59 | } |
| 60 | |
| 61 | fn test_quarter() { |
| 62 | pattern := '2025-xx-27t12:34:56z' |
| 63 | expect := [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4] |
| 64 | mut want := []int{len: 12} |
| 65 | for n in 0 .. 12 { |
| 66 | tt := time.parse_rfc3339(pattern.replace('xx', '${n + 1:02}'))! |
| 67 | want[n] = tt.custom_format('Q').int() |
| 68 | } |
| 69 | assert want == expect |
| 70 | |
| 71 | turing := time.parse_format('1912-06-23', 'YYYY-MM-DD')! // Alan Turing was born |
| 72 | assert turing.custom_format('Qo') == '2nd' |
| 73 | mccarthy := time.parse_format('1927-09-04', 'YYYY-MM-DD')! // John McCarthy was born |
| 74 | assert mccarthy.custom_format('QQ') == '03' |
| 75 | } |
| 76 | |