Skip to content

Commit

Permalink
Merge pull request #898 from dvingo/timezone-fix
Browse files Browse the repository at this point in the history
Do not require timezone data directly for cljs
  • Loading branch information
ikitommi authored Aug 8, 2023
2 parents 94ab7b9 + 96d4440 commit b5c58c6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3042,6 +3042,21 @@ To use these schemas in ClojureScript you will need to install the npm packages
npm install @js-joda/core @js-joda/timezone
```

Because historical timezone data can add ~500kb to your ClojureScript build malli does not require the `@js-joda/timezone`
package directly. You must require timezone data before requiring the `malli.experimental.time` namespace if you want
to make use of zone related time objects.

For example, to include only timezone data for +/- 5 years from the time the library was released, use:

```clojure
(ns com.my-co.my-app
(:require ["@js-joda/timezone/dist/js-joda-timezone-10-year-range"]))
```

For more info see:

https://github.com/js-joda/js-joda/tree/main/packages/timezone

#### min/max

Time schemas respect min/max predicates for their respective types:
Expand Down
4 changes: 1 addition & 3 deletions src/malli/experimental/time.cljc
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
(ns malli.experimental.time
(:refer-clojure :exclude [<=])
(:require [malli.core :as m]
#?@(:cljs
[["@js-joda/core" :as js-joda]
["@js-joda/timezone"]]))
#?(:cljs ["@js-joda/core" :as js-joda]))
#?(:clj (:import (java.time Duration LocalDate LocalDateTime LocalTime Instant ZonedDateTime OffsetDateTime ZoneId OffsetTime ZoneOffset))))

(defn <= [^Comparable x ^Comparable y] (not (pos? (.compareTo x y))))
Expand Down
6 changes: 3 additions & 3 deletions src/malli/experimental/time/generator.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

#?(:clj (set! *warn-on-reflection* true))

(def zone-id-gen
(defn zone-id-gen []
(mg/generator (m/into-schema (m/-enum-schema) nil (map #(. ZoneId of ^String %) (. ZoneId getAvailableZoneIds)))))

(defmethod mg/-schema-generator :time/zone-id [_schema _options] zone-id-gen)
(defmethod mg/-schema-generator :time/zone-id [_schema _options] (zone-id-gen))

#?(:clj (def ^:const ^:private seconds-in-day 86400)
:cljs (def ^:private seconds-in-day 86400))
Expand Down Expand Up @@ -109,7 +109,7 @@
(gen/bind
(-instant-gen schema options)
(fn [instant]
(gen/fmap #(. ZonedDateTime ofInstant instant %) zone-id-gen))))
(gen/fmap #(. ZonedDateTime ofInstant instant %) (zone-id-gen)))))

(defmethod mg/-schema-generator :time/zoned-date-time [schema options]
(-zoned-date-time-gen schema options))
Expand Down
3 changes: 2 additions & 1 deletion test/malli/generator_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
[malli.generator :as mg]
[malli.json-schema-test :as json-schema-test]
[malli.util :as mu]
#?(:clj [malli.test-macros :refer [when-env]]))
#?(:clj [malli.test-macros :refer [when-env]]
:cljs ["@js-joda/timezone/dist/js-joda-timezone-10-year-range"]))
#?(:cljs (:require-macros [malli.test-macros :refer [when-env]])))

(deftest generator-test
Expand Down

0 comments on commit b5c58c6

Please sign in to comment.