I have a model where i have datetime type fields defined as shown:
class DamBaseModel(BaseModel):
class Config:
allow_population_by_field_name = True
use_enum_values = True
arbitrary_types_allowed = True
json_encoders = {
ObjectId: str,
datetime: lambda d: d.isoformat
}
The defined Model is as follows:
class Message(DamBaseModel):
created_datetime: datetime = Field(default_factory=datetime.now)
The data that gets inserted into MongoDb is :
{ "created_datetime" : ISODate("2022-08-22T12:02:59.546Z") }
But the problem iam currently facing is that the data is received at the client level in this format :
{ "created_datetime": "Mon, 22 Aug 2022 12:02:59 GMT" }
Iam just fetching the data from the db and projecting it directly without any formatting. Any help as to how to specify local timezone when projecting the data ??
Timezone aware timestamps are now supported in Pydantic V2:
from pydantic import AwareDatetime, TypeAdapter
dt = AwareDatetime
ta = TypeAdapter(dt)
# validate UTC timestamp
ta.validate_python("2022-08-22T12:02:59.546Z")
# Validate timestamp with timezone
ta.validate_python("2022-08-22T12:02:59.546+00:00")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With