Skip to content

Commit e4becc9

Browse files
committed
epoch - docs & tests
1 parent 6f0b765 commit e4becc9

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

pydantic_extra_types/epoch.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ def _f(cls, value: Any, serializer: Callable[[Any], Any]) -> Any: # pragma: no
4343

4444

4545
class Number(_Base):
46+
"""epoch.Number parses unix timestamp as float and converts it to datetime.
47+
48+
```py
49+
from pydantic import BaseModel
50+
51+
from pydantic_extra_types import epoch
52+
53+
class LogEntry(BaseModel):
54+
timestamp: epoch.Number
55+
56+
logentry = LogEntry(timestamp=1.1)
57+
print(logentry)
58+
#> timestamp=datetime.datetime(1970, 1, 1, 0, 0, 1, 100000, tzinfo=datetime.timezone.utc)
59+
```
60+
"""
61+
4662
TYPE = 'number'
4763
SCHEMA = core_schema.float_schema()
4864

@@ -53,6 +69,23 @@ def _f(cls, value: Any, serializer: Callable[[float], float]) -> float:
5369

5470

5571
class Integer(_Base):
72+
"""epoch.Integer parses unix timestamp as integer and converts it to datetime.
73+
74+
```
75+
```py
76+
from pydantic import BaseModel
77+
78+
from pydantic_extra_types import epoch
79+
80+
class LogEntry(BaseModel):
81+
timestamp: epoch.Integer
82+
83+
logentry = LogEntry(timestamp=1)
84+
print(logentry)
85+
#> timestamp=datetime.datetime(1970, 1, 1, 0, 0, 1, tzinfo=datetime.timezone.utc)
86+
```
87+
"""
88+
5689
TYPE = 'integer'
5790
SCHEMA = core_schema.int_schema()
5891

tests/test_json_schema.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from typing_extensions import Annotated
1212

1313
import pydantic_extra_types
14+
from pydantic_extra_types import epoch
1415
from pydantic_extra_types.color import Color
1516
from pydantic_extra_types.coordinate import Coordinate, Latitude, Longitude
1617
from pydantic_extra_types.country import CountryAlpha2, CountryAlpha3, CountryNumericCode, CountryShortName
@@ -464,6 +465,40 @@
464465
],
465466
},
466467
),
468+
(
469+
epoch.Integer,
470+
{
471+
'title': 'Model',
472+
'type': 'object',
473+
'properties': {
474+
'x': {
475+
'title': 'X',
476+
'type': 'integer',
477+
'format': 'date-time',
478+
},
479+
},
480+
'required': [
481+
'x',
482+
],
483+
},
484+
),
485+
(
486+
epoch.Number,
487+
{
488+
'title': 'Model',
489+
'type': 'object',
490+
'properties': {
491+
'x': {
492+
'title': 'X',
493+
'type': 'number',
494+
'format': 'date-time',
495+
},
496+
},
497+
'required': [
498+
'x',
499+
],
500+
},
501+
),
467502
],
468503
)
469504
def test_json_schema(cls, expected):

0 commit comments

Comments
 (0)