Fork me on GitHub
#babashka
<
2020-05-21
>
bherrmann01:05:22

I have a directory of files. I want to only see ones modified today... Is there a simple way to do this?

bherrmann01:05:52

The old school approach is to find midnight by truncating the time with Calendar, but

user=> (java.util.Calendar/getInstance)
clojure.lang.ExceptionInfo: Could not resolve symbol: java.util.Calendar/getInstance [at line 2, column 2]

bherrmann01:05:57

A super cheezy way would be to get the time, format it w/o the time, and parse it back... but

bherrmann01:05:15

(java.text.SimpleDateFormat. "YYY")
clojure.lang.ExceptionInfo: Unable to resolve classname: java.text.SimpleDateFormat [at line 1, column 2]

bherrmann01:05:52

I guess this,

bherrmann01:05:55

(def midnight (* 1000 (.getEpochSecond (.truncatedTo (java.time.Instant/now) java.time.temporal.ChronoUnit/DAYS))))
(filter #(> (.lastModified %) midnight) (.listFiles (.File. "/tmp/myfiles")))

mbjarland07:05:12

How about:

(LocalDateTime/of (LocalDate/now) LocalTime/MIDNIGHT)
=> #object[java.time.LocalDateTime 0x39d8d143 "2020-05-21T00:00"]

borkdude08:05:10

@bherrmann Another variation, more verbose 😉

(require '[ :as io])

(defn day [millis]
  (let [instant (java.time.Instant/ofEpochMilli millis)
        zoned (.atZone instant (java.time.ZoneId/systemDefault))
        local-date (.toLocalDate zoned)
        year (.getYear local-date)
        day-of-year (.getDayOfYear local-date)]
    [year day-of-year]))

(prn (day (System/currentTimeMillis)))      ;;=> [2020 142]
(day (.lastModified (io/file "README.md"))) ;;=> [2020 141]

bherrmann11:05:16

How odd.

$ clj -r
Clojure 1.10.1
user=> (java.time.ZoneId/systemDefault)
#object[java.time.ZoneRegion 0x315df4bb "America/New_York"]

$ bb
Babashka v0.0.94 REPL.
user=> (java.time.ZoneId/systemDefault)
#object[java.time.ZoneRegion 0x1698137b "Etc/UTC"]

borkdude12:05:33

I think there's an issue about this on the GraalVM repo.

borkdude12:05:28

weirdly enough:

$ bb -e '(java.time.ZoneId/systemDefault)'
#object[java.time.ZoneRegion 0x15b47051 "America/Los_Angeles"]

borkdude12:05:39

while:

$ clj -e '(java.time.ZoneId/systemDefault)'
#object[java.time.ZoneRegion 0x6cdba6dc "Europe/Amsterdam"]

borkdude12:05:28

@bherrmann I asked a question about this in the GraalVM slack: https://graalvm.slack.com/archives/CN9KSFB40/p1590063790414600

borkdude12:05:02

Yes, it appears so!

$ ./bb -e '(java.time.ZoneId/systemDefault)'
#object[java.time.ZoneRegion 0x282638a0 "America/Sao_Paulo"]
$ clj -e '(java.time.ZoneId/systemDefault)'
#object[java.time.ZoneRegion 0x6cdba6dc "America/Sao_Paulo"]

bherrmann12:05:56

Thanks! Thatts great.

thomas.ormezzano16:05:07

is there a way to use babashka as websockets client ? I couldn’t tell if it’s the case or not (I really love babashka btw 😍)

borkdude16:05:49

@thomas.ormezzano There is no built-in websocket support, as far as I know (unless there is something in Java which supports this?) But you could shell out to something like websocat maybe

thomas.ormezzano16:05:23

ok cool thank you 👍

borkdude16:05:40

if you feel inspired, you can build a websocket pod for babashka: https://github.com/babashka/babashka.pods

mkvlr20:05:10

hato supports websockets, wasn’t there pod for it? https://github.com/gnarroway/hato

borkdude20:05:44

I did some experiments around including it as the default http client, but that didn't work out. It was technically possible but it slowed down compilation by 8 minutes and bloated the binary considerably. However, maybe it's about time bb moved to java11 so we can just include .http.WebSocket and people can build a lib around that

borkdude20:05:33

hato pod is also an option. pods are a new thing

thomas.ormezzano08:05:14

ah ok I actually don’t need it anymore but didn’t know about pods - going to keep that in mind for next time 👍

borkdude17:05:35

This is a very cool workflow for doing Clojure exercises with babashka: https://github.com/samedhi/ctci