(define-module (srfi srfi-19 alt util) :use-module (srfi srfi-19 alt) :use-module ((srfi srfi-1) :select (fold)) :use-module (srfi srfi-26) :use-module (srfi srfi-41) :use-module (util) ) (define-public (start-of-month date) (set (day date) 1)) (define-public (parse-freeform-date str) (let* (((year month day) (map string->number (string-split str #\-)))) (date year: year month: month day: day) )) (define-public (day-stream start-day) (stream-iterate (cut date+ <> #0-0-1) start-day)) (define-public (month-stream start-day) (stream-iterate (cut date+ <> #0-1-0) start-day)) (define-public (time-min a b) (if (timestring date optional: (fmt "~Y-~m-~d") key: allow-unknown?) (with-output-to-string (lambda () (fold (lambda (token state) (case state ((#\~) (case token ((#\~) (display "~")) ((#\Y) (format #t "~4'0d" (year date))) ((#\m) (format #t "~2'0d" (month date))) ((#\d) (format #t "~2'0d" (day date))) ((#\1) (format #t "~4'0d-~2'0d-~2'0d" (year date) (month date) (day date))) ((#\a) (display (week-day-name (week-day date)))) (else (unless allow-unknown? (error 'date->string "Invalid format token ~a" token)))) #f) (else (unless (char=? #\~ token) (display token)) token))) #f (string->list fmt))))) (define*-public (time->string time optional: (fmt "~H:~M:~S") key: allow-unknown?) (with-output-to-string (lambda () (fold (lambda (token state) (case state ((#\~) (case token ((#\~) (display "~")) ((#\H) (format #t "~2'0d" (hour time))) ((#\M) (format #t "~2'0d" (minute time))) ((#\S) (format #t "~2'0d" (second time))) (else (unless allow-unknown? (error 'time->string "Invalid format token ~a" token)))) #f) (else (unless (char=? #\~ token) (display token)) token))) #f (string->list fmt))))) ;; @verbatim ;; A B C D E ¬F ;; |s1| : |s2| : |s1| : |s2| : : |s1| ;; | | : | | : | ||s2| : |s1|| | : |s1||s2| : | | ;; | ||s2| : |s1|| | : | || | : | || | : | || | : ;; | | : | | : | || | : | || | : | || | : |s2| ;; | | : | | : | | : | | : : | | ;; @end verbatim ;; ;; E is covered by both case A and B. (define-public (timespan-overlaps? s1-begin s1-end s2-begin s2-end) "Return whetever or not two timespans overlap." (or ;; A (and (date/-timestring start-date) ;; (date->string date) ;; (date->string end-date) ;; (date<= start-date date end-date) ) (date<= start-date date end-date)))