datetime.fromordinal()#
relationalai.std.dates
#datetime.fromordinal(ordinal: int | Producer) -> Expression
Constructs a DateTime
value from a Gregorian proleptic ordinal number.
Must be called in a rule or query context.
A Gregorian proleptic ordinal number is the number of days since January 1, 0001 (day 1).
The sub-day components of the resulting DateTime
are set to 0.
To construct a Date
value, use the date.fromordinal
function.
Parameters#
Name | Type | Description |
---|---|---|
ordinal | int or Producer | The Gregorian proleptic ordinal number representing the datetime. |
Returns#
An Expression
that produces DateTime
values.
Example#
Use datetime.fromordinal()
to construct a DateTime
value from a Gregorian proleptic ordinal number:
#import relationalai as rai
from relationalai.std.dates import datetime
# Create a model named "events" with an `Event` type.
model = rai.Model("events")
Event = model.Type("Event")
# Add an event with a start datetime using an ordinal number.
with model.rule():
# The event starts on January 1, 2021 (ordinal 737791).
Event.add(
name="Conference",
start_datetime=datetime.fromordinal(737791)
)
with model.query() as select:
event = Event()
response = select(event.name, event.start_datetime)
print(response.results)
# Output:
# name start_datetime
# 0 Conference 2021-01-01
You can also use object property values as ordinal numbers:
#import relationalai as rai
from relationalai.std.dates import datetime
# Create a model named "events" with an `Event` type.
model = rai.Model("events")
Event = model.Type("Event")
# Add events with ordinal start datetimes.
with model.rule():
Event.add(
name="Conference",
start_ordinal=737791
)
Event.add(
name="Workshop",
start_ordinal=737792
)
# Create a start_datetime property using the datetime.fromordinal() constructor.
with model.rule():
event = Event()
event.set(
start_datetime=datetime.fromordinal(event.start_ordinal)
)
with model.query() as select:
event = Event()
response = select(event.name, event.start_datetime)
print(response.results)
# Output:
# name start_datetime
# 0 Conference 2021-01-01
# 1 Workshop 2021-01-02