@@ -371,7 +371,9 @@ def format(self, locale: Locale) -> str:
371371 return format_date (selftz , format = "medium" , locale = locale )
372372 else :
373373 return format_time (selftz , format = ts or "short" , locale = locale )
374- elif ts is None :
374+ assert not isinstance (selftz , time )
375+
376+ if ts is None :
375377 return format_date (selftz , format = ds , locale = locale )
376378
377379 # Both date and time. Logic copied from babel.dates.format_datetime,
@@ -388,13 +390,24 @@ def format(self, locale: Locale) -> str:
388390
389391def _ensure_datetime_tzinfo (dt : Union [datetime , time ], tzinfo : Union [str , None ] = None ) -> Union [datetime , time ]:
390392 """
391- Ensure the datetime passed has an attached tzinfo.
393+ Ensure the datetime or time passed has an attached tzinfo.
392394 """
393- # Adapted from babel's function.
395+ if isinstance (dt , datetime ):
396+ # Adapted from babel's function.
397+ if dt .tzinfo is None :
398+ dt = dt .replace (tzinfo = pytz .UTC )
399+ if tzinfo is not None :
400+ dt = dt .astimezone (get_timezone (tzinfo ))
401+ return dt
394402 if dt .tzinfo is None :
395- dt = dt .replace (tzinfo = pytz .UTC )
396- if tzinfo is not None :
397- dt = dt .astimezone (get_timezone (tzinfo ))
403+ tz = get_timezone (tzinfo ) if tzinfo is not None else pytz .UTC
404+ return dt .replace (tzinfo = tz )
405+ elif tzinfo is not None :
406+ tz = get_timezone (tzinfo )
407+ if tz != dt .tzinfo :
408+ print (dt .tzinfo , tz )
409+ raise TypeError ("timezone conversion not supported for time values" )
410+
398411 return dt
399412
400413
0 commit comments