66)
77from uuid import UUID
88
9+ try :
10+ import zoneinfo
11+ except ImportError :
12+ from backports import zoneinfo
13+
914
1015NoneType = type (None )
1116PyType = TypeVar ('PyType' )
@@ -47,6 +52,9 @@ def __repr__(self):
4752class StrType (BaseType ):
4853 py_type = str
4954
55+ def __init__ (self , * params ):
56+ pass
57+
5058 @classmethod
5159 def escape (cls , value : str , escape = None ) -> str :
5260 value = value .replace ('\\ ' , '\\ \\ ' ).replace ("'" , "\\ '" )
@@ -70,6 +78,9 @@ class FloatType(BaseType):
7078class DecimalType (BaseType ):
7179 py_type = Decimal
7280
81+ def __init__ (self , * params ):
82+ pass
83+
7384 @classmethod
7485 def escape (cls , value : PyType , escape : Callable ) -> str :
7586 return f"'{ value } '"
@@ -101,6 +112,14 @@ def from_json(self, value: str) -> Optional[date]:
101112class DateTimeType (BaseType ):
102113 py_type = datetime
103114
115+ __slots__ = ('_tzinfo' ,)
116+
117+ def __init__ (self , tz_name = None ):
118+ if tz_name is None :
119+ self ._tzinfo = None
120+ else :
121+ self ._tzinfo = zoneinfo .ZoneInfo (tz_name )
122+
104123 @classmethod
105124 def escape (cls , value : datetime , escape = None ) -> str :
106125 value = value .replace (tzinfo = None , microsecond = 0 )
@@ -114,7 +133,10 @@ def to_json(cls, value: datetime, to_json: Callable) -> JsonType:
114133 def from_json (self , value : str ) -> Optional [datetime ]:
115134 if value == '0000-00-00 00:00:00' :
116135 return None
117- return datetime .fromisoformat (value )
136+ result = datetime .fromisoformat (value )
137+ if self ._tzinfo is not None :
138+ result = result .replace (tzinfo = self ._tzinfo )
139+ return result
118140
119141
120142class DateTimeUTCType (DateTimeType ):
@@ -144,7 +166,11 @@ def to_json(cls, value: datetime, to_json: Callable) -> JsonType:
144166 return value .isoformat ()
145167
146168 def from_json (self , value : str ) -> datetime :
147- return datetime .fromisoformat (value ).replace (tzinfo = timezone .utc )
169+ result = datetime .fromisoformat (value )
170+ if self ._tzinfo is None :
171+ return result .replace (tzinfo = timezone .utc )
172+ else :
173+ return result .replace (tzinfo = self ._tzinfo ).astimezone (timezone .utc )
148174
149175
150176class UUIDType (BaseType ):
0 commit comments