From 1b1e564288d4ab4f0777a0aed1349cf3c9c7bc1c Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Wed, 18 Dec 2019 16:38:21 +0100 Subject: [PATCH] [#149] Add java.time.* classes --- README.md | 4 ++ examples/pst.clj | 7 +++ reflection.json | 100 +++++++++++++++++++++++++++++++ src/babashka/impl/classes.clj | 20 +++++++ test/babashka/java_time_test.clj | 19 ++++++ test/babashka/main_test.clj | 5 +- 6 files changed, 152 insertions(+), 3 deletions(-) create mode 100755 examples/pst.clj create mode 100644 test/babashka/java_time_test.clj diff --git a/README.md b/README.md index b2789187..7146c83c 100644 --- a/README.md +++ b/README.md @@ -689,6 +689,10 @@ bb '(let [{:keys [dependencies source-paths resource-paths]} (apply hash-map (dr jet --pretty > deps.edn ``` +### Print current time in California + +See [examples/pst.clj](https://github.com/borkdude/babashka/blob/master/examples/pst.clj) + ## Thanks - [adgoji](https://www.adgoji.com/) for financial support diff --git a/examples/pst.clj b/examples/pst.clj new file mode 100755 index 00000000..2a919f5f --- /dev/null +++ b/examples/pst.clj @@ -0,0 +1,7 @@ +#!/usr/bin/env bb + +(def now (java.time.ZonedDateTime/now)) +(def LA-timezone (java.time.ZoneId/of "America/Los_Angeles")) +(def LA-time (.withZoneSameInstant now LA-timezone)) +(def pattern (java.time.format.DateTimeFormatter/ofPattern "HH:mm")) +(println (.format LA-time pattern)) diff --git a/reflection.json b/reflection.json index 030baf63..507ad76c 100644 --- a/reflection.json +++ b/reflection.json @@ -163,6 +163,106 @@ "allPublicMethods" : true, "allPublicFields" : true, "allPublicConstructors" : true +}, { + "name" : "java.time.Clock", + "allPublicMethods" : true, + "allPublicFields" : true, + "allPublicConstructors" : true +}, { + "name" : "java.time.DateTimeException", + "allPublicMethods" : true, + "allPublicFields" : true, + "allPublicConstructors" : true +}, { + "name" : "java.time.DayOfWeek", + "allPublicMethods" : true, + "allPublicFields" : true, + "allPublicConstructors" : true +}, { + "name" : "java.time.Duration", + "allPublicMethods" : true, + "allPublicFields" : true, + "allPublicConstructors" : true +}, { + "name" : "java.time.Instant", + "allPublicMethods" : true, + "allPublicFields" : true, + "allPublicConstructors" : true +}, { + "name" : "java.time.LocalDate", + "allPublicMethods" : true, + "allPublicFields" : true, + "allPublicConstructors" : true +}, { + "name" : "java.time.LocalDateTime", + "allPublicMethods" : true, + "allPublicFields" : true, + "allPublicConstructors" : true +}, { + "name" : "java.time.LocalTime", + "allPublicMethods" : true, + "allPublicFields" : true, + "allPublicConstructors" : true +}, { + "name" : "java.time.Month", + "allPublicMethods" : true, + "allPublicFields" : true, + "allPublicConstructors" : true +}, { + "name" : "java.time.MonthDay", + "allPublicMethods" : true, + "allPublicFields" : true, + "allPublicConstructors" : true +}, { + "name" : "java.time.OffsetDateTime", + "allPublicMethods" : true, + "allPublicFields" : true, + "allPublicConstructors" : true +}, { + "name" : "java.time.OffsetTime", + "allPublicMethods" : true, + "allPublicFields" : true, + "allPublicConstructors" : true +}, { + "name" : "java.time.Period", + "allPublicMethods" : true, + "allPublicFields" : true, + "allPublicConstructors" : true +}, { + "name" : "java.time.Year", + "allPublicMethods" : true, + "allPublicFields" : true, + "allPublicConstructors" : true +}, { + "name" : "java.time.YearMonth", + "allPublicMethods" : true, + "allPublicFields" : true, + "allPublicConstructors" : true +}, { + "name" : "java.time.ZoneId", + "allPublicMethods" : true, + "allPublicFields" : true, + "allPublicConstructors" : true +}, { + "name" : "java.time.ZoneOffset", + "allPublicMethods" : true, + "allPublicFields" : true, + "allPublicConstructors" : true +}, { + "name" : "java.time.ZonedDateTime", + "allPublicMethods" : true, + "allPublicFields" : true, + "allPublicConstructors" : true +}, { + "name" : "java.time.format.DateTimeFormatter", + "allPublicMethods" : true, + "allPublicFields" : true, + "allPublicConstructors" : true +}, { + "name" : "java.time.temporal.TemporalAccessor", + "allPublicMethods" : true, + "allPublicFields" : true, + "allPublicConstructors" : true }, { "name" : "java.util.concurrent.LinkedBlockingQueue", "allPublicMethods" : true, diff --git a/src/babashka/impl/classes.clj b/src/babashka/impl/classes.clj index d4938344..be6cbec9 100644 --- a/src/babashka/impl/classes.clj +++ b/src/babashka/impl/classes.clj @@ -38,6 +38,26 @@ java.nio.file.attribute.FileAttribute java.nio.file.attribute.PosixFilePermission java.nio.file.attribute.PosixFilePermissions + java.time.format.DateTimeFormatter + java.time.Clock + java.time.DateTimeException + java.time.DayOfWeek + java.time.Duration + java.time.Instant + java.time.LocalDate + java.time.LocalDateTime + java.time.LocalTime + java.time.Month + java.time.MonthDay + java.time.OffsetDateTime + java.time.OffsetTime + java.time.Period + java.time.Year + java.time.YearMonth + java.time.ZonedDateTime + java.time.ZoneId + java.time.ZoneOffset + java.time.temporal.TemporalAccessor java.util.regex.Pattern sun.nio.fs.UnixPath ;; included because of permission check ] diff --git a/test/babashka/java_time_test.clj b/test/babashka/java_time_test.clj new file mode 100644 index 00000000..65b22709 --- /dev/null +++ b/test/babashka/java_time_test.clj @@ -0,0 +1,19 @@ +(ns babashka.java-time-test + (:require + [babashka.test-utils :as test-utils] + [clojure.edn :as edn] + [clojure.test :as test :refer [deftest is]])) + +(defn bb [expr] + (edn/read-string (apply test-utils/bb nil [(str expr)]))) + +(deftest java-time-test + (is (= "2019-12-18" (bb '(str (java.time.LocalDate/of 2019 12 18))))) + (is (= "2019-12-01" (bb '(str + (-> (java.time.LocalDate/of 2019 12 18) + (.minusDays 17)))))) + (is (= "MONDAY" (bb '(str java.time.DayOfWeek/MONDAY)))) + (is (= "18-12-2019 16:01:41" + (bb '(.format + (java.time.LocalDateTime/parse "2019-12-18T16:01:41.485") + (java.time.format.DateTimeFormatter/ofPattern "dd-MM-yyyy HH:mm:ss")))))) diff --git a/test/babashka/main_test.clj b/test/babashka/main_test.clj index 7fa304ad..056246a0 100644 --- a/test/babashka/main_test.clj +++ b/test/babashka/main_test.clj @@ -3,12 +3,11 @@ [babashka.main :as main] [babashka.test-utils :as test-utils] [clojure.edn :as edn] + [clojure.java.io :as io] [clojure.java.shell :refer [sh]] [clojure.string :as str] [clojure.test :as test :refer [deftest is testing]] - [clojure.java.io :as io] - [sci.core :as sci] - [clojure.test :as t])) + [sci.core :as sci])) (defn bb [input & args] (edn/read-string (apply test-utils/bb (when (some? input) (str input)) (map str args))))