1+ import datetime
12from collections .abc import Sized
23from datetime import timezone
3- from typing import Required , TypedDict
4+ from typing import Required , Self , TypedDict
45
56from bson import ObjectId
67from pydantic import (
1415 ValidationInfo ,
1516 field_serializer ,
1617 field_validator ,
18+ model_validator ,
1719)
1820from pydantic .functional_validators import BeforeValidator
1921from pydantic .json_schema import SkipJsonSchema
@@ -55,6 +57,7 @@ class DataRequest(BaseModel):
5557 authors : list [Author ]
5658 geometry : GeoJSON | None
5759 temporal : Temporal
60+ tz_offset : SkipJsonSchema [list [float ] | None ] = Field (default = None , exclude = True )
5861 links : Links
5962 path : str
6063 contact : EmailStr
@@ -78,6 +81,21 @@ def validate_geometries(cls, value: GeoJSON | None) -> dict | None:
7881 validate_collapsible (value )
7982 return value
8083
84+ @model_validator (mode = "after" )
85+ def get_tz_offset (self ) -> Self :
86+ """Store the timezone offset for the temporal data."""
87+ if self .temporal is not None :
88+ self .tz_offset = [datetime .datetime .utcoffset (t ).total_seconds () for t in self .temporal ]
89+ return self
90+
91+ @field_serializer ("temporal" )
92+ def convert_from_utc (self , value : Temporal , info : FieldSerializationInfo ) -> list [str ]:
93+ """Apply the timezone offset to convert this from UTC to a date in the correct timezone."""
94+ return [
95+ t .astimezone (datetime .timezone (datetime .timedelta (seconds = self .tz_offset [i ]))).isoformat ()
96+ for i , t in enumerate (value )
97+ ]
98+
8199 @field_serializer ("user" )
82100 def require_user_set (self , value : str , info : FieldSerializationInfo ) -> str :
83101 """Require that the user_name is set when the model is serialized."""
0 commit comments