timefmt._auto
Provides a method to automatically convert a datetime or timedelta object to a human-readable string.
Module Contents
Functions
|
Automatically convert a datetime.datetime or datetime.timedelta object to a string and return it. |
- timefmt._auto.auto(time_in: datetime.datetime | datetime.timedelta, long: bool = False)
Automatically convert a datetime.datetime or datetime.timedelta object to a string and return it.
- Parameters:
time_in (datetime.datetime | datetime.timedelta) – The object to convert.
long – If we should return the long or short version.
- Raises:
onecondition.ValidationError – If the time_in is not a datetime.datetime or datetime.timedelta object
- Returns:
The input time in a human-readable format.
- Return type:
str
- Example:
>>> from freezegun import freeze_time >>> freezer = freeze_time("2023-12-31") >>> r = freezer.start()
>>> from zoneinfo import ZoneInfo >>> test_datetime = datetime.datetime(2023, 12, 31, 12, 23, 31, 379292, tzinfo=ZoneInfo("MST")) >>> auto(test_datetime) '12:23:31 PM' >>> auto(test_datetime, long=True) '12:23:31 PM MST' >>> test_timedelta = datetime.timedelta(hours=1000, seconds=9999) >>> auto(test_timedelta) '5W 6D 18:46:39' >>> auto(test_timedelta, long=True) '5 weeks, 6 days, 18 hours, 46 minutes, and 39 seconds' >>> auto(42) Traceback (most recent call last): ... onecondition.ValidationError: Value `42` must must be either a datetime or timedelta object, not a <class 'int'>