juxt

2025-06-24T14:37:05.154259Z

Hey all, just dropping this here to see if anyone wants a crack at improving my somewhat hackish tick code 😅 This works, but feels like I'm missing something in the tick API that would make this read better. If anyone sees any opportunities for improvement here, by all means. Working across instants and periods and back to instants and doing math on them is melting my brain.

(:require [tick.core :as t]))

(defn timelord [i]
  (let [zoned-date-time (-> i
                            t/instant
                            (t/in "UTC"))
        formatter (fn [year month day] (format "%04d-%02d-%02dT00:00:00Z" year month day))

        basis-year (-> zoned-date-time t/year .getValue)
        basis-month (-> zoned-date-time t/month .getValue)

        prev-year (if (= basis-month 1) (dec basis-year) basis-year)
        prev-month (if (= basis-month 1) 12 (dec basis-month))

        next-year (if (= basis-month 12) (inc basis-year) basis-year)
        next-month (if (= basis-month 12) 1 (inc basis-month))

        ;; Last day of current month = 1 day before first of next month
        first-day-next-month (t/instant (formatter next-year next-month 01))

        first-day-basis-month (t/instant (formatter basis-year basis-month 01))
        last-day-basis-month (t/<< first-day-next-month (t/new-period 1 :days))

        first-day-previous-month (t/instant (format "%04d-%02d-01T00:00:00Z" prev-year prev-month))
        last-day-previous-month  (-> (t/<< first-day-basis-month (t/new-period 1 :days)))]

    {:first-day-basis-month first-day-basis-month
     :last-day-basis-month last-day-basis-month
     :first-day-previous-month first-day-previous-month
     :last-day-previous-month last-day-previous-month}))

;; usage
(timelord #inst "2025-02-28")

2025-06-24T15:07:54.511759Z

here you go

(ns play
  (:require [tick.core :as t]))


(defn xx [legacy-date]
  (let [date (-> legacy-date
                          t/instant
                          (t/in "UTC")
                            t/date)
        start-prev-month (-> date t/year-month t/dec (t/beginning) t/date)]
    {:first-day-basis-month (-> date t/first-day-of-month t/beginning (t/in "UTC") t/instant )
     :last-day-basis-month (-> date t/last-day-of-month t/beginning (t/in "UTC") t/instant )
     :first-day-previous-month (-> start-prev-month t/beginning (t/in "UTC") t/instant)
     :last-day-previous-month (-> start-prev-month t/last-day-of-month t/beginning (t/in "UTC") t/instant)
     }))

(comment
  (xx (java.util.Date.))
  

  )
biggest thing there IMO is to use year-month when 'moving' years or months, then add on day-of-month etc after

2025-06-24T15:13:17.646279Z

Ahh, so much better, thank you @henryw374 🙂 Really wonderful, readable API. Wish I had more occasion to use it so I could get more comfortable with these conventions.

👍 1