forked from RunestoneInteractive/BookServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtzparsetest.py
More file actions
33 lines (26 loc) · 788 Bytes
/
tzparsetest.py
File metadata and controls
33 lines (26 loc) · 788 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from datetime import datetime
from pydantic import BaseModel, validator
from dateutil.parser import isoparse
first_format = {
"time": "2018-01-05 16:59:33+00:00",
}
# second_format = {'time': '2021-03-05T08:21:00.000Z',}
second_format = {
"time": "2022-01-16T06:07:00.000Z",
}
class TimeModel(BaseModel):
time: datetime
class Config:
json_encoders = {
datetime: lambda v: v.isoformat(),
}
@validator("time", pre=True)
def time_validate(cls, v):
# return datetime.fromisoformat(v)
return isoparse(v)
print(TimeModel.parse_obj(first_format).json())
print("first_format successfull")
print(TimeModel.parse_obj(second_format))
print("second_format successfull")
x = TimeModel(time=first_format["time"])
print(x.time)