diff options
-rw-r--r-- | mumi/web/view/utils.scm | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/mumi/web/view/utils.scm b/mumi/web/view/utils.scm index 0f037c3..3695045 100644 --- a/mumi/web/view/utils.scm +++ b/mumi/web/view/utils.scm @@ -1,6 +1,7 @@ ;;; mumi -- Mediocre, uh, mail interface ;;; Copyright © 2017, 2018, 2019, 2020 Ricardo Wurmus <rekado@elephly.net> ;;; Copyright © 2018, 2019 Arun Isaac <arunisaac@systemreboot.net> +;;; Copyright © 2018 Ludovic Courtès <ludo@gnu.org> ;;; ;;; This program is free software: you can redistribute it and/or ;;; modify it under the terms of the GNU Affero General Public License @@ -23,6 +24,7 @@ #:use-module (ice-9 iconv) #:use-module (srfi srfi-1) #:use-module (srfi srfi-9) + #:use-module (srfi srfi-19) #:use-module (srfi srfi-26) #:use-module (syntax-highlight) #:use-module (syntax-highlight scheme) @@ -31,7 +33,8 @@ #:use-module (rnrs bytevectors) #:export (prettify avatar-color - display-message-body)) + display-message-body + time->string)) (define-record-type <block> (make-block type lines) @@ -268,3 +271,43 @@ BUG-NUM), even when it is a multipart message." (make-mime-entity (email-headers message) (email-body message)))))) +(define (time->string time) + "Return a string representing TIME in a concise, human-readable way." + (define seconds + (time-second + (if (date? time) + (date->time-utc time) + time))) + + (define now* + (current-time time-utc)) + + (define now + (time-second now*)) + + (define elapsed + (- now seconds)) + + (cond ((< elapsed 120) + "seconds ago") + ((< elapsed 7200) + (let ((minutes (inexact->exact + (round (/ elapsed 60))))) + (format #f "~a minutes ago" minutes))) + ((< elapsed (* 48 3600)) + (let ((hours (inexact->exact + (round (/ elapsed 3600))))) + (format #f "~a hours ago" hours))) + ((< elapsed (* 3600 24 7)) + (let ((days (inexact->exact + (round (/ elapsed 3600 24))))) + (format #f "~a days ago" days))) + (else + (let* ((time (make-time time-utc 0 seconds)) + (date (time-utc->date time)) + (year (date-year date)) + (current (date-year (time-utc->date now*))) + (format (if (= year current) + "~e ~b ~H:~M ~z" + "~e ~b ~Y ~H:~M"))) + (string-append "on " (date->string date format)))))) |