Code Monkey home page Code Monkey logo

cljs.java-time's Introduction

cljs.java.time

A Clojurescript library that provides the jsr-310 (java.time) api.

Note: this is not a wrapper library, meaning there are not additional Clojurescript functions, only what java.time provides.

Underneath this lib is a pure JS implementation of java.time. This extends that by adding Clojurescript's equivalence, hash and comparison protocols to the java.time domain objects, providing externs and having the packages of java.time be mirrored by namespaces.

Related Libraries

cljc.java-time offers a one for one mapping of the classes and methods from java.time into a Clojure(Script) library

Using that library is tick, an intuitive Clojure(Script) library for dealing with time, intended as a replacement for clj-time.

time-literals is a Clojure(Script) library which provides tagged literals for objects from jsr-310 domain and depends on this library

my talk at Clojure/North 2019 provides some background

Usage

get it from Clojars

(require '[java.time :refer [LocalDate]])

(.parse LocalDate "2020-01-01")

Cross Platform (.cljc) Example

(ns foo
  (:require
    #?(:cljs [java.time :refer [LocalDate]])
    [time-literals.read-write]
    [cljs.java-time.interop :as t.i])
   #?(:clj (:import [java.time LocalDate])))
   
   
  (. LocalDate parse "2020-12-01")
  ;=> #time/date"2020-12-01"
  
  (= 
    (. LocalDate parse "2020-12-01")
    (. LocalDate parse "2020-12-01"))
  ; => true  
  
  ; call a getter method
  (let [l (. LocalDate parse "2020-12-01")]
    (t.i/getter dayOfMonth l))
  

Getter Methods

Unfortunately, all java.time getter methods have had the 'get' part of their name removed in the underlying js lib. So instead of 'getNano' method, you have 'nano'. As a workaround, to write cross platform code that calls any getter methods on java.time objects, use the cljs.java-time.interop/getter macro

NPM Dependency

The npm library that provides the jsr-310 api is provided via a foreign-lib. Whilst this is handy if you don't already have an npm build step, if you do want to depend directly on the js-joda npm lib you can do so. For more info see the tick docs on setup for use with ClojureScript

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.