qntm / t-a-i

Converts Unix milliseconds to and from International Atomic Time (TAI) milliseconds
MIT License
43 stars 2 forks source link

High Precision TAI Timestamp #61

Open dukesook opened 2 months ago

dukesook commented 2 months ago

Hello,

Does this repository support TAI timestamps with greater precision than milliseconds? Like microseconds or even nanoseconds?

qntm commented 2 months ago

Right now this library only accepts integer millisecond counts as input and throws an exception otherwise. It also returns results truncated to the millisecond. The reason for this is that this is how JavaScript time is expressed.

However, there is some semi-secret internal infrastructure which carries out all the relevant calculations using precise ratios of BigInts. We only truncate at the very end, everything internal is exact. It could be possible to expose this interface, or some kind of microsecond converter or nanosecond converter or similar.

What are your actual requirements? Or are you just curious?

dukesook commented 2 months ago

MPEG is adding a TAI nanosecond timestamp capability for their HEIF & MP4 files. I'm wondering if there's any open-source tool to convert such a timestamp to/from a human readable form. However, It seems like everyone uses UTC with millisecond precision.

I would be interested in such an interface that handles an arbitrary level of precision.

qntm commented 2 months ago

I see. Well, some of the pieces for that are in place, but I wouldn't exactly call them suitable for public consumption, there's a decent amount of jank. Let me think about the best way to provide that interface.

qntm commented 4 weeks ago

@dukesook I should probably mention that you may be able to get away with:

const OFFSET_MILLIS = 37_000
const unixToAtomic = x => x + OFFSET_MILLIS
const atomicToUnix = x => x - OFFSET_MILLIS

Similarly for microseconds, nanoseconds or whatever. This implementation is sufficient for your needs provided that

  1. you don't need to handle any dates prior to 2017, and
  2. the abolition of leap seconds, currently expected to occur in or before 2035, is completed before any more leap seconds are announced, which on current showing seems quite possible.