Skip to content

Commit c4f6557

Browse files
authored
Merge pull request #108 from hackberrydev/schedule-on-last-day
Schedule on last day
2 parents 160f496 + 422649d commit c4f6557

5 files changed

Lines changed: 63 additions & 15 deletions

File tree

src/commands/schedule_tasks.janet

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"every 3 months" (and (= (date :day) 1)
2828
(index-of (date :month) [1 4 7 10]))
2929
(string "every year on " (remove-year formatted-date)) true
30-
(string "on " formatted-date) true))
30+
(string "on " formatted-date) true
31+
"every last day" (date/last-day-of-month? date)))
3132

3233
(defn- missed-on-day [plan task date]
3334
(find (fn [day] (and (scheduled-for? task (day :date))

src/date.janet

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,17 @@
8686
:day (+ (today :month-day) 1)
8787
:week-day (week-day-string (today :week-day))}))
8888

89+
(defn +days [date n]
90+
(def new-date-time (+ (to-time date) (* n seconds-in-day)))
91+
(from-os-date-struct (os/date new-date-time)))
92+
93+
(defn -days [date n]
94+
(def new-date-time (- (to-time date) (* n seconds-in-day)))
95+
(from-os-date-struct (os/date new-date-time)))
96+
97+
(defn days-from-now [n]
98+
(+days (today) n))
99+
89100
(defn equal?
90101
[d1 d2]
91102
(= (to-time d1) (to-time d2)))
@@ -112,13 +123,10 @@
112123
[d1 d2]
113124
(>= (to-time d1) (to-time d2)))
114125

115-
(defn +days [date n]
116-
(def new-date-time (+ (to-time date) (* n seconds-in-day)))
117-
(from-os-date-struct (os/date new-date-time)))
118-
119-
(defn -days [date n]
120-
(def new-date-time (- (to-time date) (* n seconds-in-day)))
121-
(from-os-date-struct (os/date new-date-time)))
122-
123-
(defn days-from-now [n]
124-
(+days (today) n))
126+
(defn last-day-of-month?
127+
```
128+
Returns true if date is the last day of a month.
129+
```
130+
[date]
131+
(def tomorrow (+days date 1))
132+
(not= (date :month) (tomorrow :month)))

test/commands/schedule_tasks_test.janet

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,23 @@
6666
(is (not (scheduled-for? task (d/date 2022 2 1))))
6767
(is (not (scheduled-for? task (d/date 2023 1 27)))))
6868

69+
(deftest scheduled-for-last-day-of-month
70+
(def task (task/build-scheduled-task "Review logs" "every last day"))
71+
(is (scheduled-for? task (d/date 2022 1 31)))
72+
(is (scheduled-for? task (d/date 2022 2 28)))
73+
(is (scheduled-for? task (d/date 2022 3 31)))
74+
(is (scheduled-for? task (d/date 2022 4 30)))
75+
(is (scheduled-for? task (d/date 2022 5 31)))
76+
(is (scheduled-for? task (d/date 2022 6 30)))
77+
(is (scheduled-for? task (d/date 2022 7 31)))
78+
(is (scheduled-for? task (d/date 2022 8 31)))
79+
(is (scheduled-for? task (d/date 2022 9 30)))
80+
(is (scheduled-for? task (d/date 2022 10 31)))
81+
(is (scheduled-for? task (d/date 2022 11 30)))
82+
(is (scheduled-for? task (d/date 2022 12 31)))
83+
(is (scheduled-for? task (d/date 2023 1 31)))
84+
(is (not (scheduled-for? task (d/date 2022 1 30)))))
85+
6986
## —————————————————————————————————————————————————————————————————————————————————————————————————
7087
## Test missed?
7188

test/date_test.janet

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,26 @@
6868
(is (d/after-or-eq? (d/date 2021 7 1) (d/date 2021 7 1)))
6969
(is (not (d/after-or-eq? (d/date 2021 7 1) (d/date 2021 7 15)))))
7070

71+
## ————————————————————————————————————————————————————————————————————————————————————————————————
72+
## Test last-day-of-month?
73+
74+
(deftest last-day-of-month?
75+
(is (d/last-day-of-month? (d/date 2022 1 31)))
76+
(is (d/last-day-of-month? (d/date 2022 2 28)))
77+
(is (d/last-day-of-month? (d/date 2022 3 31)))
78+
(is (d/last-day-of-month? (d/date 2022 4 30)))
79+
(is (d/last-day-of-month? (d/date 2022 5 31)))
80+
(is (d/last-day-of-month? (d/date 2022 6 30)))
81+
(is (d/last-day-of-month? (d/date 2022 7 31)))
82+
(is (d/last-day-of-month? (d/date 2022 8 31)))
83+
(is (d/last-day-of-month? (d/date 2022 9 30)))
84+
(is (d/last-day-of-month? (d/date 2022 10 31)))
85+
(is (d/last-day-of-month? (d/date 2022 11 30)))
86+
(is (d/last-day-of-month? (d/date 2022 12 31)))
87+
(is (d/last-day-of-month? (d/date 2023 1 31)))
88+
(is (not (d/last-day-of-month? (d/date 2022 1 30)))))
89+
90+
7191
## ————————————————————————————————————————————————————————————————————————————————————————————————
7292
## Test +days
7393

test/schedule_parser_test.janet

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,24 @@
1616
- Pay football practice (every month)
1717
- Martha's birthday (every 05-24)
1818
- Meeting with Jack (on 2022-05-03)
19+
- Review logs (every last day)
1920
```)
2021
(def result (schedule_parser/parse schedule-string))
2122
(def scheduled-tasks (result :tasks))
22-
(is (= 6 (length scheduled-tasks)))
23+
(is (= 7 (length scheduled-tasks)))
2324
(let [task (scheduled-tasks 0)]
2425
(is (= "Weekly Meeting" (task :title)))
2526
(is (= false (task :done)))
2627
(is (= "every Tuesday" (task :schedule))))
2728
(let [task (scheduled-tasks 1)]
2829
(is (= "Puzzle Storm on Lichess" (task :title)))
29-
(is (= false (task :done)))
3030
(is (= "every day" (task :schedule))))
3131
(let [task (scheduled-tasks 5)]
3232
(is (= "Meeting with Jack" (task :title)))
33-
(is (= false (task :done)))
34-
(is (= "on 2022-05-03" (task :schedule)))))
33+
(is (= "on 2022-05-03" (task :schedule))))
34+
(let [task (scheduled-tasks 6)]
35+
(is (= "Review logs" (task :title)))
36+
(is (= "every last day" (task :schedule)))))
3537

3638
(deftest parse-schedule-when-schedule-can-not-be-parsed
3739
(def schedule-string

0 commit comments

Comments
 (0)