Skip to content

Commit 1b09699

Browse files
BordaSkafteNicki
andauthored
doctest
Co-authored-by: Nicki Skafte Detlefsen <[email protected]>
1 parent 4693672 commit 1b09699

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/lightning/pytorch/trainer/setup.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,39 @@ def _log_device_info(trainer: "pl.Trainer") -> None:
199199

200200

201201
def _parse_time_interval_seconds(value: Union[str, timedelta, dict]) -> float:
202+
"""Convert a time interval into seconds.
203+
204+
This helper parses different representations of a time interval and
205+
normalizes them into a float number of seconds.
206+
207+
Supported input formats:
208+
* `timedelta`: The total seconds are returned directly.
209+
* `dict`: A dictionary of keyword arguments accepted by
210+
`datetime.timedelta`, e.g. `{"days": 1, "hours": 2}`.
211+
* `str`: A string in the format `"DD:HH:MM:SS"`, where each
212+
component must be an integer.
213+
214+
Args:
215+
value (Union[str, timedelta, dict]): The time interval to parse.
216+
217+
Returns:
218+
float: The duration represented by `value` in seconds.
219+
220+
Raises:
221+
MisconfigurationException: If the input type is unsupported, the
222+
string format is invalid, or any string component is not an integer.
223+
224+
Examples:
225+
>>> _parse_time_interval_seconds("01:02:03:04")
226+
93784.0
227+
228+
>>> _parse_time_interval_seconds({"hours": 2, "minutes": 30})
229+
9000.0
230+
231+
>>> from datetime import timedelta
232+
>>> _parse_time_interval_seconds(timedelta(days=1, seconds=30))
233+
86430.0
234+
"""
202235
if isinstance(value, timedelta):
203236
return value.total_seconds()
204237
if isinstance(value, dict):

0 commit comments

Comments
 (0)