Repository: pydantic/pydantic

Issue:
JavaScript negative timestamp are not supported
# 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 }`).

```py
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.


Comments:
Happy to accept a PR to fix this.

I guess we should:
* raise the correct error resulting in a validation error if the datetime is out of bounds
* accept negative timestamps if they result in valid datetimes - there's nothing implicitly wrong with the unix timestamp of `-1`. Stuff happened before 1970 apparently.
* comparing with `MS_WATERSHED` as an absolute number makes sense I suppose, we would just need to update the docs to be explicit.
I don't get this :

>     * raise the correct error resulting in a validation error if the datetime is out of bounds

For the rest, everything is running fine and ready for PR


I think it might be fine, I just meant:

make sure passing timestamps like `1_000_000_000_000_000` or `-100_000_000_000_000` cause sensible validation errors not other exceptions.
Yes, it runs fine with these timestamps. The generated `datetime` does not match with the real date as these timestamps are greater than `MS_WATERSHED` but it is parsed without any exception.

Patch:
diff --git a/pydantic/datetime_parse.py b/pydantic/datetime_parse.py
--- a/pydantic/datetime_parse.py
+++ b/pydantic/datetime_parse.py
@@ -75,7 +75,7 @@ def get_numeric(value: StrBytesIntFloat, native_expected_type: str) -> Union[Non
 
 
 def from_unix_seconds(seconds: Union[int, float]) -> datetime:
-    while seconds > MS_WATERSHED:
+    while abs(seconds) > MS_WATERSHED:
         seconds /= 1000
     dt = EPOCH + timedelta(seconds=seconds)
     return dt.replace(tzinfo=timezone.utc)


Test patch:
diff --git a/tests/test_datetime_parse.py b/tests/test_datetime_parse.py
--- a/tests/test_datetime_parse.py
+++ b/tests/test_datetime_parse.py
@@ -93,6 +93,7 @@ def test_time_parsing(value, result):
         (1_494_012_444, datetime(2017, 5, 5, 19, 27, 24, tzinfo=timezone.utc)),
         # values in ms
         ('1494012444000.883309', datetime(2017, 5, 5, 19, 27, 24, 883, tzinfo=timezone.utc)),
+        ('-1494012444000.883309', datetime(1922, 8, 29, 4, 32, 35, 999117, tzinfo=timezone.utc)),
         (1_494_012_444_000, datetime(2017, 5, 5, 19, 27, 24, tzinfo=timezone.utc)),
         ('2012-04-23T09:15:00', datetime(2012, 4, 23, 9, 15)),
         ('2012-4-9 4:8:16', datetime(2012, 4, 9, 4, 8, 16)),