gcanti / io-ts-types

A collection of codecs and combinators for use with io-ts
https://gcanti.github.io/io-ts-types/
MIT License
311 stars 40 forks source link

DateFromUnixTime encode -> decode fails #159

Closed saevarb closed 3 years ago

saevarb commented 3 years ago

🐛 Bug report

Current Behavior

DateFromUnixTime.encode returns a floating point number which its decoder refuses to accept.

Expected behavior

DateFromUnixTime.encode returns an integer so that decode(encode(x)) = x.

Reproducible example

const res = tt.DateFromUnixTime.encode(new Date());
console.log(res); // shows floating point number
console.log(tt.DateFromUnixTime.decode(res)); // returns Left

Your environment

   "fp-ts": "2.9.5",
    "io-ts": "2.2.16",
    "io-ts-types": "0.5.15",
    "monocle-ts": "2.3.9",
    "newtype-ts": "0.3.4",
saevarb commented 3 years ago

I just realized I managed to post this in the wrong repository. This is particularly stupid of me because I realized I was about to do this half way through writing the issue, opened up the io-ts-types repository instead, copied the bug report over but somehow still finished it in this tab.

Should I close this and post this there or is there something a maintainer can do that is easier?

gcanti commented 3 years ago

I think we should wrap the encoded value in Math.floor:

-a => a.getTime() / 1000
+a => Math.floor(a.getTime() / 1000)