Util¶
Common utilities for API Python code.
-
bosdyn.util.
duration_str
(duration)[source]¶ Return a formatted string for a duration proto.
- Parameters
duration (google.protobuf.Duration) – input duration
- Returns
format ‘{seconds}.{nanoseconds}’.
- Return type
string
-
bosdyn.util.
duration_to_seconds
(duration)[source]¶ Return a number of seconds, as a float, based on Duration protobuf fields.
If you want a precise duration, use the Duration class’s ToTimedelta function.
- Parameters
duration (google.protobuf.Duration) – input duration
-
bosdyn.util.
seconds_to_duration
(seconds)[source]¶ Return a protobuf Duration from number of seconds, as a float.
- Parameters
seconds (float) – duration length
-
bosdyn.util.
seconds_to_timestamp
(seconds)[source]¶ Return a protobuf Timestamps from number of seconds, as a float.
- Parameters
seconds (float) – seconds since epoch
-
bosdyn.util.
timestamp_str
(timestamp)[source]¶ Return a formatted string for a timestamp or a duration proto.
- Parameters
timestamp (google.protobuf.Timestamp or google.protobuf.Duration) – input duration
- Returns
A string with format ‘{seconds}.{nanoseconds}’.
-
bosdyn.util.
sec_to_nsec
(secs)[source]¶ Convert time in seconds as from time.time() to a timestamp in nanoseconds.
- Parameters
secs – Time in seconds
- Returns
The time in nanoseconds, as an integer.
-
bosdyn.util.
now_nsec
()[source]¶ Returns nanoseconds from dawn of unix epoch until when this is called.
-
bosdyn.util.
set_timestamp_from_now
(timestamp_proto)[source]¶ Sets google.protobuf.Timestamp to point to the current time on the system clock.
- Parameters
timestamp_proto[out] (google.protobuf.Timestamp) – The proto into which the time will be written
-
bosdyn.util.
now_timestamp
()[source]¶ Returns a google.protobuf.Timestamp set to the current time on the system clock.
-
bosdyn.util.
set_timestamp_from_nsec
(timestamp_proto, time_nsec)[source]¶ Sets a Timestamp protobuf from an integer of nanoseconds since the unix epoch.
- Parameters
timestamp_proto[out] (google.protobuf.Timestamp) – timestamp into which time will be written
time_nsec[in] – the time, as an integer of nanoseconds from the unix epoch
-
bosdyn.util.
set_timestamp_from_datetime
(timestamp_proto, date_time)[source]¶ Sets a Timestamp protobuf from datetime.datetime value.
- Parameters
timestamp_proto[out] (google.protobuf.Timestamp) – timestamp into which time will be written
date_time[in] (datetime.datetime) –
-
bosdyn.util.
nsec_to_timestamp
(time_nsec)[source]¶ Returns a google.protobuf.Timestamp for an integer value of nanoseconds since the unix epoch.
- Parameters
time_nsec (int) – the time, as an integer of nanoseconds from the unix epoch
-
bosdyn.util.
timestamp_to_sec
(timestamp_proto)[source]¶ From a Timestamp proto, return a floating point value of seconds from the unix epoch .
- Parameters
timestamp_proto (google.protobuf.Timestamp) – input time
-
bosdyn.util.
timestamp_to_nsec
(timestamp_proto)[source]¶ From a Timestamp proto, return an integer of nanoseconds from the unix epoch.
- Parameters
timestamp_proto (google.protobuf.Timestamp) – input time
-
bosdyn.util.
timestamp_to_datetime
(timestamp_proto, use_nanos=True)[source]¶ Convert a google.protobuf.Timestamp to a Python datetime.datetime object.
- Parameters
timestamp_proto (google.protobuf.Timestamp) – input time
use_nanos (bool) – use fractional seconds in proto (default=True)
- Returns
a datetime.datetime proto
-
bosdyn.util.
secs_to_hms
(seconds)[source]¶ Format a time in seconds as ‘H:MM:SS’
- Parameters
seconds – number of seconds (will be truncated to integer value)
-
bosdyn.util.
distance_str
(meters)[source]¶ Convert a distance in meters to a either xxx.xx m or xxx.xx km
- Parameters
meters (float/int) – distance in meters
-
bosdyn.util.
format_metric
(metric)[source]¶ Returns a string representing a metric.
- Parameters
metric (bosdyn.api.Parameter) – metric description
-
exception
bosdyn.util.
DatetimeParseError
[source]¶ Bases:
Exception
Failed to parse any datetime formats known to parse_datetime()
-
bosdyn.util.
parse_datetime
(val)[source]¶ Parse datetime from string
- Parameters
val – string with format like as described by TIME_FORMAT_DESC.
Returns: datetime.datetime.
Raises: DatetimeParseError if format of val is not recognized.
-
class
bosdyn.util.
RobotTimeConverter
(robot_clock_skew_nsec)[source]¶ Bases:
object
Converts times in the local system clock to times in the robot clock.
Conversions are made given an estimate of clock skew from the local clock to the robot clock.
-
robot_timestamp_from_local_nsecs
(local_time_nsecs)[source]¶ Returns a robot-clock Timestamp proto for a local time in nanoseconds.
- Parameters
local_time_nsecs – Local system time time, in integer of nanoseconds from the unix epoch.
-
robot_timestamp_from_local_secs
(local_time_secs)[source]¶ Returns a robot-clock Timestamp proto for a local time in seconds.
- Parameters
local_time_secs – Local system time time, in seconds from the unix epoch.
-