Skip to content

Commit

Permalink
Add same-date? first and last-day-of-the-month? functions
Browse files Browse the repository at this point in the history
  • Loading branch information
psalaberria002 authored and andrewmcveigh committed Mar 6, 2016
1 parent cd130b5 commit 77649a4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/cljs_time/predicates.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
Is it January?
(january? (clj-time.core/date-time 2011 1 1))"
(:require [cljs-time.core :as time]))
(:require [cljs-time.core :as time]
[cljs-time.coerce :as coerce]))

;; days of the week
(defn monday? [date-time]
Expand Down Expand Up @@ -73,3 +74,15 @@

(defn december? [date-time]
(= (time/month date-time) 12))

;;First and last day of month checks
(defn last-day-of-month? [date-time]
(= (time/last-day-of-the-month date-time) date-time))

(defn first-day-of-month? [date-time]
(= (time/first-day-of-the-month date-time) date-time))

(defn same-date?
"Compares two date times to see if they are the same date"
[this-date-time that-date-time]
(= (coerce/to-local-date this-date-time) (coerce/to-local-date that-date-time)))
17 changes: 16 additions & 1 deletion test/cljs_time/predicates_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
[cljs-time.predicates :refer
[monday? tuesday? wednesday? thursday? friday? saturday? sunday? weekend?
weekday? january? february? march? april? may? june? july? august?
september? october? november? december?]]))
september? october? november? december?
last-day-of-month? first-day-of-month?
same-date?]]))

(deftest test-days-of-the-week
(is (= true (monday? (date-time 2012 9 10))))
Expand Down Expand Up @@ -36,3 +38,16 @@
(is (= true (november? (date-time 2012 11 16))))
(is (= true (december? (date-time 2012 12 16))))
(is (= false (january? (date-time 2012 12 31)))))

(deftest test-first-and-last-day-of-month
(is (= false (first-day-of-month? (date-time 2016 2 2))))
(is (= true (first-day-of-month? (date-time 2016 2 1))))
(is (= false (last-day-of-month? (date-time 2016 2 28))))
(is (= true (last-day-of-month? (date-time 2016 1 31))))
(is (= true (last-day-of-month? (date-time 2016 2 29)))))

(deftest test-same-date
(is (= true (same-date? (date-time 2013 10 8) (date-time 2013 10 8))))
(is (= true (same-date? (date-time 2012 1 1) (date-time 2012 1 1))))
(is (= false (same-date? (date-time 2012 2 2) (date-time 2012 1 1))))
(is (= false (same-date? (date-time 2011 2 2) (date-time 2012 2 2)))))

0 comments on commit 77649a4

Please sign in to comment.