Settings

Theme

Ask HN: Know any validation libraries that guess whether a timestamp is in ms/s?

2 points by ariebovenberg 2 years ago · 4 comments · 1 min read


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.)
scolvin 2 years ago

Yes, Pydantic does: https://docs.pydantic.dev/latest/api/standard_library_types/...

  • ariebovenbergOP 2 years ago

    Was this inspired by another library or language? Or simply a reaction to the all-too-common experience of dealing with under-specified data?

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection