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

Method test_strptime

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

Source from the content-addressed store, hash-verified

3010 self.assertLessEqual(abs(from_timestamp - from_now), tolerance)
3011
3012 def test_strptime(self):
3013 string = '2004-12-01 13:02:47.197'
3014 format = '%Y-%m-%d %H:%M:%S.%f'
3015 expected = _strptime._strptime_datetime_datetime(self.theclass, string,
3016 format)
3017 got = self.theclass.strptime(string, format)
3018 self.assertEqual(expected, got)
3019 self.assertIs(type(expected), self.theclass)
3020 self.assertIs(type(got), self.theclass)
3021
3022 # bpo-34482: Check that surrogates are handled properly.
3023 inputs = [
3024 ('2004-12-01\ud80013:02:47.197', '%Y-%m-%d\ud800%H:%M:%S.%f'),
3025 ('2004\ud80012-01 13:02:47.197', '%Y\ud800%m-%d %H:%M:%S.%f'),
3026 ('2004-12-01 13:02\ud80047.197', '%Y-%m-%d %H:%M\ud800%S.%f'),
3027 ]
3028 for string, format in inputs:
3029 with self.subTest(string=string, format=format):
3030 expected = _strptime._strptime_datetime_datetime(self.theclass,
3031 string, format)
3032 got = self.theclass.strptime(string, format)
3033 self.assertEqual(expected, got)
3034
3035 strptime = self.theclass.strptime
3036
3037 self.assertEqual(strptime("+0002", "%z").utcoffset(), 2 * MINUTE)
3038 self.assertEqual(strptime("-0002", "%z").utcoffset(), -2 * MINUTE)
3039 self.assertEqual(
3040 strptime("-00:02:01.000003", "%z").utcoffset(),
3041 -timedelta(minutes=2, seconds=1, microseconds=3)
3042 )
3043 self.assertEqual(strptime("+01:07", "%:z").utcoffset(),
3044 1 * HOUR + 7 * MINUTE)
3045 self.assertEqual(strptime("-10:02", "%:z").utcoffset(),
3046 -(10 * HOUR + 2 * MINUTE))
3047 self.assertEqual(strptime("-00:00:01.00001", "%:z").utcoffset(),
3048 -timedelta(seconds=1, microseconds=10))
3049 # Only local timezone and UTC are supported
3050 for tzseconds, tzname in ((0, 'UTC'), (0, 'GMT'),
3051 (-_time.timezone, _time.tzname[0])):
3052 if tzseconds < 0:
3053 sign = '-'
3054 seconds = -tzseconds
3055 else:
3056 sign ='+'
3057 seconds = tzseconds
3058 hours, minutes = divmod(seconds//60, 60)
3059 dtstr = "{}{:02d}{:02d} {}".format(sign, hours, minutes, tzname)
3060 dt = strptime(dtstr, "%z %Z")
3061 self.assertEqual(dt.utcoffset(), timedelta(seconds=tzseconds))
3062 self.assertEqual(dt.tzname(), tzname)
3063 # Can produce inconsistent datetime
3064 dtstr, fmt = "+1234 UTC", "%z %Z"
3065 dt = strptime(dtstr, fmt)
3066 self.assertEqual(dt.utcoffset(), 12 * HOUR + 34 * MINUTE)
3067 self.assertEqual(dt.tzname(), 'UTC')
3068 # yet will roundtrip
3069 self.assertEqual(dt.strftime(fmt), dtstr)

Callers

nothing calls this directly

Calls 10

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

Tested by

no test coverage detected