{-| Module: IHP.View.TimeAgo Description: View Helpers for dealing with Time and Dates Copyright: (c) digitally induced GmbH, 2020 -} module IHP.View.TimeAgo (timeAgo, dateTime, date, time) where import IHP.Prelude import Data.Time.Format.ISO8601 (iso8601Show) import IHP.HSX.ConvertibleStrings () import Text.Blaze.Html5 (Html, (!)) import qualified Text.Blaze.Html5 as H import qualified Text.Blaze.Html5.Attributes as A -- | __Display time like @5 minutes ago@__ -- -- Render's a @\<time\>@ HTML-Element. Will be displayed like @5 minutes ago@, @1 day ago@, etc.. -- -- Requires the javascript helpers to be available. Then the time will displayed in the current browser -- timezone. -- -- When the js helper is not available, the time will be displayed with the format: DD.MM.YYYY, HH:MM -- -- __Example:__ Generated HTML -- -- >>> <div>{timeAgo (project.createdAt)}</div> -- <div><time class="time-ago">31.08.2007, 16:47</time></div> -- -- __Example:__ HTML after javascript helpers have been applied -- -- >>> <div>{timeAgo (project.createdAt)}</div> -- <div><time class="time-ago">a while ago</time></div> timeAgo :: UTCTime -> Html timeAgo :: UTCTime -> Html timeAgo = Text -> UTCTime -> Html timeElement Text "time-ago" -- | __Display time like @31.08.2007, 16:47@__ -- -- Render's a @\<time\>@ HTML-Element for displaying time and date. -- -- Requires the javascript helpers to be available. Then the date and time will displayed in the current browser -- timezone. -- -- The js helper uses `toLocaleDateString` to display the date in the browsers locale format. -- -- __Example:__ Generated HTML -- -- >>> <div>{dateTime (project.createdAt)}</div> -- <div><time class="date-time">31.08.2007, 16:47</time></div> -- -- __Example:__ HTML after javascript helpers have been applied -- -- >>> <div>{dateTime (project.createdAt)}</div> -- <div><time class="date-time">31.08.2007, 16:47 Uhr</time></div> dateTime :: UTCTime -> Html dateTime :: UTCTime -> Html dateTime = Text -> UTCTime -> Html timeElement Text "date-time" -- | __Display date like @31.08.2007@__ -- -- Render's a @\<time\>@ HTML-Element for displaying the date. -- -- Requires the javascript helpers to be available. Then the date will displayed in the current browser -- locale format and timezone. -- -- The js helper uses `toLocaleDateString` to display the date in the browsers locale format. -- -- __Example:__ Generated HTML -- -- >>> <div>{date (project.createdAt)}</div> -- <div><time class="date">31.08.2007, 16:47</time></div> -- -- __Example:__ HTML after javascript helpers have been applied -- -- >>> <div>{date (project.createdAt)}</div> -- <div><time class="date">31.08.2007</time></div> date :: UTCTime -> Html date :: UTCTime -> Html date = Text -> UTCTime -> Html timeElement Text "date" -- | __Display time like @16:47@__ -- -- Render's a @\<time\>@ HTML-Element for displaying the time. -- -- Requires the javascript helpers to be available. Then the time will displayed in the current browser -- timezone. -- -- The js helper uses `toLocaleDateString` to display the date in the browsers locale format. -- -- __Example:__ Generated HTML -- -- >>> <div>{time (project.createdAt)}</div> -- <div><time class="time">16:47</time></div> -- -- __Example:__ HTML after javascript helpers have been applied -- -- >>> <div>{time (project.createdAt)}</div> -- <div><time class="time">16:47 Uhr</time></div> time :: UTCTime -> Html time :: UTCTime -> Html time = Text -> UTCTime -> Html timeElement Text "time" timeElement :: Text -> UTCTime -> Html timeElement :: Text -> UTCTime -> Html timeElement Text className UTCTime dateTime = Html -> Html H.time (Html -> Html) -> Attribute -> Html -> Html forall h. Attributable h => h -> Attribute -> h ! AttributeValue -> Attribute A.class_ (Text -> AttributeValue forall a b. ConvertibleStrings a b => a -> b cs Text className) (Html -> Html) -> Attribute -> Html -> Html forall h. Attributable h => h -> Attribute -> h ! AttributeValue -> Attribute A.datetime (String -> AttributeValue forall a b. ConvertibleStrings a b => a -> b cs (String -> AttributeValue) -> String -> AttributeValue forall a b. (a -> b) -> a -> b $ UTCTime -> String forall t. ISO8601 t => t -> String iso8601Show UTCTime dateTime) (Html -> Html) -> Html -> Html forall a b. (a -> b) -> a -> b $ String -> Html forall a b. ConvertibleStrings a b => a -> b cs (UTCTime -> String beautifyUtcTime UTCTime dateTime) beautifyUtcTime :: UTCTime -> String beautifyUtcTime :: UTCTime -> String beautifyUtcTime UTCTime utcTime = TimeLocale -> String -> UTCTime -> String forall t. FormatTime t => TimeLocale -> String -> t -> String formatTime TimeLocale defaultTimeLocale String "%d.%m.%Y, %H:%M" UTCTime utcTime