A Clojure(Script) library which mirrors the java.time api through kebab-case-named vars.
The java.time api is augmented with:
(cljc.java-time.extn.predicates/instant? x)
This library uses a pure-JS implementation of java.time, see here for discussion on whether that affects application performance or build size
See my talk at Clojure/North 2019 for more background.
This library mostly works on babashka. Only 'mostly' because babashka doesn't include all classes from java.time. TemporalQuery for example. If a namespace that uses TemporalQuery is required, an error is thrown.
Temporal, the new JS platform Date-Time lib has been considered for use as an alternative basis of this library instead of js-joda, but although it has some overlap with java.time, Temporal is different enough that implementing cljc.java-time would be very difficult. For a dependency-free Clojure(Script) date-time API, see Tempo
tick is a higher level date-time library that uses this one. Even if you're using cljc.java-time directly, ie not through tick, it has very relevant docs on extra setup for use with ClojureScript
time-literals is a Clojure(Script) library which provides tagged literals for objects from jsr-310 domain
This library sits atop java.time
on the jvm and cljs.java-time on Javascript
platforms. Writing cross-platform code that uses those libraries directly is harder than interop normally is because:
Reasons to use this:
comp
, apply
, juxt
and all other clojure.core fns that apply to fns can now be used without anon fns: #(.foo %)
#(.foo %)
in someone's code and not knowing what foo is, you'll have a properly namespaced clojure function foo
- much better!(local-date? x)
For every class in java.time, there is a clojure namespace.
For example, corresponding to java.time.LocalDate
, there is a namespace
cljc.java-time.local-date
In that and every other namespace, there is one var per public method/field in the corresponding class.
For example, for the method java.time.LocalDate/parse
, there is a corresponding function cljc.java-time.local-date/parse
Instance methods take the instance as the extra first arg
Get it from Clojars
Version 0.1.9 and up require minimum Clojurescript version of 1.10.741
In .cljc file
(ns my.cljc
(:require [cljc.java-time.local-date :as ld])
;create a date
(def a-date (ld/parse "2019-01-01"))
;add some days
(ld/plus-days a-date 99)
Roundtripping with with legacy Date
(require '[cljc.java-time.instant :as i])
;cljs
(-> (js/Date.) (.getTime) (i/of-epoch-milli) (i/to-epoch-milli) (js/Date.))
;clj
(-> (Date.) (.getTime) (i/of-epoch-milli) (i/to-epoch-milli) (Date.))
java.time.Year#isLeap exists as an instance method and a static method. Only the static version has been wrapped.
The code of this project consists of mechanically generated functions for the java.time methods, even if those methods are
inherited via superclasses or interfaces. In this project, functions are generated in every class they can be applied to. For example there is
cljc.java-time.temporal.temporal/is-supported
and also cljc.java-time.local-date/is-supported
, with the latter being
essentially unnecessary but included anyway.
A couple of new methods were added to java.time
in Java 9 - these are not included in this library because according
to the Clojure Survey about half
of Clojure users are still on Java 8 :(
Be aware that the underlying js implementation is probably only 99% complete. Anything you find missing can be added via pull request to js-joda
The code of the project is generated by the generate-library-code!
function in the
dev/gen.clj namespace.
To run all tests:
make test
Start a repl:
clj -Adev:test:test-cljs
For Clojurescript dev, see the dev/cljs.clj file for functions to start test build in shadow and run tests.
Start babashka as follows
deps -A:test -Scommand "bb -cp {{classpath}}" --socket-repl 1666'
and run tests from the repl
one cmd to do this would be preferable
Copyright © 2021 Widd Industries
Distributed under the MIT License
Can you improve this documentation? These fine people already did:
Henry Widd, Tommi Reiman & Alan ThompsonEdit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close