clj-time / clj-time

A date and time library for Clojure, wrapping the Joda Time library.
Other
739 stars 161 forks source link

Inconsistent argument order when defining formatter with one/multiple formats #270

Closed Nuo-L closed 5 years ago

Nuo-L commented 5 years ago

When the first time a user tried to use a formatter with customized timezone, he/she usually will follow the demos in the readme. The example in the readme is the one for multiple formats, which places the timezone in the first, but for single format case, the argument order is different!

(defn ^org.joda.time.format.DateTimeFormatter formatter
  "Returns a custom formatter for the given date-time pattern or keyword."
  ([fmts]
     (formatter fmts utc))
  ([fmts ^DateTimeZone dtz]
   (cond (keyword? fmts) (.withZone ^DateTimeFormatter (get formatters fmts) dtz)
         (string?  fmts) (.withZone (DateTimeFormat/forPattern fmts) dtz)
         :else           (.withZone ^DateTimeFormatter fmts dtz)))
  ([^DateTimeZone dtz fmts & more]
    (let [printer (.getPrinter ^DateTimeFormatter (formatter fmts dtz))
          parsers (map #(.getParser ^DateTimeFormatter (formatter % dtz)) (cons fmts more))]
      (-> (DateTimeFormatterBuilder.)
        ^DateTimeFormatterBuilder (.append ^DateTimePrinter printer
                                            ^"[Lorg.joda.time.format.DateTimeParser;"
                                            (into-array DateTimeParser parsers))
        (.toFormatter)
        (.withZone dtz)))))

It is quite unusual to have inconsistent argument order for different numbers of argument case.

seancorfield commented 5 years ago

Happy to take a PR to improve the readme but this library is essentially deprecated and everyone should be migrating to Java Time (or one of the Clojure wrappers for it).