github.com/Aoi-hosizora/ahlib@v1.5.1-0.20230404072829-241b93cf91c7/xtime/strftime_test.go (about) 1 package xtime 2 3 import ( 4 "github.com/Aoi-hosizora/ahlib/xtesting" 5 "testing" 6 "time" 7 ) 8 9 func TestPadItoa(t *testing.T) { 10 for _, tc := range []struct { 11 giveNum int 12 giveDigit int 13 giveSpace bool 14 want string 15 }{ 16 {-1, 1, false, "???"}, 17 {-1, 2, false, "???"}, 18 {-1, 3, false, "???"}, 19 {-1, 4, false, "???"}, 20 {0, 1, false, "0"}, 21 {0, 2, false, "00"}, 22 {0, 3, false, "000"}, 23 {0, 4, false, "0000"}, 24 {10, 1, false, "10"}, 25 {10, 2, false, "10"}, 26 {10, 3, false, "010"}, 27 {10, 4, false, "0010"}, 28 {100, 1, false, "100"}, 29 {100, 2, false, "100"}, 30 {100, 3, false, "100"}, 31 {100, 4, false, "0100"}, 32 {1000, 1, false, "1000"}, 33 {1000, 2, false, "1000"}, 34 {1000, 3, false, "1000"}, 35 {1000, 4, false, "1000"}, 36 {1, 1, true, "1"}, 37 {1, 2, true, " 1"}, 38 {1, 3, true, " 1"}, 39 {1, 4, true, " 1"}, 40 {11, 1, true, "11"}, 41 {11, 2, true, "11"}, 42 {11, 3, true, " 11"}, 43 {11, 4, true, " 11"}, 44 {111, 1, true, "111"}, 45 {111, 2, true, "111"}, 46 {111, 3, true, "111"}, 47 {111, 4, true, " 111"}, 48 {1111, 1, true, "1111"}, 49 {1111, 2, true, "1111"}, 50 {1111, 3, true, "1111"}, 51 {1111, 4, true, "1111"}, 52 } { 53 xtesting.Equal(t, _padItoa(tc.giveNum, tc.giveDigit, tc.giveSpace), tc.want) 54 } 55 } 56 57 func TestInvalidStrftime(t *testing.T) { 58 d := time.Date(2001, 1, 1, 1, 1, 1, 0, time.Local) 59 for _, tc := range []struct { 60 givePattern string 61 wantError bool 62 wantResult string 63 }{ 64 {"", false, ""}, 65 {"test测试テスТест", false, "test测试テスТест"}, 66 {"%%", false, "%"}, 67 {"-%%", false, "-%"}, 68 {"%%%n%%%%", false, "%\n%%"}, 69 {"%n%%%%%t-%%-", false, "\n%%\t-%-"}, 70 {"%%%", true, ""}, 71 {"%%%-", true, ""}, 72 {"%S", false, "01"}, 73 {"%-S", false, "1"}, 74 {"%Y/%m/%d %H:%M:%S", false, "2001/01/01 01:01:01"}, 75 {"%-y年%-m月%-d日%h %-I时%-M分%-S秒%p", false, "1年1月1日Jan 1时1分1秒AM"}, 76 {"%f", true, ""}, 77 {"%-a", true, ""}, 78 } { 79 t.Run(tc.givePattern, func(t *testing.T) { 80 r, err := StrftimeInString(tc.givePattern, d) 81 xtesting.Equal(t, err != nil, tc.wantError) 82 if err == nil { 83 xtesting.Equal(t, r, tc.wantResult) 84 } 85 }) 86 } 87 } 88 89 func TestStrftime(t *testing.T) { 90 d1 := time.Date(2001, 1, 1, 0, 0, 0, 0, time.UTC) 91 d2 := time.Date(2021, 12, 30, 23, 25, 39, 0, time.FixedZone("UTC+8", 8*60*60)) 92 d3 := time.Date(1901, 6, 9, 12, 1, 10, 0, time.FixedZone("UTC-9", -9*60*60)) 93 for _, tc := range []struct { 94 giveDate time.Time 95 givePattern string 96 wantResult string 97 }{ 98 {d1, "%Y %y %C %-Y %-y %-C", "2001 01 20 2001 1 20"}, 99 {d1, "%m %B %b %h %-m", "01 January Jan Jan 1"}, 100 {d1, "%d %e %A %a %-d", "01 1 Monday Mon 1"}, 101 {d1, "%H %k %I %l %-H %-I", "00 0 12 12 0 12"}, 102 {d1, "%p %P %M %S %-M %-S", "AM am 00 00 0 0"}, 103 {d1, "%Z %z %s %j %-j", "UTC +0000 978307200 001 1"}, 104 {d1, "%w %u %U %W %-U %-W", "1 1 01 01 1 1" /* "1 1 00 01 0 1" */}, 105 {d1, "%G %g %V %-G %-g %-V", "2001 01 01 2001 1 1"}, 106 {d1, "%c, %D, %F, %R, %r, %T, %v, %X, %x", "Mon Jan 1 00:00:00 2001, 01/01/01, 2001-01-01, 00:00, 12:00:00 AM, 00:00:00, 1-Jan-2001, 00:00:00, 01/01/01"}, 107 108 {d2, "%Y %y %C %-Y %-y %-C", "2021 21 20 2021 21 20"}, 109 {d2, "%m %B %b %h %-m", "12 December Dec Dec 12"}, 110 {d2, "%d %e %A %a %-d", "30 30 Thursday Thu 30"}, 111 {d2, "%H %k %I %l %-H %-I", "23 23 11 11 23 11"}, 112 {d2, "%p %P %M %S %-M %-S", "PM pm 25 39 25 39"}, 113 {d2, "%Z %z %s %j %-j", "UTC+8 +0800 1640877939 364 364"}, 114 {d2, "%w %u %U %W %-U %-W", "4 4 52 52 52 52"}, 115 {d2, "%G %g %V %-G %-g %-V", "2021 21 52 2021 21 52"}, 116 {d2, "%c, %D, %F, %R, %r, %T, %v, %X, %x", "Thu Dec 30 23:25:39 2021, 12/30/21, 2021-12-30, 23:25, 11:25:39 PM, 23:25:39, 30-Dec-2021, 23:25:39, 12/30/21"}, 117 118 {d3, "%Y %y %C %-Y %-y %-C", "1901 01 19 1901 1 19"}, 119 {d3, "%m %B %b %h %-m", "06 June Jun Jun 6"}, 120 {d3, "%d %e %A %a %-d", "09 9 Sunday Sun 9"}, 121 {d3, "%H %k %I %l %-H %-I", "12 12 12 12 12 12"}, 122 {d3, "%p %P %M %S %-M %-S", "PM pm 01 10 1 10"}, 123 {d3, "%Z %z %s %j %-j", "UTC-9 -0900 -2163639530 160 160"}, 124 {d3, "%w %u %U %W %-U %-W", "0 7 23 23 23 23" /* "0 7 23 22 23 22" */}, 125 {d3, "%G %g %V %-G %-g %-V", "1901 01 23 1901 1 23"}, 126 {d3, "%c, %D, %F, %R, %r, %T, %v, %X, %x", "Sun Jun 9 12:01:10 1901, 06/09/01, 1901-06-09, 12:01, 12:01:10 PM, 12:01:10, 9-Jun-1901, 12:01:10, 06/09/01"}, 127 } { 128 t.Run(tc.givePattern, func(t *testing.T) { 129 r, err := StrftimeInString(tc.givePattern, tc.giveDate) 130 xtesting.Nil(t, err) 131 xtesting.Equal(t, r, tc.wantResult) 132 }) 133 } 134 } 135 136 func TestStrftimeToGlobPattern(t *testing.T) { 137 for _, tc := range []struct { 138 give string 139 want string 140 }{ 141 {"", ""}, 142 {"test测试テスТест", "test测试テスТест"}, 143 {"%%", "%"}, 144 {"-%%", "-%"}, 145 {"%%%n%%%%", "%*%%"}, 146 {"%n%%%%%t-%%-", "*%%*-%-"}, 147 {"%%%", "%%"}, // <- error 148 {"%%-%-", "%-%-"}, // <- error 149 {"%S", "*"}, 150 {"%A%f%F", "*"}, 151 {"-%S%-S-", "-*-"}, 152 {"%Y/%m/%d %H:%M:%S", "*/*/* *:*:*"}, 153 {"%Y%m%d %H%M%S", "* *"}, 154 } { 155 t.Run(tc.give, func(t *testing.T) { 156 xtesting.Equal(t, StrftimeToGlobPattern(tc.give), tc.want) 157 }) 158 } 159 }