MCPcopy Index your code
hub / github.com/python/cpython / test_strptime_tz

Method test_strptime_tz

Lib/test/datetimetester.py:4204–4244  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

4202 self.assertIs(type(got), self.theclass)
4203
4204 def test_strptime_tz(self):
4205 strptime = self.theclass.strptime
4206 self.assertEqual(strptime("+0002", "%z").utcoffset(), 2 * MINUTE)
4207 self.assertEqual(strptime("-0002", "%z").utcoffset(), -2 * MINUTE)
4208 self.assertEqual(
4209 strptime("-00:02:01.000003", "%z").utcoffset(),
4210 -timedelta(minutes=2, seconds=1, microseconds=3)
4211 )
4212 self.assertEqual(strptime("+01:07", "%:z").utcoffset(),
4213 1 * HOUR + 7 * MINUTE)
4214 self.assertEqual(strptime("-10:02", "%:z").utcoffset(),
4215 -(10 * HOUR + 2 * MINUTE))
4216 self.assertEqual(strptime("-00:00:01.00001", "%:z").utcoffset(),
4217 -timedelta(seconds=1, microseconds=10))
4218 # Only local timezone and UTC are supported
4219 for tzseconds, tzname in ((0, 'UTC'), (0, 'GMT'),
4220 (-_time.timezone, _time.tzname[0])):
4221 if tzseconds < 0:
4222 sign = '-'
4223 seconds = -tzseconds
4224 else:
4225 sign ='+'
4226 seconds = tzseconds
4227 hours, minutes = divmod(seconds//60, 60)
4228 tstr = "{}{:02d}{:02d} {}".format(sign, hours, minutes, tzname)
4229 with self.subTest(tstr=tstr):
4230 t = strptime(tstr, "%z %Z")
4231 self.assertEqual(t.utcoffset(), timedelta(seconds=tzseconds))
4232 self.assertEqual(t.tzname(), tzname)
4233 self.assertIs(type(t), self.theclass)
4234
4235 # Can produce inconsistent time
4236 tstr, fmt = "+1234 UTC", "%z %Z"
4237 t = strptime(tstr, fmt)
4238 self.assertEqual(t.utcoffset(), 12 * HOUR + 34 * MINUTE)
4239 self.assertEqual(t.tzname(), 'UTC')
4240 # yet will roundtrip
4241 self.assertEqual(t.strftime(fmt), tstr)
4242
4243 # Produce naive time if no %z is provided
4244 self.assertEqual(strptime("UTC", "%Z").tzinfo, None)
4245
4246 def test_strptime_errors(self):
4247 for tzstr in ("-2400", "-000", "z", "24:00"):

Callers

nothing calls this directly

Calls 8

timedeltaClass · 0.90
assertEqualMethod · 0.45
utcoffsetMethod · 0.45
formatMethod · 0.45
subTestMethod · 0.45
tznameMethod · 0.45
assertIsMethod · 0.45
strftimeMethod · 0.45

Tested by

no test coverage detected