days_to_int()#
relationalai.std.dates
#days_to_int(days: Producer) -> Expression
Converts a period of days, like those created with days()
, to an integer value.
Any values produced by the days
argument that are not periods of days are filtered out.
Must be called in a rule or query context.
Parameters#
Name | Type | Description |
---|---|---|
days | Producer | The period of days to convert to an integer value. |
Returns#
An Expression
object.
Example#
Use days_to_int()
to convert a period of days to an integer value:
#import relationalai as rai
from relationalai.std.dates import days, days_to_int
model = rai.Model("MyModel")
with model.query() as select:
num_days = days_to_int(days(2))
response = select(num_days)
print(response.results)
# v
# 0 2
The days_to_int()
function is useful when you need to subtract two dates and do arithmetic with the result.
For example, the following calculates an age
property of Person
entities based on their birthdate
:
#import relationalai as rai
from relationalai.std.dates import date, days_to_int
model = rai.Model("MyModel")
Person = model.Type("Person")
# Add some people to the model.
with model.rule():
Person.add(name="Alice", birthdate=date(1990, 1, 1))
Person.add(name="Bob", birthdate=date(1985, 4, 15))
Person.add(name="Charlie", birthdate=date(1975, 12, 31))
# Define a rule to calculate the age of each person.
with model.rule():
today = date(2025, 1, 1)
person = Person()
age = days_to_int(today - person.birthdate) // 365
person.set(age=age)
# Query the model for the age of each person.
with model.query() as select:
person = Person()
response = select(person.name, person.age)
print(response.results)
# name age
# 0 Alice 35
# 1 Bob 39
# 2 Charlie 49