From 6366d8d3cb8928ffd06502122dc690e158df0b7f Mon Sep 17 00:00:00 2001 From: Ricardo Wurmus Date: Tue, 12 May 2020 13:20:52 +0200 Subject: view/utils: Add time->string. --- mumi/web/view/utils.scm | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) 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 ;;; Copyright © 2018, 2019 Arun Isaac +;;; Copyright © 2018 Ludovic Courtès ;;; ;;; 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 (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)))))) -- cgit v1.2.3