mirror of
https://code.sup39.dev/repos/Wqawg
synced 2024-12-27 17:24:53 +09:00
Dedicated module for "time ago"
This commit is contained in:
parent
4dd4e1b932
commit
1c4b674550
2 changed files with 65 additions and 0 deletions
64
src/Data/EventTime/Local.hs
Normal file
64
src/Data/EventTime/Local.hs
Normal file
|
@ -0,0 +1,64 @@
|
|||
{- This file is part of Vervis.
|
||||
-
|
||||
- Written in 2016 by fr33domlover <fr33domlover@riseup.net>.
|
||||
-
|
||||
- ♡ 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
|
||||
- <http://creativecommons.org/publicdomain/zero/1.0/>.
|
||||
-}
|
||||
|
||||
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"
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue