Skip to content

Instantly share code, notes, and snippets.

@ppsdatta
Last active June 26, 2019 16:43
Show Gist options
  • Save ppsdatta/a37fbbd5e64f3b8167a6bf2e295fd928 to your computer and use it in GitHub Desktop.
Save ppsdatta/a37fbbd5e64f3b8167a6bf2e295fd928 to your computer and use it in GitHub Desktop.
A simple function to generate streams of dates based on certain params (uses simple-date-time Common Lisp library)
(defun date-stream (&key (start-date nil) (interval 1) (interval-type :day))
(let ((start (if (not start-date) (now) start-date)))
(lambda ()
(let ((current start))
(setq start (funcall (cond
((eq interval-type :day) #'day+)
((eq interval-type :month) #'month+)
((eq interval-type :year) #'year+)
(t #'day+)) start interval))
current))))
;; Usage
;;DATE-TEST> (defparameter *ds* (date-stream :interval 10))
;;*DS*
;;DATE-TEST> (funcall *ds*)
;;#<DATE-TIME 2019-06-26 21:55:26.515 {1005E4D3B3}>
;;DATE-TEST> (funcall *ds*)
;;#<DATE-TIME 2019-07-06 21:55:26.515 {1005E69D63}>
;;DATE-TEST> (funcall *ds*)
;;#<DATE-TIME 2019-07-16 21:55:26.515 {1005E6EA03}>
;;DATE-TEST> (defparameter *ds2* (date-stream :interval 1 :interval-type :month))
;;*DS2*
;;DATE-TEST> (funcall *ds2*)
;;#<DATE-TIME 2019-06-26 22:01:38.198 {100667B753}>
;;DATE-TEST> (funcall *ds2*)
;;#<DATE-TIME 2019-07-26 22:01:38.198 {100667C523}>
;;DATE-TEST> (defparameter *ds2* (date-stream :start-date (day+ (now) 4) :interval-type :month))
;;*DS2*
;;DATE-TEST> (funcall *ds2*)
;;#<DATE-TIME 2019-06-30 22:05:09.301 {1002783B93}>
;; Call lots of time till February
;;DATE-TEST> (funcall *ds2*)
;;#<DATE-TIME 2020-02-29 22:05:09.301 {100278AE13}>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment