https://github.com/juxt/tick
Time as a value.
https://github.com/juxt/tick
Last synced: 3 months ago
JSON representation
Time as a value.
- Host: GitHub
- URL: https://github.com/juxt/tick
- Owner: juxt
- License: mit
- Created: 2016-10-26T15:41:33.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2025-06-10T16:14:33.000Z (5 months ago)
- Last Synced: 2025-07-07T19:47:28.705Z (4 months ago)
- Language: Clojure
- Size: 3.16 MB
- Stars: 624
- Watchers: 17
- Forks: 57
- Open Issues: 6
-
Metadata Files:
- Readme: README.adoc
- Changelog: CHANGELOG
- Contributing: CONTRIBUTING.adoc
- License: LICENSE
Awesome Lists containing this project
- awesome-clojure - tick - time (Date and Time)
- stars - juxt/tick - Time as a value. \[*MIT License*\] (⭐️630) (Clojure)
- stars - juxt/tick - Time as a value. \[*MIT License*\] (⭐️626) (Clojure)
README
image:https://img.shields.io/clojars/v/tick.svg?style=svg["clojars",link="https://clojars.org/tick"]
image:https://github.com/juxt/tick/actions/workflows/tests.yaml/badge.svg?style=svg["tests build",link="https://github.com/juxt/tick/actions/workflows/tests.yaml"]
image:https://raw.githubusercontent.com/babashka/babashka/master/logo/badge.svg["bb compatible",link="https://book.babashka.org#badges"]
= tick
A Clojure(Script) & babashka library for dealing with time. Intended as a
replacement for clj-time.
Based on `java.time` (on the JVM) and `js-joda` (on JavaScript
runtimes).
For a cross-platform library using Javascript's modern date-time API https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal[Temporal], see https://github.com/henryw374/tempo[Tempo]
See https://www.youtube.com/watch?v=UFuL-ZDoB2U[Henry Widd's talk at Clojure/North 2019] for more background & rationale.
== Status
* tick.core - stable
* tick.alpha.interval - Alpha: Ready to use with the caveat that the API might still undergo minor changes. Note also that this part of tick is not being actively maintained at the moment (other than to keep existing tests passing). There are currently around https://github.com/juxt/tick/issues?q=is%3Aissue+is%3Aopen+label%3Ainterval-calculus[4 open tickets] relating to intervals which no one is looking at. It may be maintained again in the future of course.
== Should you use Tick for date-time work?
=== Concision vs Discoverability
* If you are just working on the JVM then raw interop will work of course. Doing so, every java.time recipe is easily found via web search and there are many tutorials to aid understanding.
* New and even experienced users of java.time often trip up on Instant not being calendar-aware, so if you would like https://widdindustries.com/why-not-interop/[improved error messages] then use either https://github.com/henryw374/cljc.java-time[cljc.java-time] (which has the same API as java.time and which underpins tick) or tick.
* To use tick effectively an understanding of java.time is required. If you have that understanding and will spend significant time working with dates and times from Clojure, then learning Tick can be worthwhile because it provides a concise, single-ns API.
=== Cross-Platform and/or just JS runtimes
* If working in a Javascript environment then one should be aware that the legacy Date API is https://maggiepint.com/2017/04/09/fixing-javascript-date-getting-started/[flawed]
* Either tick or https://github.com/henryw374/cljc.java-time[cljc.java-time] (which underpins tick) are sane alternatives to js/Date. The JS build size https://widdindustries.com/blog/clojurescript-datetime-lib-comparison.html[will likely not adversely affect your users] and if you already know java.time then your knowledge will carry over.
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal[Temporal] is the native JS replacement for the legacy Date (similar to how java.time was on the JVM). It has some overlap with java.time but also some significant differences. Please see the https://github.com/henryw374/tempo[Tempo] library for an alternative to tick with uses that.
== Usage
[source,clojure]
----
(require '[tick.core :as t])
;; Get the current time
(t/instant)
----
https://github.com/juxt/tick/blob/master/docs/cheatsheet.md[Cheat Sheet]
https://juxt.github.io/tick/[Comprehensive Documentation]
=== Top tips
==== java.time
In both Clojure and Clojurescript versions, tick is just calling through to java.time methods. Understanding https://docs.oracle.com/javase/tutorial/datetime/iso/overview.html[the main entities of java.time] is necessary to use tick. For example, one should know that there are 2 separate ways to measure amounts of time (Period and Duration), 3 ways to represent a point on the timeline (Instant, ZonedDateTime & OffsetDateTime) and so on.
==== Instants
Instants are not 'calendar-aware' - they just contain millis+nanos fields representing an
offset from the Unix epoch. This means Instants have no method to get their year or month for example,
because to do so would require a calendar (e.g. the https://en.wikipedia.org/wiki/Gregorian_calendar[Gregorian calendar]).
However, the following does work in Tick:
`(t/year (t/instant))`. To make that possible, tick first converts the Instant into a ZonedDateTime
(which does have a calendar). Take note however that the zone of the ZonedDateTime will be the
browser's or jvm's timezone. To be explicit about the zone required do this:
[source,clojure]
----
(-> (t/instant)
(t/in "UTC")
(t/year))
----
The other cases where calendar-awareness might come up is when formatting Instants to string or when
shifting them by e.g. years/months, so Tick
https://widdindustries.com/why-not-interop/[shows explanatory error messages in that case].
=== Singular vs Plural ?
As with java.time, any functions working with amounts of time (ie Durations or Periods),
will have names which are in the plural. Functions that
work with dates and times are singular. Knowing that, e.g. `t/second` vs `t/seconds`
makes sense.
== Install
Get the latest from https://clojars.org/tick[Clojars] and
add to your `project.clj`, `build.boot` or `deps.edn`.
To use this from Clojurescript, you must have at least version 1.11.51. If using shadow-cljs, it must be at least version 2.19.3
If using cljsjs, add https://clojars.org/henryw374/js-joda[js-joda] and https://clojars.org/henryw374/js-joda-locale-en-us[js-joda-locale-en-us] and https://clojars.org/cljsjs/js-joda-timezone[js-joda-timezone] to your dependencies as well.
For Clojurescript users of Tick, see https://github.com/juxt/tick/blob/master/docs/cljs.adoc[docs/cljs.adoc], for
some discussion around Clojurescript build size.
Here is a one-liner to drop into a node repl with tick:
----
clj -Sdeps '{:deps {org.clojure/clojurescript {:mvn/version "RELEASE" } tick/tick {:mvn/version "RELEASE"} henryw374/js-joda {:mvn/version "RELEASE"} }}' -m cljs.main -re node --repl
----
Here is how to get to a babashka (v 1.2.174+) repl with tick:
----
export BABASHKA_CLASSPATH=$(clojure -Spath -Sdeps '{:deps {tick/tick {:mvn/version "0.6.0"}}}')
bb
----
== Development
=== Develop The Documentation Site
Build the html
----
make
----
`make dev-docs-cljs` to build the js for the site needs fixing.
=== Build a production version of the Documentation Site
The following is not working currently:
----
make release-docs-cljs
----
=== Develop Tick
----
clj -Atest-clj:test-cljs
----
- To start testing clojurescript, follow instructions in the `cljs` ns.
- To start testing clojure, follow instructions in the the `repl` ns.
See the .github dir to see instructions for CLI testing
=== Release
create a git tag.
`make install` (this installs in ~/.m2 - check that things look ok)
`make deploy` - you need to have set up clojars credentials as per https://github.com/applied-science/deps-library
`git push origin new-tag-name`
== Acknowledgements
In particular, special credit to Eric Evans for discovering Allen's
interval algebra and pointing out its potential usefulness,
demonstrating a working implementation of Allen's ideas in
link:https://github.com/domainlanguage/time-count[his Clojure library].
Thanks also to my esteemed colleagues Patrik Kårlin for his redesign of
the interval constructor function, and Henry Widd for porting to cljc.
== References
* https://github.com/dm3/clojure.java-time
* https://clojuresync.com/emily-ashley/
* https://github.com/aphyr/tea-time
* https://github.com/sunng87/rigui
== Copyright & License
The MIT License (MIT)
Copyright © 2016-2021 JUXT LTD.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.