8000 JavaScript negative timestamp are not supported · Issue #1600 · pydantic/pydantic · GitHub
[go: up one dir, main page]

Skip to content

JavaScript negative timestamp are not supported #1600

@mlbiche

Description

@mlbiche

Bug

When parsing a JavaScript positive timestamp (number of milliseconds since EPOCH), pydantic parses it properly. However, for a negative JavaScript timestamp (before 1st January 1970), a date value out of range error is thrown.

  File "pydantic/main.py", line 447, in pydantic.main.BaseModel.parse_obj
  File "pydantic/main.py", line 336, in pydantic.main.BaseModel.__init__
  File "pydantic/main.py", line 887, in pydantic.main.validate_model
  File "pydantic/fields.py", line 549, in pydantic.fields.ModelField.validate
  File "pydantic/fields.py", line 704, in pydantic.fields.ModelField._validate_singleton
  File "pydantic/fields.py", line 711, in pydantic.fields.ModelField._apply_validators
  File "pydantic/class_validators.py", line 313, in pydantic.class_validators._generic_validator_basic.lambda11
  File "pydantic/datetime_parse.py", line 169, in pydantic.datetime_parse.parse_datetime
  File "pydantic/datetime_parse.py", line 80, in pydantic.datetime_parse.from_unix_seconds
OverflowError: date value out of range

Version

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

             pydantic version: 1.5.1
            pydantic compiled: True
                 install path: /Users/thibaud/Documents/Programmation/Scopyleft/Terrapeutes/aposto-server/venv/lib/python3.7/site-packages/pydantic
               python version: 3.7.3 (default, Mar  6 2020, 22:34:30)  [Clang 11.0.3 (clang-1103.0.32.29)]
                     platform: Darwin-19.5.0-x86_64-i386-64bit
     optional deps. installed: ['typing-extensions', 'email-validator']

Code example

The following code raises the describe error (while it would correctly work with { "jsTimestamp": 1118102400000 }).

from datetime import datetime

from pydantic import BaseModel


class MyModel(BaseModel):
    jsTimestamp: datetime

MyModel.parse_obj({ "jsTimestamp": -1118102400000 })

Possible solution

A solution would be to update the from_unix_seconds function in datetime_parse.py to compare seconds with MS_WATERSHED as absolute value.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V1Bug related to Pydantic V1.X

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0