time.scm
#lang scheme/base

(require "util.scm"
         "record.scm"
         (planet "rfc3339.ss" ("neil" "rfc3339.plt" 1 0)))

(provide created-when-str
         atom-time-str)

;; returns "" if no creation time of given rec is known
(define (created-when-str rec)
  (aif (rec-prop rec 'created_at)
       (let* ((passed (- (current-seconds) it))
              (mins (round (/ passed 60)))
              (hours (round (/ passed 3600)))
              (days (round (/ passed 86400))))
         (cond ((= mins 1) (format "~A minute ago" mins))
               ((< mins 60) (format "~A minutes ago" mins))
               ((= hours 1) (format "~A hour ago" hours))
               ((< hours 24) (format "~A hours ago" hours))
               ((= days 1) (format "~A day ago" days))
               (else (format "~A days ago" days))))
       ""))

(define (atom-time-str secs)
  (let ((d (seconds->date secs)))
    (rfc3339-record->string
     (make-rfc3339-record (date-year d) (date-month d) (date-day d)
                          (date-hour d) (date-minute d) (date-second d)
                          #f (/ (date-time-zone-offset d) 60)))))