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
| Attribute | Type | Description |
|---|---|---|
| input_formats | list of strings | A 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
| Name | Type | Description |
|---|---|---|
| input_formats | `list | null` = None |
| **kwargs | dict | Arbitrary 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
| Name | Type | Description |
|---|---|---|
| value | string | The raw input string to be parsed and converted into a temporal object. |
Returns
| Type | Description |
|---|---|
object | A 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
| Name | Type | Description |
|---|---|---|
| value | string | The string representation of the temporal data to parse. |
| format | string | The specific format string used to interpret the input value. |
Returns
| Type | Description |
|---|---|
object | The parsed temporal object corresponding to the provided format. |