1
Ask HN: Know any validation libraries that guess whether a timestamp is in ms/s?
When parsing timestamps, Pydantic (a wildly popular validation library for Python)
will infer for you whether it's in seconds or milliseconds.
I've never encountered behavior like this, and I wondered:
Do you know of any other libraries, languages, or APIs that also do this type of inference?
The reason is that it can result in surprising behavior for timestamps in 1970 and 1969:
from pydantic import BaseModel
class Foo(BaseModel):
a: datetime
# some timestamps in ms
earlier_in_2023 = datetime(2023, 10, 4, 12, 22).timestamp() * 1000
apollo17_launch = datetime(1972, 12, 7, 5, 33).timestamp() * 1000
apollo13_launch = datetime(1970, 4, 11, 19, 13).timestamp() * 1000
print(Foo(a=earlier_in_2023)) # 2023-10-04 (expected)
print(Foo(a=apollo17_launch)) # 1972-12-07 (expected)
print(Foo(a=apollo13_launch)) # 2245-11-14 (unexpectedly inferred as seconds)
(What's happening here is that timestamps close to 1970 are smaller,
and it infers that you must have meant to use seconds.)