Skip to content

Commit da53fb1

Browse files
authored
Merge pull request #138 from hackberrydev/every-last-weekday
Command for scheduling task on every last week day
2 parents 06e0732 + e90f3a7 commit da53fb1

5 files changed

Lines changed: 70 additions & 3 deletions

File tree

src/commands/schedule_tasks.janet

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
(string "every year on " (remove-year formatted-date)) true
3131
(string "on " formatted-date) true
3232
"every last day" (date/last-day-of-month? date)
33+
"every last weekday" (date/last-weekday-of-month? date)
3334
"every last Friday" (date/last-friday-of-month? date)))
3435

3536
(defn- missed-on-day [plan task date]

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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,22 @@
8383
(test (scheduled-for? task (d/date 2023 1 31)) true)
8484
(test (not (scheduled-for? task (d/date 2022 1 30))) true))
8585

86+
(deftest "every last weekday"
87+
(def task (task/build-scheduled-task 42 "Review logs" "every last weekday"))
88+
(test (scheduled-for? task (d/date 2022 1 31)) true)
89+
(test (scheduled-for? task (d/date 2022 2 28)) true)
90+
(test (scheduled-for? task (d/date 2022 3 31)) true)
91+
(test (scheduled-for? task (d/date 2022 4 29)) true)
92+
(test (scheduled-for? task (d/date 2022 5 31)) true)
93+
(test (scheduled-for? task (d/date 2022 6 30)) true)
94+
(test (scheduled-for? task (d/date 2022 7 29)) true)
95+
(test (scheduled-for? task (d/date 2022 8 31)) true)
96+
(test (scheduled-for? task (d/date 2022 9 30)) true)
97+
(test (scheduled-for? task (d/date 2022 10 31)) true)
98+
(test (scheduled-for? task (d/date 2022 11 30)) true)
99+
(test (scheduled-for? task (d/date 2022 12 30)) true)
100+
(test (not (scheduled-for? task (d/date 2022 1 30))) true))
101+
86102
(deftest "every last Friday"
87103
(def task (task/build-scheduled-task 42 "Review logs" "every last Friday"))
88104
(test (scheduled-for? task (d/date 2022 1 28)) 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)

test/schedule_parser_test.janet

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
- Martha's birthday (every 05-24)
1919
- Meeting with Jack (on 2022-05-03)
2020
- Review logs (every last day)
21+
- Send invoice (every last weekday)
2122
```)
2223
(def result (schedule_parser/parse schedule-string))
2324
(def scheduled-tasks (result :tasks))
24-
(test (length scheduled-tasks) 7)
25+
(test (length scheduled-tasks) 8)
2526
(let [task (scheduled-tasks 0)]
2627
(test (task :title) "Weekly Meeting")
2728
(test (task :done) false)
@@ -34,7 +35,10 @@
3435
(test (task :schedule) "on 2022-05-03"))
3536
(let [task (scheduled-tasks 6)]
3637
(test (task :title) "Review logs")
37-
(test (task :schedule) "every last day")))
38+
(test (task :schedule) "every last day"))
39+
(let [task (scheduled-tasks 7)]
40+
(test (task :title) "Send invoice")
41+
(test (task :schedule) "every last weekday")))
3842

3943
(deftest "returns an error when the schedule can't be parsed"
4044
(def schedule-string

0 commit comments

Comments
 (0)