aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2020-03-31 00:38:41 +0200
committerHugo Hörnquist <hugo@lysator.liu.se>2020-03-31 00:38:41 +0200
commita642c05620e33ca89ecc7d8fe1ccf76d7e07df46 (patch)
treedd03d223879dd4c744afbecddd7a651ec2e037c5
parentRedefine all <date>-stream in terms of general date-stream. (diff)
downloadcalp-a642c05620e33ca89ecc7d8fe1ccf76d7e07df46.tar.gz
calp-a642c05620e33ca89ecc7d8fe1ccf76d7e07df46.tar.xz
Add {start,end}-of-week.
-rw-r--r--module/datetime/util.scm16
1 files changed, 16 insertions, 0 deletions
diff --git a/module/datetime/util.scm b/module/datetime/util.scm
index 46f9bbbc..0d4699c6 100644
--- a/module/datetime/util.scm
+++ b/module/datetime/util.scm
@@ -270,6 +270,22 @@
week-start)
7))
+;; given start of month, returns the date that week started on.
+;; (start-of-week #2020-05-01 mon)
+;; => 2020-04-27
+;; TODO update these two week-methods to do what they ostensibly do.
+(define*-public (start-of-week date optional: (week-start mon))
+ (let* ((prev-month-len (days-in-month (month- date)))
+ (month-start (modulo (- (week-day date) week-start) 7)))
+ (set (day (month- date)) (1+ (- prev-month-len month-start)))))
+
+;; (end-of-week #2020-04-01 mon)
+;; => 2020-05-03
+(define*-public (end-of-week date optional: (week-start mon))
+ (let* ((month-len (days-in-month date))
+ (month-start (modulo (- (week-day date) week-start) 7)))
+ (set (day (month+ date)) (modulo (- (* 7 5) month-len month-start) 7))))
+
;; Given a month and and which day the week starts on,
;; returns three lists, which are: