|
38 | 38 | # SOFTWARE.
|
39 | 39 |
|
40 | 40 | import time
|
| 41 | +import calendar |
41 | 42 | import unittest
|
42 | 43 |
|
43 | 44 | def test_sleep():
|
@@ -70,6 +71,141 @@ def test_new_struct_time(self):
|
70 | 71 | self.assertRaises(TypeError, time.struct_time, (2018, 11, 26, 17, 34, 12, 0, 340))
|
71 | 72 | self.assertRaises(TypeError, time.struct_time, (2018, 11, 26, 17, 34, 12, 0, 340, 9, 10, 11, 12))
|
72 | 73 |
|
| 74 | +class StrftimeTests(unittest.TestCase): |
| 75 | + |
| 76 | + def check_format(self, format, date, expectedStr): |
| 77 | + st = time.struct_time(date); |
| 78 | + self.assertEqual(expectedStr, time.strftime(format, st)) |
| 79 | + |
| 80 | + def check_weekDay(self, format, days): |
| 81 | + for d in range (0,21): |
| 82 | + self.check_format(format, (2018, 11, 28, 15, 24, 30, d, 1, 0), days[d % 7]) |
| 83 | + |
| 84 | + self.check_format(format, (2018, 11, 28, 15, 24, 30, -1, 1, 0), days[6]) |
| 85 | + self.check_format(format, (2018, 11, 28, 15, 24, 30, 356, 1, 0), days[6]) |
| 86 | + self.check_format(format, (2018, 11, 28, 15, 24, 30, 9000, 1, 0), days[5]) |
| 87 | + |
| 88 | + self.assertRaises(ValueError, time.strftime, format, time.struct_time((2018, 10, 28, 15, 24, 30, -2, 1, 0))) |
| 89 | + |
| 90 | + def check_month(self, format, months): |
| 91 | + self.check_format(format, (2018, 0, 28, 15, 24, 30, 1, 1, 0), months[1]) |
| 92 | + #for m in range (1,12): |
| 93 | + # self.check_format(format, (2018, m, 28, 15, 24, 30, 1, 1, 0), months[m-1]) |
| 94 | + |
| 95 | + self.assertRaises(ValueError, time.strftime, format, time.struct_time((2018, -1, 28, 15, 24, 30, 1, 1, 0))) |
| 96 | + self.assertRaises(ValueError, time.strftime, format, time.struct_time((2018, 13, 28, 15, 24, 30, 1, 1, 0))) |
| 97 | + |
| 98 | + def test_weekOfDayShort(self): |
| 99 | + self.check_weekDay("%a", calendar.day_abbr) |
| 100 | + |
| 101 | + def test_weekOfDay(self): |
| 102 | + self.check_weekDay("%A", calendar.day_name) |
73 | 103 |
|
| 104 | + def test_monthShortName(self): |
| 105 | + self.check_month("%b", calendar.month_abbr) |
| 106 | + |
| 107 | + def test_monthLongName(self): |
| 108 | + self.check_month("%B", calendar.month_name) |
| 109 | + |
| 110 | + def test_asctime(self): |
| 111 | + # 'Thu Aug 8 05:24:10 2018' in en locale |
| 112 | + self.check_format("%c", (2018, 8, 8, 5, 24, 10, 3, 1, 0), calendar.day_abbr[3] + ' ' + calendar.month_abbr[8] + ' 8 05:24:10 2018') |
| 113 | + |
| 114 | + def test_day(self): |
| 115 | + self.check_format("%d", (2018, 8, 8, 5, 24, 10, 3, 1, 0), '08') |
| 116 | + self.check_format("%d", (2018, 8, 18, 5, 24, 10, 3, 1, 0), '18') |
| 117 | + self.check_format("%d", (2018, 8, 0, 5, 24, 10, 3, 1, 0), '01') |
| 118 | + self.check_format("%d", (2018, 2, 31, 5, 24, 10, 3, 1, 0), '31') |
| 119 | + self.assertRaises(ValueError, time.strftime, "%d", time.struct_time((2018, 8, -1, 15, 24, 30, 1, 1, 0))) |
| 120 | + self.assertRaises(ValueError, time.strftime, "%d", time.struct_time((2018, 8, 32, 15, 24, 30, 1, 1, 0))) |
74 | 121 |
|
| 122 | + def test_hour24(self): |
| 123 | + self.check_format("%H", (2018, 8, 8, 0, 24, 0, 3, 1, 0), '00') |
| 124 | + self.check_format("%H", (2018, 8, 18, 1, 24, 1, 3, 1, 0), '01') |
| 125 | + self.check_format("%H", (2018, 8, 8, 22, 24, 10, 3, 1, 0), '22') |
| 126 | + self.check_format("%H", (2018, 2, 31, 23, 24, 10, 3, 1, 0), '23') |
| 127 | + self.assertRaises(ValueError, time.strftime, "%H", time.struct_time((2018, 8, 2, -1, 24, 30, 1, 1, 0))) |
| 128 | + self.assertRaises(ValueError, time.strftime, "%H", time.struct_time((2018, 8, 2, 24, 24, 30, 1, 1, 0))) |
| 129 | + |
| 130 | + def test_hour12(self): |
| 131 | + self.check_format("%I", (2018, 8, 8, 0, 24, 0, 3, 1, 0), '12') |
| 132 | + self.check_format("%I", (2018, 8, 18, 1, 24, 1, 3, 1, 0), '01') |
| 133 | + self.check_format("%I", (2018, 8, 8, 12, 24, 0, 3, 1, 0), '12') |
| 134 | + self.check_format("%I", (2018, 8, 8, 22, 24, 10, 3, 1, 0), '10') |
| 135 | + self.check_format("%I", (2018, 2, 31, 23, 24, 10, 3, 1, 0), '11') |
| 136 | + self.assertRaises(ValueError, time.strftime, "%I", time.struct_time((2018, 8, 2, -1, 24, 30, 1, 1, 0))) |
| 137 | + self.assertRaises(ValueError, time.strftime, "%I", time.struct_time((2018, 8, 2, 24, 24, 30, 1, 1, 0))) |
| 138 | + |
| 139 | + def test_dayOfYear(self): |
| 140 | + self.check_format("%j", (2018, 8, 8, 0, 24, 0, 7, 0, 0), '001') |
| 141 | + self.check_format("%j", (2018, 8, 8, 0, 24, 0, 7, 1, 0), '001') |
| 142 | + self.check_format("%j", (2018, 8, 8, 0, 24, 0, 7, 10, 0), '010') |
| 143 | + self.check_format("%j", (2018, 8, 8, 0, 24, 0, 7, 365, 0), '365') |
| 144 | + self.check_format("%j", (2018, 8, 8, 0, 24, 0, 7, 366, 0), '366') |
| 145 | + self.assertRaises(ValueError, time.strftime, "%j", time.struct_time((2018, 8, 2, 24, 24, 30, 1, -1, 0))) |
| 146 | + self.assertRaises(ValueError, time.strftime, "%j", time.struct_time((2018, 8, 2, 24, 24, 30, 1, 367, 0))) |
| 147 | + |
| 148 | + def test_month(self): |
| 149 | + self.check_format("%m", (2018, 0, 8, 0, 24, 0, 3, 1, 0), '01') |
| 150 | + self.check_format("%m", (2018, 1, 8, 0, 24, 0, 3, 1, 0), '01') |
| 151 | + self.check_format("%m", (2018, 8, 18, 1, 24, 1, 3, 1, 0), '08') |
| 152 | + self.check_format("%m", (2018, 12, 8, 10, 24, 0, 3, 1, 0), '12') |
| 153 | + self.assertRaises(ValueError, time.strftime, "%m", time.struct_time((2018, -1, 2, 2, 24, 30, 1, 1, 0))) |
| 154 | + self.assertRaises(ValueError, time.strftime, "%m", time.struct_time((2018, 13, 2, 2, 24, 30, 1, 1, 0))) |
| 155 | + |
| 156 | + def test_minute(self): |
| 157 | + self.check_format("%M", (2018, 0, 8, 0, 0, 0, 3, 1, 0), '00') |
| 158 | + self.check_format("%M", (2018, 1, 8, 0, 8, 0, 3, 1, 0), '08') |
| 159 | + self.check_format("%M", (2018, 8, 18, 1, 50, 1, 3, 1, 0), '50') |
| 160 | + self.check_format("%M", (2018, 12, 8, 10, 59, 0, 3, 1, 0), '59') |
| 161 | + self.assertRaises(ValueError, time.strftime, "%M", time.struct_time((2018, 11, 2, 2, -1, 30, 1, 1, 0))) |
| 162 | + self.assertRaises(ValueError, time.strftime, "%M", time.struct_time((2018, 11, 2, 2, 60, 30, 1, 1, 0))) |
| 163 | + |
| 164 | + def test_ampm(self): |
| 165 | + pm_am = time.strftime("%p"); |
| 166 | + if pm_am == 'AM' or pm_am == 'am' or pm_am == 'PM' or pm_am == 'pm': |
| 167 | + # the test has sence only if the pm/am is provided |
| 168 | + self.check_format("%p", (2018, 2, 18, 0, 0, 0, 3, 1, 0), 'AM') |
| 169 | + self.check_format("%p", (2018, 8, 18, 11, 8, 0, 3, 1, 0), 'AM') |
| 170 | + self.check_format("%p", (2018, 8, 18, 12, 50, 1, 3, 1, 0), 'PM') |
| 171 | + self.check_format("%p", (2018, 8, 18, 23, 59, 0, 3, 1, 0), 'PM') |
| 172 | + self.assertRaises(ValueError, time.strftime, "%p", time.struct_time((2018, 8, 2, -1, 24, 30, 1, 1, 0))) |
| 173 | + self.assertRaises(ValueError, time.strftime, "%p", time.struct_time((2018, 8, 2, 24, 24, 30, 1, 1, 0))) |
| 174 | + |
| 175 | + def test_sec(self): |
| 176 | + self.check_format("%S", (2018, 2, 18, 10, 10, 0, 3, 1, 0), '00') |
| 177 | + self.check_format("%S", (2018, 8, 18, 11, 12, 2, 3, 1, 0), '02') |
| 178 | + self.check_format("%S", (2018, 8, 18, 12, 20, 60, 3, 1, 0), '60') |
| 179 | + self.check_format("%S", (2018, 8, 18, 23, 20, 61, 3, 1, 0), '61') |
| 180 | + self.assertRaises(ValueError, time.strftime, "%S", time.struct_time((2018, 8, 2, 10, -1, 30, 1, 1, 0))) |
| 181 | + self.assertRaises(ValueError, time.strftime, "%S", time.struct_time((2018, 8, 2, 24, 62, 30, 1, 1, 0))) |
| 182 | + |
| 183 | + def test_weekDay(self): |
| 184 | + self.check_format("%w", (2018, 11, 28, 10, 0, 0, -1, 1, 0), '0') |
| 185 | + self.check_format("%w", (2018, 11, 28, 10, 0, 0, 0, 1, 0), '1') |
| 186 | + self.check_format("%w", (2018, 11, 25, 11, 12, 2, 3, 1, 0), '4') |
| 187 | + self.check_format("%w", (2018, 11, 24, 23, 20, 61, 6, 1, 0), '0') |
| 188 | + self.check_format("%w", (2018, 11, 24, 23, 20, 61, 7, 1, 0), '1') |
| 189 | + self.check_format("%w", (2018, 11, 24, 23, 20, 61, 999, 1, 0), '6') |
| 190 | + self.assertRaises(ValueError, time.strftime, "%w", time.struct_time((2018, 8, 2, 10, 20, 30, -2, 1, 0))) |
75 | 191 |
|
| 192 | + def test_YearY(self): |
| 193 | + self.check_format("%Y", (2018, 11, 28, 10, 0, 0, -1, 1, 0), '2018') |
| 194 | + self.check_format("%Y", (18, 11, 28, 10, 0, 0, 0, 1, 0), '18') |
| 195 | + self.check_format("%Y", (0, 11, 25, 11, 12, 2, 3, 1, 0), '0') |
| 196 | + self.check_format("%Y", (-365, 11, 24, 23, 20, 61, 6, 1, 0), '-365') |
| 197 | + self.check_format("%Y", (17829, 11, 24, 23, 20, 61, 7, 1, 0), '17829') |
| 198 | + |
| 199 | + def test_Yeary(self): |
| 200 | + self.check_format("%y", (2018, 11, 28, 10, 0, 0, -1, 1, 0), '18') |
| 201 | + self.check_format("%y", (18, 11, 28, 10, 0, 0, 0, 1, 0), '18') |
| 202 | + self.check_format("%y", (0, 11, 25, 11, 12, 2, 3, 1, 0), '00') |
| 203 | + # This is failing on CPython, which return '35' |
| 204 | + #self.check_format("%y", (-365, 11, 24, 23, 20, 61, 6, 1, 0), '65') |
| 205 | + self.check_format("%y", (17829, 11, 24, 23, 20, 61, 7, 1, 0), '29') |
| 206 | + |
| 207 | + def test_wrongInput(self): |
| 208 | + self.assertRaises(TypeError, time.strftime, 10, (2018, 8, 2, 10, 20, 30, -2, 1, 0)) |
| 209 | + self.assertRaises(TypeError, time.strftime, "%w", 10) |
| 210 | + self.assertRaises(TypeError, time.strftime, "%w", (2018, 11, 29)) |
| 211 | + |
0 commit comments