Skip to content

Commit 38daf95

Browse files
committed
Implement week-number
1 parent cbb4711 commit 38daf95

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/cljs_time/core.cljs

+20-2
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ expected."}
118118
(minus- [this period] "Returns a new date/time corresponding to the given date/time moved backwards by the given Period(s).")
119119
(first-day-of-the-month- [this] "Returns the first day of the month")
120120
(last-day-of-the-month- [this] "Returns the last day of the month")
121-
(week-number-of-year [this] "Returs the number of weeks in the year"))
121+
(week-number-of-year [this] "Returns the week of the week based year of the given date/time")
122+
(week-year [this] "Returns the the week based year of the given date/time."))
122123

123124
(defprotocol InTimeUnitProtocol
124125
"Interface for in-<time unit> functions"
@@ -187,6 +188,17 @@ expected."}
187188
(not= dayo dayother) (- dayo dayother)
188189
:else 0)))
189190

191+
(defn get-week-year
192+
"Counterpart ot goog.date/getWeekNumber"
193+
[year month date]
194+
(let [january (= month 0)
195+
december (= month 11)
196+
week-number (goog.date/getWeekNumber year month date)]
197+
(cond
198+
(and january (>= week-number 52)) (dec year)
199+
(and december (= week-number 1)) (inc year)
200+
:else year)))
201+
190202
(extend-protocol DateTimeProtocol
191203
goog.date.UtcDateTime
192204
(year [this] (.getYear this))
@@ -211,6 +223,8 @@ expected."}
211223
(week-number-of-year [this]
212224
(goog.date/getWeekNumber
213225
(.getYear this) (.getMonth this) (.getDate this)))
226+
(week-year [this]
227+
(get-week-year (.getYear this) (.getMonth this) (.getDate this)))
214228

215229
goog.date.DateTime
216230
(year [this] (.getYear this))
@@ -235,6 +249,8 @@ expected."}
235249
(week-number-of-year [this]
236250
(goog.date/getWeekNumber
237251
(.getYear this) (.getMonth this) (.getDate this)))
252+
(week-year [this]
253+
(get-week-year (.getYear this) (.getMonth this) (.getDate this)))
238254

239255
goog.date.Date
240256
(year [this] (.getYear this))
@@ -258,7 +274,9 @@ expected."}
258274
(period :days 1)))
259275
(week-number-of-year [this]
260276
(goog.date/getWeekNumber
261-
(.getYear this) (.getMonth this) (.getDate this))))
277+
(.getYear this) (.getMonth this) (.getDate this)))
278+
(week-year [this]
279+
(get-week-year (.getYear this) (.getMonth this) (.getDate this))))
262280

263281
(def utc #js {:id "UTC" :std_offset 0 :names ["UTC"] :transitions []})
264282

test/cljs_time/core_test.cljs

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
years? months? weeks? days? hours? minutes? seconds?
1919
extend start end mins-ago default-time-zone
2020
to-default-time-zone from-default-time-zone
21-
overlap week-number-of-year floor]]
21+
overlap week-number-of-year week-year floor]]
2222
[cljs-time.extend]))
2323

2424
(deftest test-now
@@ -550,6 +550,12 @@
550550
(is (= 1 (week-number-of-year (date-time 2012 12 31))))
551551
(is (= 1 (week-number-of-year (date-time 2013 1 1)))))
552552

553+
(deftest test-week-year
554+
(is (= 2015 (week-year (date-time 2014 12 29))))
555+
(is (= 2015 (week-year (date-time 2015 1 5))))
556+
(is (= 2009 (week-year (date-time 2010 1 3))))
557+
(is (= 2010 (week-year (date-time 2010 1 4)))))
558+
553559
(deftest test-number-of-days-in-the-month
554560
(is (= 31 (number-of-days-in-the-month 2012 1)))
555561
(is (= 31 (number-of-days-in-the-month (date-time 2012 1 3))))

0 commit comments

Comments
 (0)