|
3 | 3 | (:require
|
4 | 4 | [clojure.string :as string]
|
5 | 5 | [goog.string :as gstring]
|
6 |
| - [goog.string.format])) |
| 6 | + [goog.string.format] |
| 7 | + [goog.date])) |
7 | 8 |
|
8 | 9 | (def months
|
9 | 10 | ["January" "February" "March" "April" "May" "June" "July" "August"
|
|
36 | 37 | (and (leap-year? year) (= month 2)) inc))
|
37 | 38 |
|
38 | 39 | (defn valid-date?
|
39 |
| - [{:keys [years months days hours minutes seconds millis] :as d}] |
| 40 | + [{:keys [years months days hours minutes seconds millis |
| 41 | + weekyear weekyear-week day-of-week] :as d}] |
40 | 42 | (let [months? (when months (<= 1 months 12))
|
41 | 43 | dim (if years
|
42 | 44 | (and months months? (year-corrected-dim years months))
|
|
45 | 47 | hours? (when hours (<= 0 hours 23))
|
46 | 48 | minutes? (when minutes (<= 0 minutes 59))
|
47 | 49 | seconds? (when seconds (<= 0 seconds 60))
|
48 |
| - millis? (when millis (<= 0 millis 999))] |
49 |
| - (if (->> [months? days? hours? minutes? seconds? millis?] |
| 50 | + millis? (when millis (<= 0 millis 999)) |
| 51 | + weekyear-week? (when weekyear-week (<= 1 weekyear-week 53)) |
| 52 | + day-of-week? (when day-of-week (<= 1 day-of-week 7))] |
| 53 | + (if (->> [months? days? hours? minutes? seconds? millis? |
| 54 | + weekyear-week? day-of-week?] |
50 | 55 | (remove nil?)
|
51 | 56 | (every? true?))
|
52 |
| - d |
| 57 | + (if (not (and (or years months days) |
| 58 | + (or weekyear weekyear-week day-of-week))) |
| 59 | + d |
| 60 | + (throw |
| 61 | + (ex-info "Mixing year, month, day and week-year week-number fields" |
| 62 | + {:type :invalid-date :date d |
| 63 | + :errors {}}))) |
53 | 64 | (throw
|
54 | 65 | (ex-info "Date is not valid"
|
55 | 66 | {:type :invalid-date :date d
|
|
59 | 70 | (false? hours?) (assoc :hours hours)
|
60 | 71 | (false? minutes?) (assoc :minutes minutes)
|
61 | 72 | (false? seconds?) (assoc :seconds seconds)
|
62 |
| - (false? millis?) (assoc :millis millis))}))))) |
| 73 | + (false? millis?) (assoc :millis millis) |
| 74 | + (false? weekyear-week?) (assoc :weekyear-week weekyear-week) |
| 75 | + (false? day-of-week?) (assoc :day-of-week day-of-week))}))))) |
63 | 76 |
|
64 | 77 | (defn index-of [coll x]
|
65 | 78 | (first (keep-indexed #(when (= %2 x) %1) coll)))
|
|
97 | 110 | (update-in [:weeks] scale-fn)
|
98 | 111 | (update-in [:months] scale-fn)
|
99 | 112 | (update-in [:years] scale-fn))))
|
| 113 | + |
| 114 | +(defn get-week-year |
| 115 | + "Counterpart ot goog.date/getWeekNumber. |
| 116 | + month 0 is jan per goog.date" |
| 117 | + [year month date] |
| 118 | + (let [january (= month 0) |
| 119 | + december (= month 11) |
| 120 | + week-number (goog.date/getWeekNumber year month date)] |
| 121 | + (cond |
| 122 | + (and january (>= week-number 52)) (dec year) |
| 123 | + (and december (= week-number 1)) (inc year) |
| 124 | + :else year))) |
0 commit comments