hours()#
relationalai.std.dates
#hours(n: int | Producer) -> Expression
Produces a period of n
hours.
Must be called in a rule or query context.
Parameters#
Name | Type | Description |
---|---|---|
n | int or Producer | The number of hours. |
Returns#
An Expression
that represents a period of n
hours.
Example#
Use hours()
to create a period of n
hours.
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, hours
# 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 5 hours to the event start datetime.
with model.query() as select:
event = Event(name="Conference")
new_datetime = event.starts_at + hours(5)
response = select(event.name, alias(new_datetime, "new_time"))
print(response.results)
# Output:
# name new_time
# 0 Conference 2021-01-01 14:00:00
You can’t add or subtract hours()
from a Date
.
See date_add()
and
date_subtract()
for details.