datetime.strptime()#
#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.
The format codes used by RAI’s datetime.strptime()
are different from Python’s datetime.strptime()
function.
See Format Codes for details.
Parameters#
Name | Type | Description |
---|---|---|
date_string | str or Producer | The date string to convert to a UTC 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#
The following format codes are supported by datetime.strptime()
:
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. |
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