I have a type that's an extended date-time. It can be constructed with the 
usual year, month, day parameters but (for legacy reasons) the month can be 
overloaded to mean extra things. E.g. a month of 21 means 'spring', 33 
means 'third quarter'. I want to construct this type in a way that it 
preserves that information (so, for example, it can print itself 
appropriately), but that has comparison and equality with other dates.

I want to extend clj-time somehow. My first thought is write a type that 
construct with the given arguments and implements the 
clj-time/DateTimeProtocol, converting whenever required (I don't need high 
performance).

(defprotocol IWeirdDate
  (as-date [this])
  (pp [this]))

(defrecord WeirdDate [year month day]
  IWeirdDate
  (pp [this] (condp = month
               21 (str "Spring " year)
               33 (str "Third Quarter of " year)
               (str (as-date this))))
  
  (as-date [this]
           (let [[y m d] (condp = month
                            21 [year 3 day]
                            33 [year 7 1] ; third quarter

                            ; ... others
                            
                            ; default
                            [year month day])]
                (clj-time/date-time y m d)))
  
  clj-time/DateTimeProtocol
  (clj-time/year [this] (clj-time/year (as-date this)))
  (clj-time/month [this] (clj-time/month (as-date this)))
  (clj-time/day [this] (clj-time/day (as-date this)))
  ; ... and the rest of DateTimeProtocol
)

Is this the right way to do this? Could I somehow make this implicit and 
avoid re-writing the DateTimeProtocol implementations?

Cheers

Joe

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to