-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Epoch - data type for unix timestamps using pydantic-extra-types (#300)
* v20/v3x - epoch datetime values have a datatype for epoch datetimes type:integer/number with format:date-time is datetime c.f. pydantic/pydantic-extra-types#240
- Loading branch information
Showing
11 changed files
with
159 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from __future__ import annotations | ||
|
||
import datetime | ||
from typing import Any, Callable | ||
|
||
import pydantic_core.core_schema | ||
from pydantic import GetJsonSchemaHandler | ||
from pydantic.json_schema import JsonSchemaValue | ||
from pydantic_core import CoreSchema, core_schema | ||
|
||
EPOCH = datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc) | ||
|
||
|
||
class _Base(datetime.datetime): | ||
TYPE: str = "" | ||
SCHEMA: pydantic_core.core_schema.CoreSchema | ||
|
||
@classmethod | ||
def __get_pydantic_json_schema__( | ||
cls, core_schema: core_schema.CoreSchema, handler: GetJsonSchemaHandler | ||
) -> JsonSchemaValue: | ||
field_schema: dict[str, Any] = {} | ||
field_schema.update(type=cls.TYPE, format="date-time") | ||
return field_schema | ||
|
||
@classmethod | ||
def __get_pydantic_core_schema__( | ||
cls, source: type[Any], handler: Callable[[Any], CoreSchema] | ||
) -> core_schema.CoreSchema: | ||
return core_schema.with_info_after_validator_function( | ||
cls._validate, | ||
cls.SCHEMA, | ||
serialization=core_schema.wrap_serializer_function_ser_schema(cls._f, return_schema=cls.SCHEMA), | ||
) | ||
|
||
@classmethod | ||
def _validate(cls, __input_value: Any, _: Any) -> datetime.datetime: | ||
return EPOCH + datetime.timedelta(seconds=__input_value) | ||
|
||
@classmethod | ||
def _f(cls, value: Any, serializer: Callable[[Any], Any]) -> Any: # pragma: no cover | ||
raise NotImplementedError(cls) | ||
|
||
|
||
class Number(_Base): | ||
TYPE = "number" | ||
SCHEMA = core_schema.float_schema() | ||
|
||
@classmethod | ||
def _f(cls, value: Any, serializer: Callable[[float], float]) -> float: | ||
ts = value.timestamp() | ||
return serializer(ts) | ||
|
||
|
||
class Integer(_Base): | ||
TYPE = "integer" | ||
SCHEMA = core_schema.int_schema() | ||
|
||
@classmethod | ||
def _f(cls, value: Any, serializer: Callable[[int], int]) -> int: | ||
ts = value.timestamp() | ||
return serializer(int(ts)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
openapi: "3.1.0" | ||
info: | ||
version: 1.0.0 | ||
title: date-time | ||
|
||
components: | ||
schemas: | ||
Integer: | ||
type: integer | ||
format: date-time | ||
|
||
Number: | ||
type: number | ||
format: date-time | ||
|
||
String: | ||
type: string | ||
format: date-time |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters