From 1c4b6745503625ec6b3fd7cf83b73ca74a021bdd Mon Sep 17 00:00:00 2001 From: fr33domlover Date: Tue, 3 May 2016 01:20:23 +0000 Subject: [PATCH] Dedicated module for "time ago" --- src/Data/EventTime/Local.hs | 64 +++++++++++++++++++++++++++++++++++++ vervis.cabal | 1 + 2 files changed, 65 insertions(+) create mode 100644 src/Data/EventTime/Local.hs diff --git a/src/Data/EventTime/Local.hs b/src/Data/EventTime/Local.hs new file mode 100644 index 0000000..9ef0ee3 --- /dev/null +++ b/src/Data/EventTime/Local.hs @@ -0,0 +1,64 @@ +{- This file is part of Vervis. + - + - Written in 2016 by fr33domlover . + - + - ♡ Copying is an act of love. Please copy, reuse and share. + - + - The author(s) have dedicated all copyright and related and neighboring + - rights to this software to the public domain worldwide. This software is + - distributed without any warranty. + - + - You should have received a copy of the CC0 Public Domain Dedication along + - with this software. If not, see + - . + -} + +module Data.EventTime.Local + ( TimeUnit (..) + , TimeAgo (..) + , EventTime (..) + , showEventTime + ) +where + +import Prelude + +import Data.Text (Text, snoc) + +import qualified Formatting as F + +data TimeUnit = Second | Minute | Hour | Day | Week | Month | Year + +showSingle :: TimeUnit -> Text +showSingle tu = + case tu of + Second -> "second" + Minute -> "minute" + Hour -> "hour" + Day -> "day" + Week -> "week" + Month -> "month" + Year -> "year" + +showPlural :: TimeUnit -> Text +showPlural tu = showSingle tu `snoc` 's' + +showTimeUnit :: TimeUnit -> Int -> Text +showTimeUnit tu 1 = showSingle tu +showTimeUnit tu _ = showPlural tu + +data TimeAgo = TimeAgo + { taUnit :: TimeUnit + , taCount :: Int + } + +showTimeAgo :: TimeAgo -> Text +showTimeAgo (TimeAgo u n) = + F.sformat (F.int F.% " " F.% F.stext F.% " ago") n (showTimeUnit u n) + +data EventTime = Now | Ago TimeAgo | Never + +showEventTime :: EventTime -> Text +showEventTime Now = "Now" +showEventTime (Ago ta) = showTimeAgo ta +showEventTime Never = "Never" diff --git a/vervis.cabal b/vervis.cabal index 98a8c41..fb4fafc 100644 --- a/vervis.cabal +++ b/vervis.cabal @@ -38,6 +38,7 @@ library Data.ByteString.Char8.Local Data.ByteString.Local Data.Char.Local + Data.EventTime.Local Data.Git.Local Data.List.Local Network.SSH.Local