Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix java.time's brittleness #108

Open
ohbadiah opened this issue Mar 6, 2024 · 0 comments
Open

Fix java.time's brittleness #108

ohbadiah opened this issue Mar 6, 2024 · 0 comments

Comments

@ohbadiah
Copy link

ohbadiah commented Mar 6, 2024

Hi, I'm filing this tongue-in-cheek, but I hope it's edifying.

I did a coding test for a job today. I used clj-time. I got the right answers. Later on I was embarrassed I had used a deprecated library, and so I decided to upgrade to current best practices.

My issue boils down to: in the java time api, you can't parse a LocalDate from a formatter like "yyyy". For example:

(jt/local-date "yyyy" "2024")

will get you

java.time.DateTimeException                                                                                                                                                                                 
   Unable to obtain LocalDate from TemporalAccessor: {Year=2024},ISO of type java.time.format.Parsed 

This leads to code like this:

(defn parse-date [date-str]
  (condp = (count date-str)
    4 (-> (java.time.Year/parse date-str yyyy)
          (.atMonth 1)
          (.atDay 1))
    6 (-> (java.time.YearMonth/parse date-str yyyyMM)
          (.atDay 1))
    8 (jt/local-date "yyyyMMdd" date-str)))

instead of this clj-time code:

(def parse-date
  (partial clj-time.format/parse
           (clj-time.format/formatter clj-time.core/utc
                                      "yyyy"
                                      "yyyyMM"
                                      "yyyyMMdd")))

The problem here seems somewhat philosophical to me. Here the library is just faithfully wrapping java.time, but due to the brittle type hierarchy, that results in code which really smells like java, somewhat defeating the purpose. Maybe there's nothing that can be done here? But in the off chance anyone has thoughts about a layer that could be added to make a more clojure-like API, I'd be interested to hear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant