---
sidebar:
  label: roboto.time
  order: 29
title: roboto.time
---
## Module Contents

### MAX_32BIT_EPOCH_SECONDS

```python
roboto.time.MAX_32BIT_EPOCH_SECONDS = 2147483647
```

`from roboto.time import MAX_32BIT_EPOCH_SECONDS`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/time.py#L21-L21)

the instant 2038-01-19 03:14:07 UTC.

No SDK method reads it.

### NSEC_PER_MS

```python
roboto.time.NSEC_PER_MS = 1000000
```

`from roboto.time import NSEC_PER_MS`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/time.py#L30-L30)

Number of nanoseconds in a millisecond.

### NSEC_PER_SEC

```python
roboto.time.NSEC_PER_SEC = 1000000000
```

`from roboto.time import NSEC_PER_SEC`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/time.py#L27-L27)

Number of nanoseconds in a second.

### NSEC_PER_US

```python
roboto.time.NSEC_PER_US = 1000
```

`from roboto.time import NSEC_PER_US`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/time.py#L33-L33)

Number of nanoseconds in a microsecond.

### Time

```python
type roboto.time.Time = typing.Union[int, float, decimal.Decimal, str, datetime.datetime]
```

`from roboto.time import Time`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/time.py#L40-L40)

Point in time in any form `to_epoch_nanoseconds` accepts.

### TimeUnit

```python
class roboto.time.TimeUnit
```

`from roboto.time import TimeUnit`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/time.py#L44-L65)

Bases: `roboto.compat.StrEnum`

Unit of a timestamp in recorded data.

**Attributes**

- **TimeUnit.Microseconds** = `'us'`
- **TimeUnit.Milliseconds** = `'ms'`
- **TimeUnit.Nanoseconds** = `'ns'`
- **TimeUnit.Seconds** = `'s'`

#### TimeUnit.nano_multiplier()

```python
def nano_multiplier() -> int
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/time.py#L55-L65)

Return the number of nanoseconds in one of this unit.

**Returns**

- `int`

### log

```python
roboto.time.log
```

`from roboto.time import log`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/time.py#L19-L19)

### to_epoch_nanoseconds()

```python
def roboto.time.to_epoch_nanoseconds(value: Time, unit: Optional[TimeUnit] = None)
```

`from roboto.time import to_epoch_nanoseconds`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/time.py#L68-L152)

Convert a time to nanoseconds since the Unix epoch (1970-01-01 00:00:00 UTC).

How `value` is read depends on its type:

- `int`, or any other integer type, such as a `numpy.int64` read out of a NumPy or pandas column: a number of `unit`, nanoseconds by default.
- `float`: a number of `unit`, seconds by default. A `float` can lose precision, so pass a `str` or `decimal.Decimal` when you can.
- `decimal.Decimal` or numeric `str`: a number of `unit`, seconds by default, such as a ROS timestamp in the form `"<sec>.<nsec>"`.
- Any other `str`: an ISO 8601 timestamp, such as `"2024-05-16T10:25:47Z"`, read as the `datetime.datetime` it names.
- `datetime.datetime`: that instant. A naive datetime is read as UTC, and a datetime before 1970 converts to a negative number of nanoseconds.

A fraction of a nanosecond is dropped.

**Parameters**

- **value** (`Time`): Time to convert.
- **unit** (`Optional[TimeUnit]`): Unit of a numeric `value`. `None` uses the default for the type of `value`, listed above. Ignored for a `datetime.datetime` or an ISO 8601 string.

**Raises**

- `TypeError`: `value` is not one of the types above.
- `ValueError`: `value` is a `bool` (which Python counts as an `int`), a negative number, NaN, or a string that is neither a number nor an ISO 8601 timestamp.
- `OverflowError`: `value` is infinite, such as `float("inf")` or `"inf"`.

**Usage**

An `int` is nanoseconds and a numeric string is seconds, unless `unit` says otherwise:

```python
from roboto.time import TimeUnit, to_epoch_nanoseconds
to_epoch_nanoseconds(1_700_000_000_000_000_000)
# 1700000000000000000
to_epoch_nanoseconds("1700000000.5")
# 1700000000500000000
to_epoch_nanoseconds(1_700_000_000, unit=TimeUnit.Seconds)
# 1700000000000000000
```

An ISO 8601 string is read as the instant it names:

```python
to_epoch_nanoseconds("2024-05-16T10:25:47Z")
# 1715855147000000000
```

### utcnow()

```python
def roboto.time.utcnow() -> datetime.datetime
```

`from roboto.time import utcnow`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/time.py#L197-L199)

Return the current time as a timezone-aware `datetime.datetime` in UTC.

**Returns**

- `datetime.datetime`
