Skip to content

Commit

Permalink
Merge pull request #128 from weiznich/support_more_fraction
Browse files Browse the repository at this point in the history
Add support for more second fraction digits
  • Loading branch information
andrewmcveigh committed Jun 18, 2019
2 parents e1c37af + 1fc5bf9 commit c03fb32
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
12 changes: 10 additions & 2 deletions src/cljs_time/internal/parse.cljs
Expand Up @@ -129,7 +129,14 @@
(defn parse-millis
([limit] (parse-millis 1 limit))
([lower upper]
(fn [s] (parse-period s :millis lower upper))))
(fn [s]
(let [[n s] (read-while #(re-find #"\d" %) s)
[n s]
(if (>= (count n) lower)
[(js/parseInt (apply str (take (min upper 3) n))) (concat (drop upper n) s)]
[(js/parseInt (apply str (take 3 n))) s])]
[[:millis n] s])
)))

(defn timezone-adj [sign hh mm]
(let [hh (js/parseInt hh 10)
Expand Down Expand Up @@ -228,6 +235,7 @@
(case pattern
"S" (parse-millis 1 2)
"SSS" (parse-millis 3 3)
"SSSS" (parse-millis 9)
"s" (parse-seconds 1 2)
"ss" (parse-seconds 2 2)
"m" (parse-minutes 1 2)
Expand Down Expand Up @@ -307,7 +315,7 @@
(if (and weekyear weekyear-week)
(let [date (Date. weekyear 0 4)]
(.add date (Interval. 0 0 (* 7 (dec weekyear-week))))
(.add date (Interval. 0 0 (- (or day-of-week 1)
(.add date (Interval. 0 0 (- (or day-of-week 1)
(inc (mod (dec (.getDay date)) 7)))))
(-> date-map
(assoc :years (.getYear date))
Expand Down
12 changes: 8 additions & 4 deletions test/cljs_time/format_test.cljs
Expand Up @@ -77,7 +77,11 @@
(is (= [2002 10 2 13 0 0 0]
(utc-int-vec
(format/parse (:rfc822 formatters)
"Wed, 02 Oct 2002 15:00:00 +0200")))))
"Wed, 02 Oct 2002 15:00:00 +0200"))))
(is (= [2018 3 27 10 44 23 22]
(utc-int-vec
(format/parse (formatter "yyyy-MM-dd'T'HH:mm:ss.SSSSZZ")
"2018-03-27T10:44:23.022787+00:00")))))

(deftest unparse-test
(let [date (from-date #inst "2013-08-29T00:00:00.000-00:00")]
Expand Down Expand Up @@ -378,11 +382,11 @@

(deftest weekyear-week-day-test
(let [fmt (format/formatters :weekyear-week-day)]
(is (= "2015-W01-1"
(is (= "2015-W01-1"
(format/unparse fmt (time/date-time 2014 12 29))))
(is (= "2015-W01-2"
(is (= "2015-W01-2"
(format/unparse fmt (time/date-time 2014 12 30))))
(is (= "2015-W01-7"
(is (= "2015-W01-7"
(format/unparse fmt (time/date-time 2015 1 4))))
(is (= "2009-W53-7"
(format/unparse fmt (time/date-time 2010 1 3))))
Expand Down

0 comments on commit c03fb32

Please sign in to comment.