Fork me on GitHub
#biff
<
2023-01-28
>
Epidiah Ravachol17:01:55

Those Tailwind Space Between utilities play really well with biff/formhttps://tailwindcss.com/docs/space — because they ignore those pesky hidden elements that can otherwise mess up your best laid CSS plans.

😎 6
👾 4
4
macrobartfast20:01:15

I’ve probably asked this before or elsewhere, so forgive me… I’m just at one of those places where I need a little boost along motivationally. I need to query for documents on the server since epoch to display events in a user’s current day. I have it working on my local machine using the following no doubt very improvable query in app.clj:

(defn get-events-since-epoch [email event-type]
  (let [{:keys [biff/db] :as sys} (get-sys)
        now (biff/now)
        epoch (biff/crop-date (biff/now) "yyyy")
        events (q db
                  '{:find (pull event [*])
                    :where [[event :event/user email]
                            [event :event/type event-type]
                            [event :event/event-time event-time]
                            [(>= event-time epoch)]]
                    :in [email event-type epoch]}
                  email event-type (biff/crop-date (biff/now) "yyyy-MM-dd"))]
    (reverse (sort-by first events))))
First off, any tips on that code is always welcome. I think it has been suggested to not use get-sys, for starters. That said, this does not work on the server given its different temporal frame of reference. Attempting to make this work on the server has surfaced a bewildering array of libraries for contending with an equally bewildering situation in Java. It’s possible the solution is very simple. At any rate, it seems like I have to store a user’s time zone on the server as it isn’t gatherable from the front end, AFAIK. I suppose that’s one of the pluses of mobile apps… tight integration with automatically updating time when, say, a user gets off a plane into a new time zone. Thanks! PS - I’ve probably failed to say what I need… I just want to have a user see all events since their midnight. It’s not a big deal right now but I’m aware daylight savings can be an issue with this. I will also want a user to be able to see events from other days eventually. Another thing: I store :event/event-time when I make the document… I’m not leveraging the temporal aspects of xtdb and maybe I should.

Jacob O'Bryant21:01:09

ah yes, definitely refrain from using get-sys 🙂. You should be able to pass in the system map (or "context map" as I've started calling it, and to which I will update the biff docs/code accordingly at some point) or db from whatever function is calling this one. (And if that function isn't currently being passed the context map, you can repeat until you reach a function that does have it.) > Another thing: I store :event/event-time when I make the document… I’m not leveraging the temporal aspects of xtdb and maybe I should. eh, I wouldn't worry about it. If you're using a time value in your application code, imo it's usually better to put it on the document explicitly anyway. anyway, on to your actual main question: to get the user's timezone, you can run some javascript on the frontend and pass that back to the server. i.e. use Intl.DateTimeFormat().resolvedOptions().timeZone to populate a hidden input field or something. that'll give you a value like "America/Los_Angeles". Once you have that on the backend, you can pass it to this function:

(defn midnight-for-timezone [timezone-text]
  (let [tz (java.util.TimeZone/getTimeZone timezone-text)
        fmt (doto (java.text.SimpleDateFormat. "yyyy-MM-dd")
              (.setTimeZone tz))
        now (java.util.Date.)]
    (.parse fmt (.format fmt now))))

(midnight-for-timezone "America/Los_Angeles")
=> #inst "2023-01-28T08:00:00.000-00:00"
It is highly likely there's a better way to do this, e.g. using java.time stuff instead of the old deprecated java.util.Date stuff. But this works.

Jacob O'Bryant21:01:41

(As it happens I needed to do something similar recently, so I already had code like this laying around)

macrobartfast21:01:17

Wow, wonderful. Thank you!

macrobartfast21:01:19

Good enough for me. One day, when the operation scales into the 1B+ range, someone will stop me in the hallway… “uhh… that midnight-to-timezone function…”

macrobartfast21:01:45

Yes. Yes. get-sys. My bad habit. I’ll grep for it today and send it packing. Maybe with another query here about how to work around it in certain spots.