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#
Name | Type | Description |
---|---|---|
date_string | str or Producer | The date string to convert to a datetime value. |
format | str or Producer | The format string to parse the date string. See Format Codes for supported format codes. |
Returns#
An Expression
object.
Format Codes#
Code | Example | Description |
---|---|---|
y | 2024 | Matches a year. |
m | 1, 01 | Matches a 1 or 2-digit month. |
d | 1, 01 | Matches a 1 or 2-digit day. |
H | 00 | Matches 2-digit hours (24-hour clock) |
I | 00 | Matches 2-digit hours (12-hour clock) |
M | 00 | Matches 2-digit minutes. |
S | 00 | Matches 2-digit seconds. |
s | .500 | Matches 3-digit milliseconds. |
e | Mon, Tues | Matches abbreviated days of the week. |
E | Monday, Tuesday | Matches full days of the week. |
p | AM, am | Matches AM/PM (case-insensitive). |
yyyymmdd | 20240101 | Matches fixed-width year, month, and date. |
z | +04:00, +0400, UTC+4 | Matches a numeric UTC offset. |
Z | Asia/Dubai, UTC | Matches names of time zones in the TZ database. |
u | Jan | Matches abbreviated months according to the locale. |
U | January | Matches 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