Skip to content

Commit 422649d

Browse files
committed
Move last-day-of-month? to date
1 parent 1470eeb commit 422649d

3 files changed

Lines changed: 39 additions & 15 deletions

File tree

src/commands/schedule_tasks.janet

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717
(defn- remove-year [formatted-date]
1818
(string/join (drop 1 (string/split "-" formatted-date)) "-"))
1919

20-
(defn- last-day-of-month? [date]
21-
(def tomorrow (date/+days date 1))
22-
(not= (date :month) (tomorrow :month)))
23-
2420
# Public
2521
(defn scheduled-for? [task date]
2622
(def formatted-date (date/format date true))
@@ -32,7 +28,7 @@
3228
(index-of (date :month) [1 4 7 10]))
3329
(string "every year on " (remove-year formatted-date)) true
3430
(string "on " formatted-date) true
35-
"every last day" (last-day-of-month? date)))
31+
"every last day" (date/last-day-of-month? date)))
3632

3733
(defn- missed-on-day [plan task date]
3834
(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/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

0 commit comments

Comments
 (0)