Code Monkey home page Code Monkey logo

cljc.java-time's Introduction

Clojars Project

CircleCI

cljc.java-time

A Clojure(Script) library which mirrors the java.time api through kebab-case-named vars.

It also has predicates for each entity: (instant? x)

See my talk at Clojure/North 2019 for more background.

Related Libraries

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

Rationale

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:

  • To call the java.time 'static' methods in a cljc file you need to use the dot-special-form which is not idiomatic
  • The underlying js library has changed the name of the getter methods in java.time (in almost all cases) to remove the 'get' part of the name. There are ways to get around that on a case by case basis, but this library handles it for you.

How it works

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

Usage

Get it from Clojars

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)
  

Problems & Irregularities

java.time.Year#isLeap exists as an instance method and a static method. Only the static version has been wrapped.

Inheritcance/Polymorphism

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.

Note

Java 9

New methods were added in Java 9 - these are not included in this library

ClojureScript

Be aware that the Implementation is not 100%. It's probably 99% though and anything you find missing can be added via pull request to js-joda

License

Copyright © 2019 Widd Industries

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.