time icon indicating copy to clipboard operation
time copied to clipboard

Time.Posix has insufficient range

Open ksvanhorn opened this issue 4 years ago • 4 comments

Time.Posix is defined as

type Posix = Posix Int

where the integer field is in milliseconds since Jan. 1, 1970. However, the language documentation for Int states that

"Int math is well-defined in the range -2^31 to 2^31 - 1. Outside of that range, the behavior is determined by the compilation target."

This means that Posix time is only guaranteed to be able to represent times between 07 Dec 1969 03:28:37 UTC and 25 Jan 1970 20:31:22 UTC.

ksvanhorn avatar May 28 '20 19:05 ksvanhorn

The documentation then says:

When generating JavaScript, the safe range expands to -2^53 to 2^53 - 1 for some operations

As Javascript is the only target for now, and the operations used for Posix.Time are safe in this range, time should be safe from Sunday, September 26, -0885 2:04:12.591 AM to Sunday, April 7, 4824 9:55:47.409 PM.

But you're right that some care must be taken if a new target like WebAssembly is one day supported.

rlefevre avatar May 28 '20 20:05 rlefevre

"some operations"?

ksvanhorn avatar May 28 '20 20:05 ksvanhorn

Operations that only work in 32bit are bitwise operations: https://package.elm-lang.org/packages/elm/core/latest/Bitwise

Plus a few others that use them indirectly.

rlefevre avatar May 28 '20 21:05 rlefevre

"some operations"

That is not documented anywhere - Elm is notoriously awfully documented. For example integer division will only work for 32-bit values so if you try to calculate average of two timestamps that will not work.

Also since "some operations" is not defined, list of those operations can change at any time. So e.g. addition could suddenly stop working.

malaire avatar Oct 26 '20 11:10 malaire