aboutsummaryrefslogtreecommitdiff
path: root/module/datetime.scm
diff options
context:
space:
mode:
Diffstat (limited to 'module/datetime.scm')
-rw-r--r--module/datetime.scm39
1 files changed, 26 insertions, 13 deletions
diff --git a/module/datetime.scm b/module/datetime.scm
index 3b03bf53..478fc479 100644
--- a/module/datetime.scm
+++ b/module/datetime.scm
@@ -10,12 +10,10 @@
:use-module (srfi srfi-9 gnu)
:use-module ((hnh util)
- :select (vector-last define*-public set! -> swap case* set
+ :select (vector-last define*-public set! -> ->> swap case* set
span-upto let* set->))
:use-module (srfi srfi-41)
- :use-module ((srfi srfi-41 util)
- :select (with-streams))
:use-module (ice-9 i18n)
:use-module (ice-9 format)
:use-module (ice-9 regex)
@@ -67,6 +65,11 @@
(year year) (month month) (day day))
(define*-public (date key: (year 0) (month 0) (day 0))
+ (unless (and (integer? year) (integer? month) (integer? day))
+ (scm-error 'wrong-type-arg "date"
+ "Year, month, and day must all be integers. ~s, ~s, ~s"
+ (list year month day)
+ #f))
(make-date year month day))
(set-record-type-printer!
@@ -74,7 +77,7 @@
(lambda (r p)
(catch 'misc-error
(lambda () (display (date->string r "#~Y-~m-~d") p))
- (lambda (err _ fmt args . rest)
+ (lambda (err proc fmt args data)
(format p "#<<date> BAD year=~s month=~s day=~s>"
(year r) (month r) (day r))))))
@@ -535,14 +538,15 @@
(iota (modulo (- (* 7 5) month-len month-start) 7) 1)))))
+;; The amount of days in the given interval, both end pointts inclusive
(define-public (days-in-interval start-date end-date)
(let ((diff (date-difference (date+ end-date (date day: 1)) start-date)))
- (with-streams
- (fold + (day diff)
- (map days-in-month
- (take (+ (month diff)
- (* 12 (year diff)))
- (month-stream start-date)))))))
+ (->> (month-stream start-date)
+ (stream-take (+ (month diff)
+ (* 12 (year diff))))
+ (stream-map days-in-month)
+ (stream-fold + (day diff)))))
+
;; Day from start of the year, so 1 feb would be day 32.
;; Also known as Julian day.
@@ -676,6 +680,11 @@ Returns -1 on failure"
[else dt]))
(cond [(null? str)
+ ;; TODO should this be considered an error?
+ ;; Should it be toggleable through a flag.
+ ;; It's sometimes useful to allow it, since it allows optional
+ ;; trailing fields, but sometimes useful to disallow it, since
+ ;; it gives a better check that the data is valid
;; ((@ (hnh util exceptions) warning)
;; "Premature end of string, still got fmt = ~s"
;; fmt)
@@ -736,11 +745,15 @@ Returns -1 on failure"
(let* ((head post (cond ((null? (cddr fmt)) (values str '()))
((eqv? #\~ (caddr fmt))
(cond ((null? (cdddr fmt))
- (error "Unexpected ~ at end of fmt"))
+ (scm-error 'misc-error "string->datetime"
+ "Unexpected ~ at end of fmt"
+ #f #f))
((eqv? #\~ (cadddr fmt))
(span (lambda (c) (not (eqv? #\~ c)))
str))
- (else (error "Can't have format specifier directly after month by name"))))
+ (else (scm-error 'misc-error "string->datetime"
+ "Can't have format specifier directly after month by name"
+ #f #f))))
(else (span (lambda (c) (not (eqv? c (caddr fmt))))
str)))))
(loop post
@@ -1125,7 +1138,7 @@ Returns -1 on failure"
;; overflow is number of days above
;; time x time → time x int
-(define-public (time+% base change)
+(define (time+% base change)
;; while (day base) > (days-in-month base)
;; month++; days -= (days-in-month base)