Liking cljdoc? Tell your friends :D

Holi

Holi is a Clojure and ClojureScript library for working with non-business days

build Clojars Project

Examples

(ns my-app
  (:require [luciolucio.holi :as holi]
            [tick.core :as t]))

;      July 2019
; Su Mo Tu We Th Fr Sa
;     1  2  3  4  5  6
;  7  8  9 10 11 12 13
; 14 15 16 17 18 19 20
; 21 22 23 24 25 26 27
; 28 29 30 31

(holi/add (t/date "2019-07-12") 3 :business-days) ; 17Jul2020 (skips weekends)
(holi/add (t/date "2019-07-03") 1 :business-days "US") ; 5Jul2019 (skips 4th of July as a US holiday)

(holi/weekend? (t/date "2019-07-06")) ; -> true
(holi/holiday? (t/date "2019-07-04") "US") ; -> true

Use of juxt/tick is not required, but highly recommended.

Installation

Import the latest version from clojars into your project dependencies

Usage in ClojureScript

Holi has been successfully tested when built with shadow-cljs and run on a browser. If you have issues with other setups, hit me up on clojurians Slack: @Lucio Assis

Usage

add

(add [date n unit & calendars])

Adds n of unit to date and returns a new date. Skips holidays in calendars when unit is :business-days.

ParameterDescriptionExample
dateAn instance of java.time.LocalDate or java.time.LocalDateTime(LocalDate/of 2020 10 9)
nAn integer2, -1, 0
unitUnit of n:days :weeks :months :years or :business-days
calendarsOne or more strings representing holiday calendars (:business-days only)"US", "BR"

Notes

  1. Types are preserved, i.e., passing a LocalDate in will return a LocalDate
  2. The time portion is never altered on a LocalDateTime instance
  3. unit will accept both :weeks and :week etc. That way you can say 1 :week or 1 :business-day.

weekend?

(weekend? [date])

Returns true if date is in a weekend, and false otherwise

ParameterDescriptionExample
dateAn instance of java.time.LocalDate or java.time.LocalDateTime(LocalDate/of 2020 10 9)

Notes

  1. Weekend days are assumed to be Saturday and Sunday

holiday?

(holiday? [date calendar])

Returns true if date is a holiday in the given calendar, and false otherwise

ParameterDescriptionExample
dateAn instance of java.time.LocalDate or java.time.LocalDateTime(LocalDate/of 2020 10 9)
calendarA string representing a holiday calendar"US", "BR"

non-business-day?

(non-business-day? [date & calendars])

Returns true only if date is whether in a weekend or a holiday in one of the given calendars. Returns false otherwise.

ParameterDescriptionExample
dateAn instance of java.time.LocalDate or java.time.LocalDateTime(LocalDate/of 2020 10 9)
calendarsOne or more strings representing holiday calendars"US", "BR"

business-day?

(business-day? [date & calendars])

Returns true only if date is not in a weekend and also not a holiday in any of the given calendars. Returns false otherwise.

ParameterDescriptionExample
dateAn instance of java.time.LocalDate or java.time.LocalDateTime(LocalDate/of 2020 10 9)
calendarsOne or more strings representing holiday calendars"US", "BR"

Tips and tricks

"Same or next" business day

Use n = 0 to get same or next semantics, that is:

  • If the date is a business day already, just return the same date
  • Otherwise it's as if you had used n = 1
(ns my-app
  (:require [luciolucio.holi :as holi]
    [tick.core :as t]))

(holi/add (t/date "2022-07-06") 0 :business-days) ; -> 8Jul22, because 6Jul22 is a Saturday
(holi/add (t/date "2022-07-07") 0 :business-days) ; -> 8Jul22, because 7Jul22 is a Sunday
(holi/add (t/date "2022-07-08") 0 :business-days) ; -> 8Jul22, as it's a regular Monday

One way this is useful is for things like "This is due next month, on the same day of the month, but if it's a weekend then it's the following business day":

(-> (t/date "2022-06-06")
    (holi/add 1 :months)         ; 6Jul22, Sat
    (holi/add 0 :business-days)) ; 8Jul22, Mon

Available holiday calendars

Holiday calendarDescription
USUnited States holidays
GBGreat Britain holidays
BRBrazilian holidays
brazil/sao-pauloSão Paulo city holidays

FAQ

What do I do if a date is wrong or a holiday calendar is missing for <insert location here>?

You have two options:

  • Contribute a fix, or a new calendar to the project (see CONTRIBUTING.md)
  • Build yourself a custom library, with your own holiday calendars (it's easy! See CUSTOM.md)

Can you improve this documentation?Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close