-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add utilities to deal with time and duration (#103)
Signed-off-by: Michel Hidalgo <mhidalgo@theaiinstitute.com>
- Loading branch information
1 parent
2224e6d
commit dbb1bab
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Copyright (c) 2024 Boston Dynamics AI Institute Inc. All rights reserved. | ||
|
||
from datetime import datetime, timedelta | ||
from typing import Union | ||
|
||
from rclpy.duration import Duration | ||
from rclpy.time import Time | ||
|
||
|
||
def as_proper_time(time: Union[int, float, datetime, Time]) -> Time: | ||
"""Convert `time` to a proper, standardized `Time` object. | ||
For conversion, the following rules apply: | ||
- if an `int` or a `float` is provided, it is assumed to be a timestamp expressed in seconds. | ||
- if a `datetime` is provided, its UTC `datetime.timestamp()` is used. | ||
- if a `Duration` is provided, it is returned as is. | ||
Args: | ||
time: the time to be converted. | ||
Returns: | ||
an standardized `Time` object in ROS time, representing the given `time`. | ||
Raises: | ||
ValueError: if the given `time` is not of a supported type. | ||
""" | ||
if isinstance(time, (int, float)): | ||
return Time(seconds=time) | ||
if isinstance(time, datetime): | ||
return Time(seconds=time.timestamp()) | ||
if not isinstance(time, Time): | ||
raise ValueError(f"unsupported time type: {time}") | ||
return time | ||
|
||
|
||
def as_proper_duration(duration: Union[int, float, timedelta, Duration]) -> Duration: | ||
"""Convert `duration` to a proper, standardized `Duration` object. | ||
For conversion, the following rules apply: | ||
- if an `int` or a `float` is provided, it is assumed to be a duration expressed in seconds. | ||
- if a `timedelta` is provided, `timedelta.total_seconds()` are used. | ||
- if a `Duration` is provided, it is returned as is. | ||
Args: | ||
duration: the duration to be converted. | ||
Returns: | ||
an standardized `Duration` object representing the given `duration`. | ||
Raises: | ||
ValueError: if the given `duration` is not of a supported type. | ||
""" | ||
if isinstance(duration, (int, float)): | ||
return Duration(seconds=duration) | ||
if isinstance(duration, timedelta): | ||
return Duration(seconds=duration.total_seconds()) | ||
if not isinstance(duration, Duration): | ||
raise ValueError(f"unsupported duration type: {duration}") | ||
return duration |