@@ -1461,6 +1461,7 @@ def test_iso_long_years(self):
14611461 def test_isoformat (self ):
14621462 t = self .theclass (2 , 3 , 2 )
14631463 self .assertEqual (t .isoformat (), "0002-03-02" )
1464+ self .assertEqual (t .isoformat (basic = True ), "00020302" )
14641465
14651466 def test_ctime (self ):
14661467 t = self .theclass (2002 , 3 , 2 )
@@ -2081,16 +2082,36 @@ def test_isoformat(self):
20812082 self .assertEqual (t .isoformat ('T' ), "0001-02-03T04:05:01.000123" )
20822083 self .assertEqual (t .isoformat (' ' ), "0001-02-03 04:05:01.000123" )
20832084 self .assertEqual (t .isoformat ('\x00 ' ), "0001-02-03\x00 04:05:01.000123" )
2085+
2086+ self .assertEqual (t .isoformat (basic = True ), "00010203T040501.000123" )
2087+ self .assertEqual (t .isoformat ('T' , basic = True ), "00010203T040501.000123" )
2088+ self .assertEqual (t .isoformat (' ' , basic = True ), "00010203 040501.000123" )
2089+ self .assertEqual (t .isoformat ('\x00 ' , basic = True ), "00010203\x00 040501.000123" )
2090+
20842091 # bpo-34482: Check that surrogates are handled properly.
20852092 self .assertEqual (t .isoformat ('\ud800 ' ),
20862093 "0001-02-03\ud800 04:05:01.000123" )
20872094 self .assertEqual (t .isoformat (timespec = 'hours' ), "0001-02-03T04" )
2095+ self .assertEqual (t .isoformat (timespec = 'hours' , basic = True ), "00010203T04" )
2096+
20882097 self .assertEqual (t .isoformat (timespec = 'minutes' ), "0001-02-03T04:05" )
2098+ self .assertEqual (t .isoformat (timespec = 'minutes' , basic = True ), "00010203T0405" )
2099+
20892100 self .assertEqual (t .isoformat (timespec = 'seconds' ), "0001-02-03T04:05:01" )
2101+ self .assertEqual (t .isoformat (timespec = 'seconds' , basic = True ), "00010203T040501" )
2102+
20902103 self .assertEqual (t .isoformat (timespec = 'milliseconds' ), "0001-02-03T04:05:01.000" )
2104+ self .assertEqual (t .isoformat (timespec = 'milliseconds' , basic = True ), "00010203T040501.000" )
2105+
20912106 self .assertEqual (t .isoformat (timespec = 'microseconds' ), "0001-02-03T04:05:01.000123" )
2107+ self .assertEqual (t .isoformat (timespec = 'microseconds' , basic = True ), "00010203T040501.000123" )
2108+
20922109 self .assertEqual (t .isoformat (timespec = 'auto' ), "0001-02-03T04:05:01.000123" )
2110+ self .assertEqual (t .isoformat (timespec = 'auto' , basic = True ), "00010203T040501.000123" )
2111+
20932112 self .assertEqual (t .isoformat (sep = ' ' , timespec = 'minutes' ), "0001-02-03 04:05" )
2113+ self .assertEqual (t .isoformat (sep = ' ' , timespec = 'minutes' , basic = True ), "00010203 0405" )
2114+
20942115 self .assertRaises (ValueError , t .isoformat , timespec = 'foo' )
20952116 # bpo-34482: Check that surrogates are handled properly.
20962117 self .assertRaises (ValueError , t .isoformat , timespec = '\ud800 ' )
@@ -2099,55 +2120,71 @@ def test_isoformat(self):
20992120
21002121 t = self .theclass (1 , 2 , 3 , 4 , 5 , 1 , 999500 , tzinfo = timezone .utc )
21012122 self .assertEqual (t .isoformat (timespec = 'milliseconds' ), "0001-02-03T04:05:01.999+00:00" )
2123+ self .assertEqual (t .isoformat (timespec = 'milliseconds' , basic = True ), "00010203T040501.999+0000" )
21022124
21032125 t = self .theclass (1 , 2 , 3 , 4 , 5 , 1 , 999500 )
21042126 self .assertEqual (t .isoformat (timespec = 'milliseconds' ), "0001-02-03T04:05:01.999" )
2127+ self .assertEqual (t .isoformat (timespec = 'milliseconds' , basic = True ), "00010203T040501.999" )
21052128
21062129 t = self .theclass (1 , 2 , 3 , 4 , 5 , 1 )
21072130 self .assertEqual (t .isoformat (timespec = 'auto' ), "0001-02-03T04:05:01" )
2131+ self .assertEqual (t .isoformat (timespec = 'auto' , basic = True ), "00010203T040501" )
21082132 self .assertEqual (t .isoformat (timespec = 'milliseconds' ), "0001-02-03T04:05:01.000" )
2133+ self .assertEqual (t .isoformat (timespec = 'milliseconds' , basic = True ), "00010203T040501.000" )
21092134 self .assertEqual (t .isoformat (timespec = 'microseconds' ), "0001-02-03T04:05:01.000000" )
2135+ self .assertEqual (t .isoformat (timespec = 'microseconds' , basic = True ), "00010203T040501.000000" )
21102136
21112137 t = self .theclass (2 , 3 , 2 )
21122138 self .assertEqual (t .isoformat (), "0002-03-02T00:00:00" )
2139+ self .assertEqual (t .isoformat (basic = True ), "00020302T000000" )
21132140 self .assertEqual (t .isoformat ('T' ), "0002-03-02T00:00:00" )
2141+ self .assertEqual (t .isoformat ('T' , basic = True ), "00020302T000000" )
21142142 self .assertEqual (t .isoformat (' ' ), "0002-03-02 00:00:00" )
2143+ self .assertEqual (t .isoformat (' ' , basic = True ), "00020302 000000" )
21152144 # str is ISO format with the separator forced to a blank.
21162145 self .assertEqual (str (t ), "0002-03-02 00:00:00" )
21172146 # ISO format with timezone
21182147 tz = FixedOffset (timedelta (seconds = 16 ), 'XXX' )
21192148 t = self .theclass (2 , 3 , 2 , tzinfo = tz )
21202149 self .assertEqual (t .isoformat (), "0002-03-02T00:00:00+00:00:16" )
2150+ self .assertEqual (t .isoformat (basic = True ), "00020302T000000+000016" )
21212151
21222152 def test_isoformat_timezone (self ):
21232153 tzoffsets = [
2124- ('05:00' , timedelta (hours = 5 )),
2125- ('02:00' , timedelta (hours = 2 )),
2126- ('06:27' , timedelta (hours = 6 , minutes = 27 )),
2127- ('12:32:30' , timedelta (hours = 12 , minutes = 32 , seconds = 30 )),
2128- ('02:04:09.123456' , timedelta (hours = 2 , minutes = 4 , seconds = 9 , microseconds = 123456 ))
2154+ (('05:00' , '0500' ), timedelta (hours = 5 )),
2155+ (('02:00' , '0200' ), timedelta (hours = 2 )),
2156+ (('06:27' , '0627' ), timedelta (hours = 6 , minutes = 27 )),
2157+ (('12:32:30' , '123230' ), timedelta (hours = 12 , minutes = 32 , seconds = 30 )),
2158+ (('02:04:09.123456' , '020409.123456' ),
2159+ timedelta (hours = 2 , minutes = 4 , seconds = 9 , microseconds = 123456 ))
21292160 ]
21302161
21312162 tzinfos = [
2132- ('' , None ),
2133- ('+00:00' , timezone .utc ),
2134- ('+00:00' , timezone (timedelta (0 ))),
2163+ (( '' , '' ) , None ),
2164+ (( '+00:00' , '+0000' ) , timezone .utc ),
2165+ (( '+00:00' , '+0000' ) , timezone (timedelta (0 ))),
21352166 ]
21362167
21372168 tzinfos += [
2138- (prefix + expected , timezone (sign * td ))
2139- for expected , td in tzoffsets
2169+ (( prefix + expected_extended , prefix + expected_basic ) , timezone (sign * td ))
2170+ for ( expected_extended , expected_basic ) , td in tzoffsets
21402171 for prefix , sign in [('-' , - 1 ), ('+' , 1 )]
21412172 ]
21422173
21432174 dt_base = self .theclass (2016 , 4 , 1 , 12 , 37 , 9 )
2144- exp_base = '2016-04-01T12:37:09'
2175+ exp_base_ext = '2016-04-01T12:37:09'
2176+ exp_base_basic = '20160401T123709'
21452177
2146- for exp_tz , tzi in tzinfos :
2178+ for ( exp_tz_ext , exp_tz_basic ) , tzi in tzinfos :
21472179 dt = dt_base .replace (tzinfo = tzi )
2148- exp = exp_base + exp_tz
2149- with self . subTest ( tzi = tzi ):
2180+ with self . subTest ( tzi = tzi , basic = False ):
2181+ exp = exp_base_ext + exp_tz_ext
21502182 assert dt .isoformat () == exp
2183+ assert dt .isoformat (basic = False ) == exp
2184+
2185+ with self .subTest (tzi = tzi , basic = True ):
2186+ exp = exp_base_basic + exp_tz_basic
2187+ assert dt .isoformat (basic = True ) == exp
21512188
21522189 def test_format (self ):
21532190 dt = self .theclass (2007 , 9 , 10 , 4 , 5 , 1 , 123 )
@@ -4048,10 +4085,15 @@ def test_zones(self):
40484085 self .assertEqual (str (t5 ), "00:00:00.000040+00:00" )
40494086
40504087 self .assertEqual (t1 .isoformat (), "07:47:00-05:00" )
4088+ self .assertEqual (t1 .isoformat (basic = True ), "074700-0500" )
40514089 self .assertEqual (t2 .isoformat (), "12:47:00+00:00" )
4090+ self .assertEqual (t2 .isoformat (basic = True ), "124700+0000" )
40524091 self .assertEqual (t3 .isoformat (), "13:47:00+01:00" )
4092+ self .assertEqual (t3 .isoformat (basic = True ), "134700+0100" )
40534093 self .assertEqual (t4 .isoformat (), "00:00:00.000040" )
4094+ self .assertEqual (t4 .isoformat (basic = True ), "000000.000040" )
40544095 self .assertEqual (t5 .isoformat (), "00:00:00.000040+00:00" )
4096+ self .assertEqual (t5 .isoformat (basic = True ), "000000.000040+0000" )
40554097
40564098 d = 'datetime.time'
40574099 self .assertEqual (repr (t1 ), d + "(7, 47, tzinfo=est)" )
@@ -4957,25 +4999,71 @@ def utcoffset(self, dt):
49574999 self .assertRaises (OverflowError , huge .utctimetuple )
49585000
49595001 def test_tzinfo_isoformat (self ):
5002+ offsets = [
5003+ (("+00:00" , "+0000" ), 0 ),
5004+ (("+03:40" , "+0340" ), 220 ),
5005+ (("-03:51" , "-0351" ), - 231 ),
5006+ (("" , "" ), None ),
5007+ ]
5008+
49605009 zero = FixedOffset (0 , "+00:00" )
49615010 plus = FixedOffset (220 , "+03:40" )
49625011 minus = FixedOffset (- 231 , "-03:51" )
49635012 unknown = FixedOffset (None , "" )
49645013
49655014 cls = self .theclass
4966- datestr = '0001-02-03'
5015+ datestr_ext = '0001-02-03'
5016+ datestr_basic = '00010203'
5017+ for (name_ext , name_basic ), value in offsets :
5018+ for us in 0 , 987001 :
5019+ timestr_suffix = (us and '.987001' or '' )
5020+
5021+ offset_ext = FixedOffset (value , name_ext )
5022+ d = cls (1 , 2 , 3 , 4 , 5 , 59 , us , tzinfo = offset_ext )
5023+ ofsstr_ext = offset_ext is not None and d .tzname () or ''
5024+ timestr_ext = '04:05:59' + timestr_suffix
5025+ tailstr_ext = timestr_ext + ofsstr_ext
5026+
5027+ iso = d .isoformat ()
5028+ self .assertEqual (iso , d .isoformat (basic = False ))
5029+ self .assertEqual (iso , datestr_ext + 'T' + tailstr_ext )
5030+ self .assertEqual (iso , d .isoformat ('T' ))
5031+ self .assertEqual (d .isoformat ('k' ), datestr_ext + 'k' + tailstr_ext )
5032+ self .assertEqual (d .isoformat ('\u1234 ' ), datestr_ext + '\u1234 ' + tailstr_ext )
5033+ self .assertEqual (str (d ), datestr_ext + ' ' + tailstr_ext )
5034+
5035+ offset_basic = FixedOffset (value , name_basic )
5036+ d = cls (1 , 2 , 3 , 4 , 5 , 59 , us , tzinfo = offset_basic )
5037+ ofsstr_basic = offset_basic is not None and d .tzname () or ''
5038+ timestr_basic = '040559' + timestr_suffix
5039+ tailstr_basic = timestr_basic + ofsstr_basic
5040+
5041+ iso = d .isoformat (basic = True )
5042+ self .assertEqual (iso , datestr_basic + 'T' + tailstr_basic )
5043+ self .assertEqual (iso , d .isoformat ('T' , basic = True ))
5044+ self .assertEqual (d .isoformat ('k' , basic = True ), datestr_basic + 'k' + tailstr_basic )
5045+ self .assertEqual (d .isoformat ('\u1234 ' , basic = True ), datestr_basic + '\u1234 ' + tailstr_basic )
5046+
5047+ def test_tzinfo_isoformat_basic (self ):
5048+ zero = FixedOffset (0 , "+0000" )
5049+ plus = FixedOffset (220 , "+0340" )
5050+ minus = FixedOffset (- 231 , "-0351" )
5051+ unknown = FixedOffset (None , "" )
5052+
5053+ cls = self .theclass
5054+ datestr = '00010203'
49675055 for ofs in None , zero , plus , minus , unknown :
49685056 for us in 0 , 987001 :
49695057 d = cls (1 , 2 , 3 , 4 , 5 , 59 , us , tzinfo = ofs )
4970- timestr = '04:05:59' + (us and '.987001' or '' )
5058+ timestr_suffix = us and '.987001' or ''
5059+ timestr = '040559' + timestr_suffix
49715060 ofsstr = ofs is not None and d .tzname () or ''
49725061 tailstr = timestr + ofsstr
4973- iso = d .isoformat ()
5062+ iso = d .isoformat (basic = True )
49745063 self .assertEqual (iso , datestr + 'T' + tailstr )
4975- self .assertEqual (iso , d .isoformat ('T' ))
4976- self .assertEqual (d .isoformat ('k' ), datestr + 'k' + tailstr )
4977- self .assertEqual (d .isoformat ('\u1234 ' ), datestr + '\u1234 ' + tailstr )
4978- self .assertEqual (str (d ), datestr + ' ' + tailstr )
5064+ self .assertEqual (iso , d .isoformat ('T' , basic = True ))
5065+ self .assertEqual (d .isoformat ('k' , basic = True ), datestr + 'k' + tailstr )
5066+ self .assertEqual (d .isoformat ('\u1234 ' , basic = True ), datestr + '\u1234 ' + tailstr )
49795067
49805068 def test_replace (self ):
49815069 cls = self .theclass
0 commit comments