Hi Mathias,
Perhaps this saves you some typing with field accessing:
(defn fields-map
"Returns a hash map with all field values of o"
[o] (apply hash-map (mapcat #(list (keyword (.getName %)) (.get
% o))
(.getFields (class o)))))
Use like this to compute the number of days until next friday:
(def now (fields-map (.getInstance java.util.GregorianCalendar)))
; if today is next friday on fridays ;-)
(rem (+ 7 (now :FRIDAY) (- (now :DAY_OF_WEEK))) 7)
; if not
(inc (rem (+ 6 (- (now :FRIDAY) (now :DAY_OF_WEAK))) 7))
Hope this helps.
Kind regards,
achim
On Sep 6, 12:05 pm, Mathias Dahl <[EMAIL PROTECTED]> wrote:
> I came up with this for my scheduling needs (I use it for getting the
> start time for a Timer):
>
> (import '(java.util GregorianCalendar))
>
> (defn get-next-friday-noon []
> (let [cal (new GregorianCalendar)
> day-of-week (. cal (get (. GregorianCalendar DAY_OF_WEEK)))
> friday (. GregorianCalendar FRIDAY)
> day-of-week-field (. GregorianCalendar DAY_OF_WEEK)
> hour-of-day-field (. GregorianCalendar HOUR_OF_DAY)
> minute-field (. GregorianCalendar MINUTE)
> second-field (. GregorianCalendar SECOND)
> diff (- friday day-of-week)]
> (cond (> diff 0) ;; Friday is later this week
> (. cal (add day-of-week-field diff))
> (< diff 0) ;; Friday was earlier this week
> (. cal (add day-of-week-field (+ 7 diff)))
> (= diff 0) ;; Today is Friday
> (if (> (. cal (get hour-of-day-field)) 12)
> (. cal (add day-of-week-field 7))))
> ;; Always set time to 12:00:00 before it is returned
> (. (doto cal
> (set hour-of-day-field 12)
> (set minute-field 00)
> (set second-field 00))
> (getTime))))
>
> It returns the time of next Friday at noon. If today is Friday, it
> might return the time of noon today if it is before noon. If it is
> after noon then it will return next Friday noon.
>
> However, it seems very complicated, does anyone have some clever idea
> on how to optimize it?
>
> /Mathias
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---