datetime.strptime()#

relationalai.std.dates
#datetime.strptime(date_string: str|Producer, format: str|Producer) -> Expression

Constructs a UTC datetime value from date_string, parsed according to format. If date_string and format are Producer objects, then datetime.strptime() also acts as a filter and removes non-string values from the producers. Strings that do not match the format are also filtered out. Must be called in a rule or query context.

IMPORTANT

The format codes used by RAI’s datetime.strptime() are different from Python’s datetime.strptime() function. See Format Codes for details.

Parameters#

NameTypeDescription
date_stringstr or ProducerThe date string to convert to a UTC datetime value.
formatstr or ProducerThe format string to parse the date string. See Format Codes for supported format codes.

Returns#

An Expression object.

Format Codes#

The following format codes are supported by datetime.strptime():

CodeExampleDescription
y2024Matches a year.
m1, 01Matches a 1 or 2-digit month.
d1, 01Matches a 1 or 2-digit day.
H00Matches 2-digit hours (24-hour clock)
I00Matches 2-digit hours (12-hour clock)
M00Matches 2-digit minutes.
S00Matches 2-digit seconds.
s.500Matches 3-digit milliseconds.
eMon, TuesMatches abbreviated days of the week.
EMonday, TuesdayMatches full days of the week.
pAM, amMatches AM/PM (case-insensitive).
yyyymmdd20240101Matches fixed-width year, month, and date.
z+04:00, +0400, UTC+4Matches a numeric UTC offset.
ZAsia/Dubai, UTCMatches names of time zones in the TZ database.
uJanMatches abbreviated months according to the locale.
UJanuaryMatches full months according to the locale.

Common ISO format strings are available in the ISO namespace.

Example#

Use datetime.strptime() to construct a UTC datetime value from a date string parsed according to a format string”

#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(time_str="2021-01-01T09:30:00")
    Event.add(id=2).set(time_str="invalid")


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

with model.rule():
    event = Event()
    # datetime.strptime() filters out any events with invalid time strings, so the
    # following only sets the time property for Event 1.
    event.set(time=dates.datetime.strptime(event.time_str, "y-m-dTH:M:S"))
    # Since Event 2 is filtered out above, the following only sets the
    # has_valid_time_str property for Event 1.
    event.set(has_valid_time_str=True)

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

print(response.results)
#    id                time has_valid_time_str
# 0   1 2021-01-01 09:30:00               True
# 1   2                 NaT                NaN

If a property is assigned to strings with multiple date formats, you can parse them all using a Model.match() block:

#with model.rule():
    Event.add(id=3).set(time_str="February 2, 2021 at 02:00 PM")

with model.rule():
    event = Event()
    with model.match():
        with model.case():
            # Set the time property for events with strings in the format "y-m-dTH:M:S".
            event.set(time=dates.datetime.strptime(event.time_str, "y-m-dTH:M:S"))
        with model.case():
            # Set the time property for events with strings in the format "U d, y H:Mp".
            event.set(time=dates.datetime.strptime(event.time_str, "U d, y at I:M p"))

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

print(response.results)
#    id                      time_str                time
# 0   1           2021-01-01T09:30:00 2021-01-01 09:30:00
# 1   2                       invalid                 NaT
# 2   3  February 2, 2021 at 02:00 PM 2021-02-02 14:00:00

If the string contains a timezone or offset, you can specify it in the format string, but note that the datetime is converted to UTC:

#with model.query() as select:
    dt = dates.datetime.strptime("2021-01-01T09:30:00+04:00", "y-m-dTH:M:Sz")
    response = select(dt)

print(response.results)
#                     v
# 0 2021-01-01 05:30:00  <-- 05:30:00 + 4 hours = 09:30:00

See Also#