Skip to main content

BaseTemporalField

This class serves as an abstract base for temporal data fields, providing a framework for parsing string values into Python date or time objects. It manages a collection of acceptable input formats and implements a trial-based parsing strategy through the to_python method. Subclasses are required to implement the strptime method to define specific parsing logic for different temporal types.

Attributes

AttributeTypeDescription
input_formatslist of stringsA list of datetime format strings used sequentially by to_python to attempt parsing a string value into a Python object.

Constructor

Signature

def BaseTemporalField(
input_formats: list|null = None,
**kwargs: dict
) - > null

Parameters

NameTypeDescription
input_formats`listnull` = None
**kwargsdictArbitrary keyword arguments passed to the parent Field class constructor.

Methods


to_python()

@classmethod
def to_python(
value: string
) - > object

Converts the input string into a Python object by attempting to parse it against each configured input format. Raises a ValidationError if the value does not match any of the expected formats.

Parameters

NameTypeDescription
valuestringThe raw input string to be parsed and converted into a temporal object.

Returns

TypeDescription
objectA Python representation of the temporal value, typically a date, time, or datetime object.

strptime()

@classmethod
def strptime(
value: string,
format: string
) - > object

Parses a string value according to a specific format; this method must be implemented by subclasses to handle specific temporal types.

Parameters

NameTypeDescription
valuestringThe string representation of the temporal data to parse.
formatstringThe specific format string used to interpret the input value.

Returns

TypeDescription
objectThe parsed temporal object corresponding to the provided format.