New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Value comparison does not work for Date.Date #468

Closed
lukewestby opened this Issue Dec 29, 2015 · 1 comment

Comments

Projects
None yet
2 participants
@lukewestby
Member

lukewestby commented Dec 29, 2015

Simplest reproducible example, doable in the REPL:

import Date
(Date.fromTime 1) == (Date.fromTime 2) -- => True

This is happening because instances of JS Date are "exotic" objects and not primitives. They have no own properties and end up falling through both the strict equality and object cases for Utils.eq(). They're a rather strange edge case as far as JS object comparison goes, as they need to be coerced to their Number representation first with + or valueOf() first.

The workaround for now that I've been using is to do the same sort of coercion, just in Elm:

import Date
date1 = Date.fromTime 1
date2 = Date.fromTime 2
(Date.toTime date1) == (Date.toTime date2) -- => False

But IMVHO it seems like if Date is going to be a built-in data type for the language, it should be possible to compare instances reliably without transformation.

The way I would plan to handle this is by adding a top-level case to eq to check x instanceof Date && y instanceof Date and then compare x.valueOf() === y.valueOf(). If that plan makes sense to you I can submit a PR 😄

Thanks!

@lukewestby lukewestby changed the title from Value comparison does not work for `Date.Date` to Value comparison does not work for Date.Date Dec 29, 2015

@tgecho tgecho referenced this issue Mar 12, 2016

Closed

Add Gigasecond #9

@athanclark

This comment has been minimized.

Show comment
Hide comment
@athanclark

athanclark May 3, 2016

I'm not sure if this is related, but I've needed to compare Dates and have been disappointed that they're not natively comparable. I had to build this verbose utility function for comparing months, may you scrape it for success!

compareMonth : Month -> Month -> Order
compareMonth m1 m2 =
  case (m1, m2) of
    (Jan, Jan) -> EQ
    (Jan, _)   -> LT
    (Feb, Jan) -> GT
    (Feb, Feb) -> EQ
    (Feb, _)   -> LT
    (Mar, Jan) -> GT
    (Mar, Feb) -> GT
    (Mar, Mar) -> EQ
    (Mar, _)   -> LT
    (Apr, Jan) -> GT
    (Apr, Feb) -> GT
    (Apr, Mar) -> GT
    (Apr, Apr) -> EQ
    (Apr, _)   -> LT
    (May, Jan) -> GT
    (May, Feb) -> GT
    (May, Mar) -> GT
    (May, Apr) -> GT
    (May, May) -> EQ
    (May, _)   -> LT
    (Jun, Jan) -> GT
    (Jun, Feb) -> GT
    (Jun, Mar) -> GT
    (Jun, Apr) -> GT
    (Jun, May) -> GT
    (Jun, Jun) -> EQ
    (Jun, _)   -> EQ
    (Jul, Dec) -> LT
    (Jul, Nov) -> LT
    (Jul, Oct) -> LT
    (Jul, Sep) -> LT
    (Jul, Aug) -> LT
    (Jul, Jul) -> EQ
    (Jul, _)   -> GT
    (Aug, Dec) -> LT
    (Aug, Nov) -> LT
    (Aug, Oct) -> LT
    (Aug, Sep) -> LT
    (Aug, Aug) -> EQ
    (Aug, _)   -> GT
    (Sep, Dec) -> LT
    (Sep, Nov) -> LT
    (Sep, Oct) -> LT
    (Sep, Sep) -> EQ
    (Sep, _)   -> GT
    (Oct, Dec) -> LT
    (Oct, Nov) -> LT
    (Oct, Oct) -> EQ
    (Oct, _)   -> GT
    (Nov, Dec) -> LT
    (Nov, Nov) -> EQ
    (Nov, _)   -> GT
    (Dec, Dec) -> EQ
    (Dec, _)   -> GT

athanclark commented May 3, 2016

I'm not sure if this is related, but I've needed to compare Dates and have been disappointed that they're not natively comparable. I had to build this verbose utility function for comparing months, may you scrape it for success!

compareMonth : Month -> Month -> Order
compareMonth m1 m2 =
  case (m1, m2) of
    (Jan, Jan) -> EQ
    (Jan, _)   -> LT
    (Feb, Jan) -> GT
    (Feb, Feb) -> EQ
    (Feb, _)   -> LT
    (Mar, Jan) -> GT
    (Mar, Feb) -> GT
    (Mar, Mar) -> EQ
    (Mar, _)   -> LT
    (Apr, Jan) -> GT
    (Apr, Feb) -> GT
    (Apr, Mar) -> GT
    (Apr, Apr) -> EQ
    (Apr, _)   -> LT
    (May, Jan) -> GT
    (May, Feb) -> GT
    (May, Mar) -> GT
    (May, Apr) -> GT
    (May, May) -> EQ
    (May, _)   -> LT
    (Jun, Jan) -> GT
    (Jun, Feb) -> GT
    (Jun, Mar) -> GT
    (Jun, Apr) -> GT
    (Jun, May) -> GT
    (Jun, Jun) -> EQ
    (Jun, _)   -> EQ
    (Jul, Dec) -> LT
    (Jul, Nov) -> LT
    (Jul, Oct) -> LT
    (Jul, Sep) -> LT
    (Jul, Aug) -> LT
    (Jul, Jul) -> EQ
    (Jul, _)   -> GT
    (Aug, Dec) -> LT
    (Aug, Nov) -> LT
    (Aug, Oct) -> LT
    (Aug, Sep) -> LT
    (Aug, Aug) -> EQ
    (Aug, _)   -> GT
    (Sep, Dec) -> LT
    (Sep, Nov) -> LT
    (Sep, Oct) -> LT
    (Sep, Sep) -> EQ
    (Sep, _)   -> GT
    (Oct, Dec) -> LT
    (Oct, Nov) -> LT
    (Oct, Oct) -> EQ
    (Oct, _)   -> GT
    (Nov, Dec) -> LT
    (Nov, Nov) -> EQ
    (Nov, _)   -> GT
    (Dec, Dec) -> EQ
    (Dec, _)   -> GT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment