-(define (ago unit amount)
- "Return the point in time that lies AMOUNT UNITs in the past."
- (let ((amount* (match unit
- ('hours amount)
- ('days (* 24 amount))
- ('weeks (* 24 7 amount))
- ('months (* 24 30 amount))
- ('years (* 24 365 amount)))))
- (subtract-duration (date->time-utc (current-date))
- (make-time time-duration 0 (* 60 60 amount*)))))
-
-(define (date-term->epoch-seconds term)
- "Convert a date search term string into seconds since the epoch, or
-return #F if the term is invalid."
- (match term
- ("now" 'now)
- ("today" (time-second (ago 'days 1)))
- ("yesterday" (time-second (ago 'days 2)))
- (_
- (cond
- ;; TODO: support more date template strings
- ((or (false-if-exception (string->date term "~Y~m~d"))
- (false-if-exception (string->date term "~Y-~m-~d")))
- => (lambda (date)
- (time-second (date->time-utc date))))
- ;; e.g. "12h" meaning "12 hours ago"
- ((string->number (string-drop-right term 1))
- => (lambda (amount)
- (match (string-take-right term 1)
- ("h"
- (time-second (ago 'hours amount)))
- ("d"
- (time-second (ago 'days amount)))
- ("w"
- (time-second (ago 'weeks amount)))
- ("m"
- (time-second (ago 'months amount)))
- ("y"
- (time-second (ago 'years amount)))
- (_ #f))))
- (else #f)))))