Skip to content

relationalai.std.dates.date.fromordinal()

date.fromordinal(ordinal: int|Producer) -> Expression

Constructs a date value from a Gregorian proleptic ordinal number. If ordinal is a Producer, then date.fromordinal() also acts as a filter and removes non-integer values from the producer. Must be called in a rule or query context.

A Gregorian proleptic ordinal number is given as the number of days since January 1, 0001 (day 1).

NameTypeDescription
ordinalint or ProducerThe Gregorian proleptic ordinal number representing the date.

An Expression object.

Use date.fromordinal() to construct a date value from a Gregorian proleptic ordinal number:

import relationalai as rai
from relationalai.std import dates
# =====
# SETUP
# =====
model = rai.Model("MyModel")
Event = model.Type("Event")
with model.rule():
Event.add(id=1).set(ordinal=737791)
Event.add(id=2).set(ordinal="invalid")
# =======
# EXAMPLE
# =======
with model.rule():
event = Event()
# date.fromordinal() filters out any events with invalid ordinals, so the
# following only sets the date property for Event 1.
event.set(date=dates.date.fromordinal(event.ordinal))
# Since Event 2 is filtered out above, the following only sets the
# has_valid_ordinal property for Event 1.
event.set(has_valid_ordinal=True)
with model.query() as select:
event = Event()
response = select(event.id, event.date, event.has_valid_ordinal)
print(response.results)
# id date has_valid_ordinal
# 0 1 2021-01-01 True
# 1 2 NaT NaN