datetime.strptime()#

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

Constructs a datetime value from date_string, parsed according to format. Format codes are different from Python’s datetime.strptime() function. See Format Codes for details. Must be called in a rule or query context.

Parameters#

NameTypeDescription
date_stringstr or ProducerThe date string to convert to a 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#

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.

Example#

Use datetime.strptime() to construct a datetime value from a date string parsed according to a format string. Common ISO format strings are available in the ISO namespace:

#import relationalai as rai
from relationalai.std.dates import datetime, ISO


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

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

with model.rule():
    Person.add(id=1).set(name="Alice", birthdate_str="2001-01-01T13:00:00")
    Person.add(id=2).set(name="Bob", birthdate_str="February 2, 2002 at 2:00pm")


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

# Use ISO.SECONDS to parse birthdate_str as an ISO 8601 datetime string with
# seconds resolution (YYYY-MM-DDTHH:MM:SS). If the string does not match the
# format, it is filtered out and the birthdate property is not set.
with model.rule():
    person = Person()
    birthdate = datetime.strptime(person.birthdate_str, ISO.SECONDS)
    person.set(birthdate=birthdate)

with model.query() as select:
    person = Person()
    response = select(person.name, person.birthdate_str, person.birthdate)

print(response.results)
#     name               birthdate_str           birthdate
# 0  Alice         2001-01-01T13:00:00 2001-01-01 13:00:00
# 1    Bob  February 2, 2002 at 2:00pm                 NaT

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

#with model.rule():
    person = Person()

    with model.match():
        # Parse birthdate_str with ISO.SECONDS if the string is in ISO 8601 format.
        with model.case():
            birthdate = datetime.strptime(person.birthdate_str, ISO.SECONDS)
            person.set(birthdate=birthdate)

        # Otherwise, use a custom format to parse birthdate_str. Note that RAI
        # format codes differ from those used by Python's built-in datetime.strptime().
        with model.case():
            birthdate = datetime.strptime(person.birthdate_str, "U d, y")
            person.set(birthdate=birthdate)


with model.query() as select:
    person = Person()
    response = select(person.name, person.birthdate_str, person.birthdate)

print(response.results)
#     name        birthdate_str           birthdate
# 0  Alice  2001-01-01T13:00:00 2001-01-01 13:00:00
# 1    Bob     February 2, 2002 2002-02-02 00:00:00

See Also#