Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not require timezone data directly for cljs #898

Merged
merged 1 commit into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3031,6 +3031,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