unisonweb / base

Unison base libraries

Home Page:https://share.unison-lang.org/@unison/base

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Instant.toText is treating nanoseconds incorrectly when transforming to fractions of a second

alvaroc1 opened this issue · comments

Both effectively produce the same iso8601 value of 0.1 seconds after epoch:

13 | > Instant.toText (fromEpochNanoseconds +100000000)
       ⧩
       "1970-01-01T00:00:00.100000000Z"

15 | > Instant.toText (fromEpochNanoseconds +1)
       ⧩
       "1970-01-01T00:00:00.1Z"

(and breaking lexicographical order in the process)

ah the culprit:

  lib.base.time.LocalTime.toText : LocalTime -> Text
  lib.base.time.LocalTime.toText = cases
    LocalTime hour minute second nanosecond ->
      Text.join
        ""
        [ "T"
        , Text.leftPad 2 "0" (Nat.toText hour)
        , ":"
        , Text.leftPad 2 "0" (Nat.toText minute)
        , ":"
        , Text.leftPad 2 "0" (Nat.toText second)
        ]
        Text.++ (if nanosecond Nat.== 0 then "" else "." Text.++ Nat.toText nanosecond)

fractional seconds need to be padded