@@ -406,37 +406,50 @@ def test_offset(self):
406406 (* _ , offset ), _ , offset_fraction = _strptime ._strptime ("-013030.000001" , "%z" )
407407 self .assertEqual (offset , - (one_hour + half_hour + half_minute ))
408408 self .assertEqual (offset_fraction , - 1 )
409- (* _ , offset ), _ , offset_fraction = _strptime ._strptime ("+01:00" , "%z" )
410- self .assertEqual (offset , one_hour )
411- self .assertEqual (offset_fraction , 0 )
412- (* _ , offset ), _ , offset_fraction = _strptime ._strptime ("-01:30" , "%z" )
413- self .assertEqual (offset , - (one_hour + half_hour ))
414- self .assertEqual (offset_fraction , 0 )
415- (* _ , offset ), _ , offset_fraction = _strptime ._strptime ("-01:30:30" , "%z" )
416- self .assertEqual (offset , - (one_hour + half_hour + half_minute ))
417- self .assertEqual (offset_fraction , 0 )
418- (* _ , offset ), _ , offset_fraction = _strptime ._strptime ("-01:30:30.000001" , "%z" )
419- self .assertEqual (offset , - (one_hour + half_hour + half_minute ))
420- self .assertEqual (offset_fraction , - 1 )
421- (* _ , offset ), _ , offset_fraction = _strptime ._strptime ("+01:30:30.001" , "%z" )
422- self .assertEqual (offset , one_hour + half_hour + half_minute )
423- self .assertEqual (offset_fraction , 1000 )
424- (* _ , offset ), _ , offset_fraction = _strptime ._strptime ("Z" , "%z" )
425- self .assertEqual (offset , 0 )
426- self .assertEqual (offset_fraction , 0 )
409+
410+ cases = [
411+ ("+01:00" , one_hour , 0 ),
412+ ("-01:30" , - (one_hour + half_hour ), 0 ),
413+ ("-01:30:30" , - (one_hour + half_hour + half_minute ), 0 ),
414+ ("-01:30:30.000001" , - (one_hour + half_hour + half_minute ), - 1 ),
415+ ("+01:30:30.001" , + (one_hour + half_hour + half_minute ), 1000 ),
416+ ("Z" , 0 , 0 ),
417+ ]
418+ for directive in ("%z" , "%:z" ):
419+ for offset_str , expected_offset , expected_fraction in cases :
420+ with self .subTest (offset_str = offset_str , directive = directive ):
421+ (* _ , offset ), _ , offset_fraction = _strptime ._strptime (
422+ offset_str , directive
423+ )
424+ self .assertEqual (offset , expected_offset )
425+ self .assertEqual (offset_fraction , expected_fraction )
427426
428427 def test_bad_offset (self ):
429- with self .assertRaises (ValueError ):
430- _strptime ._strptime ("-01:30:30." , "%z" )
431- with self .assertRaises (ValueError ):
432- _strptime ._strptime ("-0130:30" , "%z" )
433- with self .assertRaises (ValueError ):
434- _strptime ._strptime ("-01:30:30.1234567" , "%z" )
435- with self .assertRaises (ValueError ):
436- _strptime ._strptime ("-01:30:30:123456" , "%z" )
428+ error_cases_any_z = [
429+ "-01:30:30." , # Decimal point not followed with digits
430+ "-01:30:30.1234567" , # Too many digits after decimal point
431+ "-01:30:30:123456" , # Colon as decimal separator
432+ "-0130:30" , # Incorrect use of colons
433+ ]
434+ for directive in ("%z" , "%:z" ):
435+ for timestr in error_cases_any_z :
436+ with self .subTest (timestr = timestr , directive = directive ):
437+ with self .assertRaises (ValueError ):
438+ _strptime ._strptime (timestr , directive )
439+
440+ required_colons_cases = ["-013030" , "+0130" , "-01:3030.123456" ]
441+ for timestr in required_colons_cases :
442+ with self .subTest (timestr = timestr ):
443+ with self .assertRaises (ValueError ):
444+ _strptime ._strptime (timestr , "%:z" )
445+
437446 with self .assertRaises (ValueError ) as err :
438447 _strptime ._strptime ("-01:3030" , "%z" )
439448 self .assertEqual ("Inconsistent use of : in -01:3030" , str (err .exception ))
449+ with self .assertRaises (ValueError ) as err :
450+ _strptime ._strptime ("-01:3030" , "%:z" )
451+ self .assertEqual ("Missing colon in %:z before '30', got '-01:3030'" ,
452+ str (err .exception ))
440453
441454 @skip_if_buggy_ucrt_strfptime
442455 def test_timezone (self ):
0 commit comments