forked from clojure/clojurescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLJS-3291: Incorrect #inst parsing with respect to Julian / Gregorian…
… calendar transition
- Loading branch information
Showing
6 changed files
with
104 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
;; Copyright (c) Rich Hickey. All rights reserved. | ||
;; The use and distribution terms for this software are covered by the | ||
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) | ||
;; which can be found in the file epl-v10.html at the root of this distribution. | ||
;; By using this software in any fashion, you are agreeing to be bound by | ||
;; the terms of this license. | ||
;; You must not remove this notice, or any other, from this software. | ||
|
||
(ns cljs.instant | ||
(:require [clojure.instant :as inst]) | ||
(:import [java.time Instant OffsetDateTime ZoneOffset] | ||
[java.time.format DateTimeFormatter DateTimeFormatterBuilder] | ||
[java.util Locale Locale$Category])) | ||
|
||
(set! *warn-on-reflection* true) | ||
|
||
(def ^:private ^java.time.format.DateTimeFormatter utc-format | ||
(-> (DateTimeFormatterBuilder.) | ||
(.appendInstant 9) | ||
(.toFormatter (Locale/getDefault Locale$Category/FORMAT)))) | ||
|
||
(defn- remove-last-char ^String [s] | ||
(subs s 0 (dec (count s)))) | ||
|
||
(defn- print-instant | ||
"Print a java.time.Instant as RFC3339 timestamp, always in UTC." | ||
[^java.time.Instant instant, ^java.io.Writer w] | ||
(.write w "#inst \"") | ||
(.write w (remove-last-char (.format utc-format instant))) | ||
(.write w "-00:00\"")) | ||
|
||
(defmethod print-method java.time.Instant | ||
[^java.time.Instant instant, ^java.io.Writer w] | ||
(print-instant instant w)) | ||
|
||
(defmethod print-dup java.time.Instant | ||
[^java.time.Instant instant, ^java.io.Writer w] | ||
(print-instant instant w)) | ||
|
||
(defn- construct-instant | ||
"Construct a java.time.Instant, which has nanosecond precision." | ||
[years months days hours minutes seconds nanoseconds | ||
offset-sign offset-hours offset-minutes] | ||
(Instant/from | ||
(OffsetDateTime/of years months days hours minutes seconds nanoseconds | ||
(ZoneOffset/ofHoursMinutes (* offset-sign offset-hours) (* offset-sign offset-minutes))))) | ||
|
||
(defn read-instant-instant | ||
"To read an instant as a java.time.Instant, bind *data-readers* to a | ||
map with this var as the value for the 'inst key. Instant preserves | ||
fractional seconds with nanosecond precision. The timezone offset will | ||
be used to convert into UTC." | ||
[^CharSequence cs] | ||
(inst/parse-timestamp (inst/validated construct-instant) cs)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
;; Copyright (c) Rich Hickey. All rights reserved. | ||
;; The use and distribution terms for this software are covered by the | ||
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) | ||
;; which can be found in the file epl-v10.html at the root of this distribution. | ||
;; By using this software in any fashion, you are agreeing to be bound by | ||
;; the terms of this license. | ||
;; You must not remove this notice, or any other, from this software. | ||
|
||
(ns cljs.instant-tests | ||
(:require | ||
[cljs.instant :as inst] | ||
[clojure.test :refer [deftest is]])) | ||
|
||
(deftest read-instant-instant-test | ||
;; Clojure uses hybrid Julian / Gregorian, while Instant is proleptic Gregorian | ||
(is (not= #inst "1500" (inst/read-instant-instant "1500"))) | ||
(is (not= (inst-ms #inst "1500") (inst-ms (inst/read-instant-instant "1500")))) | ||
(is (= -14831769600000 (inst-ms (inst/read-instant-instant "1500")))) | ||
(is (= "#inst \"1500-01-01T00:00:00.123456789-00:00\"" | ||
(pr-str (inst/read-instant-instant "1500-01-01T00:00:00.123456789-00:00")))) | ||
(is (= "#inst \"2020-01-01T05:00:00.000000000-00:00\"" | ||
(pr-str (inst/read-instant-instant "2020-01-01T00:00:00.000-05:00"))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters