microseconds()#

relationalai.std.dates
#microseconds(n: int | Producer) -> Expression

Produces a period of n microseconds. Must be called in a rule or query context.

Parameters#

NameTypeDescription
nint or ProducerThe number of microseconds.

Returns#

An Expression that represents a period of n microseconds.

Example#

Use microseconds() to create a period of n microseconds. You can add and subtract time periods from Date or DateTime values using the + and - operators:

#import relationalai as rai
from relationalai.std import alias
from relationalai.std.dates import datetime, microseconds

# Create a model named "events" with an `Event` type.
model = rai.Model("events")
Event = model.Type("Event")

# Add an event with a start datetime.
with model.rule():
    Event.add(
        name="Conference",
        starts_at=datetime(2021, 1, 1, 9, 0, 0, tz="UTC")
    )

# Add 500 microseconds to the event start datetime.
with model.query() as select:
    event = Event(name="Conference")
    new_datetime = event.starts_at + microseconds(500)
    response = select(event.name, alias(new_datetime, "new_time"))

# NOTE: Depending on the query output format, which is a pandas
# DataFrame by default, the microseconds might not be displayed.
print(response.results)
# Output:
#          name            new_time
# 0  Conference 2021-01-01 09:00:00

You can’t add or subtract microseconds() from a Date. See date_add() and date_subtract() for details.

See Also#