11import warnings
2- from datetime import date , datetime
2+ from datetime import date , datetime , time
33from decimal import Decimal
44
55import attr
@@ -297,8 +297,11 @@ class FluentDateType(FluentType):
297297 # So we leave those alone, and implement another `_init_options`
298298 # which is called from other constructors.
299299 def _init_options (self , dt_obj : Union [date , datetime ], kwargs : Dict [str , Any ]) -> None :
300- if 'timeStyle' in kwargs and not isinstance (self , datetime ):
301- raise TypeError ("timeStyle option can only be specified for datetime instances, not date instance" )
300+ if 'timeStyle' in kwargs and not isinstance (self , (datetime , time )):
301+ raise TypeError ("timeStyle option can only be specified for datetime or time instances, not date instance" )
302+
303+ if 'dateStyle' in kwargs and not isinstance (self , (datetime , date )):
304+ raise TypeError ("dateStyle option can only be specified for datetime or time instances, not date instance" )
302305
303306 self .options = merge_options (DateFormatOptions ,
304307 getattr (dt_obj , 'options' , None ),
@@ -308,18 +311,18 @@ def _init_options(self, dt_obj: Union[date, datetime], kwargs: Dict[str, Any]) -
308311 warnings .warn (f"FluentDateType option { k } is not yet supported" )
309312
310313 def format (self , locale : Locale ) -> str :
311- if isinstance (self , datetime ):
314+ if isinstance (self , ( datetime , time ) ):
312315 selftz = _ensure_datetime_tzinfo (self , tzinfo = self .options .timeZone )
313316 else :
314317 selftz = cast (datetime , self )
315318
316319 ds = self .options .dateStyle
317320 ts = self .options .timeStyle
318321 if ds is None :
319- if ts is None :
322+ if ts is None and not isinstance ( selftz , time ) :
320323 return format_date (selftz , format = 'medium' , locale = locale )
321324 else :
322- return format_time (selftz , format = ts , locale = locale )
325+ return format_time (selftz , format = ts or 'short' , locale = locale )
323326 elif ts is None :
324327 return format_date (selftz , format = ds , locale = locale )
325328
@@ -333,7 +336,7 @@ def format(self, locale: Locale) -> str:
333336 .replace ('{1}' , format_date (selftz , ds , locale = locale )))
334337
335338
336- def _ensure_datetime_tzinfo (dt : datetime , tzinfo : Union [str , None ] = None ) -> datetime :
339+ def _ensure_datetime_tzinfo (dt : Union [ datetime , time ], tzinfo : Union [str , None ] = None ) -> Union [ datetime , time ] :
337340 """
338341 Ensure the datetime passed has an attached tzinfo.
339342 """
@@ -353,6 +356,15 @@ def from_date(cls, dt_obj: date, **kwargs: Any) -> 'FluentDate':
353356 return obj
354357
355358
359+ class FluentTime (FluentDateType , time ):
360+ @classmethod
361+ def from_time (cls , dt_obj : time , ** kwargs ) -> 'FluentTime' :
362+ obj = cls (dt_obj .hour , dt_obj .minute , dt_obj .second ,
363+ dt_obj .microsecond , tzinfo = dt_obj .tzinfo )
364+ obj ._init_options (dt_obj , kwargs )
365+ return obj
366+
367+
356368class FluentDateTime (FluentDateType , datetime ):
357369 @classmethod
358370 def from_date_time (cls , dt_obj : datetime , ** kwargs : Any ) -> 'FluentDateTime' :
@@ -371,6 +383,8 @@ def fluent_date(
371383 return dt
372384 if isinstance (dt , datetime ):
373385 return FluentDateTime .from_date_time (dt , ** kwargs )
386+ elif isinstance (dt , time ):
387+ return FluentTime .from_time (dt , ** kwargs )
374388 elif isinstance (dt , date ):
375389 return FluentDate .from_date (dt , ** kwargs )
376390 elif isinstance (dt , FluentNone ):
0 commit comments