Datetimes
Pydantic supports the following datetime types:
datetime.datedatetime.timedatetime.datetimedatetime.timedelta
Validation of datetime types¶
-
datetimefields will accept values of type: -
datetime; an existingdatetimeobject intorfloat; assumed as Unix time, i.e. seconds (if >=-2e10and <=2e10) or milliseconds (if <-2e10or >2e10) since 1 January 1970-
str; the following formats are accepted:YYYY-MM-DD[T]HH:MM[:SS[.ffffff]][Z or [±]HH[:]MM]intorfloatas a string (assumed as Unix time)
-
datefields will accept values of type: -
date; an existingdateobject intorfloat; handled the same as described fordatetimeabove-
str; the following formats are accepted:YYYY-MM-DDintorfloatas a string (assumed as Unix time)
-
timefields will accept values of type: -
time; an existingtimeobject -
str; the following formats are accepted:HH:MM[:SS[.ffffff]][Z or [±]HH[:]MM]
-
timedeltafields will accept values of type: -
timedelta; an existingtimedeltaobject intorfloat; assumed to be seconds-
str; the following formats are accepted:[-][DD ][HH:MM]SS[.ffffff][±]P[DD]DT[HH]H[MM]M[SS]S(ISO 8601 format for timedelta)
from datetime import date, datetime, time, timedelta
from pydantic import BaseModel
class Model(BaseModel):
d: date = None
dt: datetime = None
t: time = None
td: timedelta = None
m = Model(
d=1679616000.0,
dt='2032-04-23T10:20:30.400+02:30',
t=time(4, 8, 16),
td='P3DT12H30M5S',
)
print(m.model_dump())
"""
{'d': datetime.date(2023, 3, 24), 'dt': datetime.datetime(2032, 4, 23, 10, 20, 30, 400000, tzinfo=TzInfo(+02:30)), 't': datetime.time(4, 8, 16), 'td': datetime.timedelta(days=3, seconds=45005)}
"""
Pydantic date types¶
The following types can be imported from pydantic, and augment the types described above
with additional validation constraints:
PastDate- like
date, with the constraint that the value must be in the past FutureDate- like
date, with the constraint that the value must be in the future PastDatetime- like
PastDate, but fordatetime FutureDatetime- like
FutureDate, but fordatetime AwareDatetime- like
datetime, with the constraint that the value must have timezone info NaiveDatetime- like
datetime, with the constraint that the value must lack timezone info