Skip to content

relationalai.std.dates.datetime.fromordinal()

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

Constructs a UTC datetime value from a Gregorian proleptic ordinal number with the sub-day time components all set to zero. If ordinal is a Producer, then datetime.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 ProducerA Gregorian proleptic ordinal number.

An Expression object.

Use datetime.fromordinal() to construct a UTC datetime 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) # January 1, 2021
Event.add(id=2).set(ordinal="invalid")
# =======
# EXAMPLE
# =======
with model.rule():
event = Event()
# datetime.fromordinal() filters out any events with invalid ordinals, so
# the following only sets the date property for Event 1.
event.set(date=dates.datetime.fromordinal(event.ordinal))
# Since Event 2 was filtered above, the following only sets the
# has_valid_ordinal property for the 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