Skip to content

Commit e90f3a7

Browse files
committed
Implement weekday? and last-weekday-of-month?
1 parent 1cca4f2 commit e90f3a7

3 files changed

Lines changed: 48 additions & 2 deletions

File tree

src/date.janet

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@
123123
[d1 d2]
124124
(>= (to-time d1) (to-time d2)))
125125

126+
(defn weekday?
127+
```
128+
Returns true if the date is a week day (Monday-Friday).
129+
```
130+
[date]
131+
(has-value? ["Monday" "Tuesday" "Wednesday" "Thursday" "Friday"]
132+
(date :week-day)))
133+
126134
(defn last-day-of-month?
127135
```
128136
Returns true if the date is the last day of a month.
@@ -139,3 +147,11 @@
139147
(def next-week (+days date 7))
140148
(and (= (date :week-day) "Friday")
141149
(not= (date :month) (next-week :month))))
150+
151+
(defn last-weekday-of-month?
152+
```
153+
Return true if the date is the last week day of a month.
154+
```
155+
[date]
156+
(or (and (weekday? date) (last-day-of-month? date))
157+
(last-friday-of-month? date)))

test/commands/schedule_tasks_test.janet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
(test (scheduled-for? task (d/date 2022 6 30)) true)
9494
(test (scheduled-for? task (d/date 2022 7 29)) true)
9595
(test (scheduled-for? task (d/date 2022 8 31)) true)
96-
(test (scheduled-for? task (d/date 2022 9 29)) true)
96+
(test (scheduled-for? task (d/date 2022 9 30)) true)
9797
(test (scheduled-for? task (d/date 2022 10 31)) true)
9898
(test (scheduled-for? task (d/date 2022 11 30)) true)
9999
(test (scheduled-for? task (d/date 2022 12 30)) true)

test/date_test.janet

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@
6868
(test (d/after-or-eq? (d/date 2021 7 1) (d/date 2021 7 1)) true)
6969
(test (not (d/after-or-eq? (d/date 2021 7 1) (d/date 2021 7 15))) true))
7070

71+
## —————————————————————————————————————————————————————————————————————————————————————————————————
72+
## Test weekday?
73+
74+
(deftest "returns true if the date is a weekday"
75+
(test (d/weekday? (d/date 2022 1 3)) true)
76+
(test (d/weekday? (d/date 2022 1 4)) true)
77+
(test (d/weekday? (d/date 2022 1 5)) true)
78+
(test (d/weekday? (d/date 2022 1 6)) true)
79+
(test (d/weekday? (d/date 2022 1 7)) true)
80+
(test (d/weekday? (d/date 2022 1 8)) false)
81+
(test (d/weekday? (d/date 2022 1 9)) false))
82+
7183
## —————————————————————————————————————————————————————————————————————————————————————————————————
7284
## Test last-day-of-month?
7385

@@ -88,7 +100,25 @@
88100
(test (not (d/last-day-of-month? (d/date 2022 1 30))) true))
89101

90102
## —————————————————————————————————————————————————————————————————————————————————————————————————
91-
## Test last-Friday-of-month?
103+
## Test last-weekday-of-month?
104+
105+
(deftest "returns true when the date is the last week day of the month"
106+
(test (d/last-weekday-of-month? (d/date 2022 1 31)) true)
107+
(test (d/last-weekday-of-month? (d/date 2022 2 28)) true)
108+
(test (d/last-weekday-of-month? (d/date 2022 3 31)) true)
109+
(test (d/last-weekday-of-month? (d/date 2022 4 29)) true)
110+
(test (d/last-weekday-of-month? (d/date 2022 5 31)) true)
111+
(test (d/last-weekday-of-month? (d/date 2022 6 30)) true)
112+
(test (d/last-weekday-of-month? (d/date 2022 7 29)) true)
113+
(test (d/last-weekday-of-month? (d/date 2022 8 31)) true)
114+
(test (d/last-weekday-of-month? (d/date 2022 9 30)) true)
115+
(test (d/last-weekday-of-month? (d/date 2022 10 31)) true)
116+
(test (d/last-weekday-of-month? (d/date 2022 11 30)) true)
117+
(test (d/last-weekday-of-month? (d/date 2022 12 30)) true)
118+
(test (not (d/last-weekday-of-month? (d/date 2022 1 30))) true))
119+
120+
## —————————————————————————————————————————————————————————————————————————————————————————————————
121+
## Test last-friday-of-month?
92122

93123
(deftest "returns true when the date is the last friday of the month"
94124
(test (d/last-friday-of-month? (d/date 2022 1 28)) true)

0 commit comments

Comments
 (0)