News for hackers. Stuff that matters.

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.)
( 4 comments | on HN )
Re:Ask HN: Know any validation libraries that guess whether ... by scolvin on Saturday October 28, 2023 @03:42AM (#38047966)
Re:Ask HN: Know any validation libraries that guess whether ... by ariebovenberg on Saturday October 28, 2023 @05:58AM (#38048514)
Was this inspired by another library or language? Or simply a reaction to the all-too-common experience of dealing with under-specified data?
Re:Ask HN: Know any validation libraries that guess whether ... by scolvin on Saturday October 28, 2023 @02:55PM (#38052398)
As far as I can remember it just seemed sensible to me at the time.
Re:Ask HN: Know any validation libraries that guess whether ... by jval43 on Saturday October 28, 2023 @04:10AM (#38048103)
Wow that is a recipe for disaster. That feature alone would lead me to recommend staying away from the whole library.

I'd argue one needs to be especially pedantic here, not lax and "helpful". There are already so many edge cases working with dates and times, implementing something like this is plain irresponsible.

EDIT: Apparently Pydantic didn't even support timezones until recently. That signals to me a blatant disregard for one of the core issues with times and dates from the authors. Not sure how this library could get so popular.