hour()#
relationalai.std.dates
#hour(date: datetime | Producer, tz: str = "UTC") -> Expression
Extracts the hour component from a DateTime
value.
If date
is a Python datetime
object, the tz
parameter is ignored.
Must be called in a rule or query context.
Parameters#
Name | Type | Description |
---|---|---|
date | datetime or Producer | The DateTime value from which to extract the hour. |
tz | str | Optional timezone component for DateTime values (default is “UTC”). Ignored if date is a Python datetime object. |
Returns#
An Expression
that produces the hour component of the given DateTime
value.
Raises#
TypeError
if the date
parameter is not a datetime
or Producer
object.
Example#
Use hour()
to extract the hour component from a DateTime
value:
#import relationalai as rai
from relationalai.std import alias
from relationalai.std.dates import hour, datetime
# Create a model named "people" with a `Person` type.
model = rai.Model("people")
Person = model.Type("Person")
# Add a person with a birth datetime.
with model.rule():
Person.add(
name="Bob",
born_at=datetime(1985, 8, 15, 15, 45, 0, tz="UTC")
)
# Extract the hour from `born_at`.
with model.query() as select:
person = Person()
birth_hour = hour(person.born_at)
response = select(person.name, alias(birth_hour, "birth_hour"))
print(response.results)
# Output:
# name birth_hour
# 0 Bob 15