This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-05-12
Channels
- # announcements (4)
- # asami (3)
- # babashka (18)
- # babashka-sci-dev (2)
- # beginners (55)
- # calva (18)
- # clj-commons (23)
- # clj-kondo (2)
- # cljfx (7)
- # cljs-dev (15)
- # clojure (96)
- # clojure-europe (43)
- # clojure-losangeles (3)
- # clojure-nl (2)
- # clojure-uk (11)
- # clojurescript (1)
- # datalevin (2)
- # datomic (10)
- # events (1)
- # google-cloud (4)
- # gratitude (16)
- # helix (3)
- # hyperfiddle (63)
- # inf-clojure (13)
- # introduce-yourself (4)
- # ipfs (2)
- # jobs (2)
- # jobs-discuss (12)
- # leiningen (7)
- # lsp (15)
- # off-topic (38)
- # polylith (24)
- # portal (27)
- # remote-jobs (8)
- # sci (27)
- # spacemacs (5)
- # specter (1)
- # sql (5)
- # xtdb (41)
GOOOD MORNING
Godmorgen 😎
With all these wonderful pictures I want to come and live on your village 😊
Morning!
Today I used a brute force reduce
to calculate the date that is N days before today (or another date) ... just skipped over weekends. I wanted to avoid a library for something seemingly simple. What do you folks think? Crazy - just use a lib or nah, better to code it up?
FWIW I would probably use tick as I like the way that it works / is designed:
(t/<< (t/now)
(t/new-duration 10 :days))
(defn from-date
[{:keys [year month day]} n-working-days]
(let [js-date (js/Date. year month day)]
(reduce (fn [{:keys [working-days] :as from} dec-by]
(if (>= working-days n-working-days)
(reduced from)
(let [_ (.setDate js-date (- (.getDate js-date) dec-by))
working-days' (condp = (.getDay js-date)
0 working-days ; Skip Sunday
6 working-days ; Skip Saturday
(inc working-days))]
{:working-days working-days'
:from-date js-date})))
{:working-days 0
:from-date js-date} (repeat 1))))
why the scream @U054UD60U? Is the code really that bad?
Not at all. But that's quite a challenge to compute. Isn't it?
When not handling the you should get away with a winter days diff * 5/7 adjusted for the weekday of one edge of the interval.
If it's fine with not handling a lot of corner cases, I'd say it's fine to roll your own. But having worked with an app where time was of utter importance (a med reminder app), I know how hairy time stuff is and would use a library I trust if just some corner cases need be address.
@U04V70XH6 - it does make sense in the paradigm of time and date that Tick provides,
t/<<
is "rewind time" &
t/>>
is "fast-forward time"it's admittedly only an obvious visual cue to the stereo / tape generation, but I figure you remember tape... 😉
@U04V5V0V4 - If you need working days from Tick you just need to add the appropriate number of weekends to the number of days that you pipe into the subtract function above and there are loads of ways to peel that onion, including ones that could also potentially include national holidays, though you would probably have to implement another library that provides cultural localisation and make some helper functions to see if you needed to add any extra non-weekend non-working days into the Tick maths... 😉
Getting the right number of weekends was giving me a headache hence the dumb implementation
But I agree with @U0ETXRFEW that a library would be better in complex cases.
Incidentally it take 200ms to go back 10k days so yeah, quite slow but our case is max 45 which takes ~5ms so I'm not going to sweat
@U04V5V0V4 not answering our question at all but it made me think. After decades in the industry I got into the habit of reaching for a library. I kind of became a library integrator. Clojure is teaching me many things, but I really like the realization that it is not that hard, and often much much simpler, to code up your own thing in Clojure instead of reaching for a library.