andrewmcveigh / cljs-time

A clj-time inspired date library for clojurescript.
342 stars 57 forks source link

no date hashing? #19

Closed postspectacular closed 9 years ago

postspectacular commented 9 years ago

Hashing of dates seems to be broken/non-existent, probably due to use of Google JS types. The following should produce a map of 3 pairs (12h steps, grouped by day), but instead does no grouping at all:

(->> (now)
     (to-long)
     (iterate #(+ % (* 12 60 60 1000)))
     (interleave '[a b c d e f])
     (partition 2)
     (group-by
       (fn [[_ t]]
        (let [d (from-long t)]
          (date-midnight (year d) (month d) (day d))))))

{#<20150106T000000> [(a 1420512947185)],
 #<20150106T000000> [(b 1420556147185)],
 #<20150107T000000> [(c 1420599347185)],
 #<20150107T000000> [(d 1420642547185)],
 #<20150108T000000> [(e 1420685747185)],
 #<20150108T000000> [(f 1420728947185)]}

The same works in Clojure w/ clj-time:

{#<DateMidnight 2015-01-06T00:00:00.000Z> [(a 1420513336586) (b 1420556536586)],
 #<DateMidnight 2015-01-07T00:00:00.000Z> [(c 1420599736586) (d 1420642936586)],
 #<DateMidnight 2015-01-08T00:00:00.000Z> [(e 1420686136586) (f 1420729336586)]}

A simple fix might be to add an IHash protocol impl based on epochs...

andrewmcveigh commented 9 years ago

Hi,

Thanks for reporting this. I guess implementing IHash and IEquiv for all the goog.date types should fix this. I'll have it in the next release.

swannodette commented 9 years ago

I highly recommend putting the hashing/equality implementations in a separate namespace that user can optionally load. I can easily imagine a case where people have already implemented these for Google Closure data structures.

andrewmcveigh commented 9 years ago

Good point, thanks. I'll fix that.

postspectacular commented 9 years ago

@andrewmcveigh thanks for this & the new release!