DateTime()#

relationalai.std.dates
#DateTime(val: Producer) -> Expression

Filters values produced by a Producer object to only include datetime values. Must be called in a rule or query context.

Parameters#

NameTypeDescription
valProducerThe values to filter.

Returns#

An Expression object.

Example#

Use DateTime() to filter datetime values in a rule or query:

#import relationalai as rai
from relationalai.std import alias, dates


# =====
# SETUP
# =====

model = rai.Model("MyModel")
Event = model.Type("Event")

with model.rule():
    Event.add(id=1).set(time="2021-01-01T12:00:00")  # time is a string.
    Event.add(id=2).set(time=dates.datetime(2021, 1, 1, 12, 0, 0))  # time is a datetime.


# =======
# EXAMPLE
# =======

# Get the names and event times of people where 'event_time' is a DateTime.
with model.query() as select:
    event = Event()
    dates.DateTime(event.time)  # Filter datetime values.
    response = select(event.id, event.time)

print(response.results)
#    id                time
# 0   2 2021-01-01 12:00:00

You can use DateTime() in a Model.match() block to conditionally write a function that returns an Expression that produces True or False depending on whether the input is a datetime value.

#def is_datetime(val):
    with model.match() as result:
        with model.case():
            dates.DateTime(val)
            result.add(True)
        with model.case():
            result.add(False)
    return result

with model.query() as select:
    event = Event()
    response = select(event.id, event.time, alias(is_datetime(event.time), "is_datetime"))

print(response.results)
#    id                 time  is_datetime
# 0   1  2021-01-01T12:00:00        False
# 1   2  2021-01-01 12:00:00         True

See Also#