forked from KirinDave/clj-time
-
Notifications
You must be signed in to change notification settings - Fork 162
/
predicates.clj
96 lines (66 loc) · 2.29 KB
/
predicates.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
(ns clj-time.predicates
"Predicate functions to ask basic questions about a date
Was it Monday?
(monday? (clj-time.core/date-time 1999 9 9))
Is it January?
(january? (clj-time.core/date-time 2011 1 1))"
(:require [clj-time.core :as time]
[clj-time.coerce :as coerce]))
;; days of the week
(defn monday? [date-time]
(= (time/day-of-week date-time) 1))
(defn tuesday? [date-time]
(= (time/day-of-week date-time) 2))
(defn wednesday? [date-time]
(= (time/day-of-week date-time) 3))
(defn thursday? [date-time]
(= (time/day-of-week date-time) 4))
(defn friday? [date-time]
(= (time/day-of-week date-time) 5))
(defn saturday? [date-time]
(= (time/day-of-week date-time) 6))
(defn sunday? [date-time]
(= (time/day-of-week date-time) 7))
;; weekend / weekday checks
(defn weekend? [date-time]
(or (saturday? date-time) (sunday? date-time)))
(defn weekday? [date-time]
(not (weekend? date-time)))
;; months of the year
(defn january? [date-time]
(= (time/month date-time) 1))
(defn february? [date-time]
(= (time/month date-time) 2))
(defn march? [date-time]
(= (time/month date-time) 3))
(defn april? [date-time]
(= (time/month date-time) 4))
(defn may? [date-time]
(= (time/month date-time) 5))
(defn june? [date-time]
(= (time/month date-time) 6))
(defn july? [date-time]
(= (time/month date-time) 7))
(defn august? [date-time]
(= (time/month date-time) 8))
(defn september? [date-time]
(= (time/month date-time) 9))
(defn october? [date-time]
(= (time/month date-time) 10))
(defn november? [date-time]
(= (time/month date-time) 11))
(defn december? [date-time]
(= (time/month date-time) 12))
;;First and last day of month checks
(defn last-day-of-month? [date-time]
(time/equal? (time/last-day-of-the-month date-time) date-time))
(def last-day-of-the-month? last-day-of-month?)
(defn first-day-of-month? [date-time]
(time/equal? (time/first-day-of-the-month date-time) date-time))
(def first-day-of-the-month? first-day-of-month?)
(defn nth-day-of-the-month? [date-time n]
(time/equal? (time/nth-day-of-the-month date-time n) date-time))
(defn same-date?
"Compares two date times to see if they are the same date"
[this-date-time that-date-time]
(time/equal? (coerce/to-local-date this-date-time) (coerce/to-local-date that-date-time)))