Fixes unformatted UTCTime displays; DisplayAble no longer available for all Show Types

This commit is contained in:
SJost 2018-09-17 15:35:14 +02:00
parent 936bbc28ac
commit c684692cc2
5 changed files with 29 additions and 5 deletions

View File

@ -8,8 +8,7 @@
module Handler.Utils.DateTime module Handler.Utils.DateTime
( utcToLocalTime ( utcToLocalTime
, localTimeToUTC, TZ.LocalToUTCResult(..) , localTimeToUTC, TZ.LocalToUTCResult(..)
, formatTime' , formatTime, formatTime', formatTimeW
, formatTime
, getTimeLocale, getDateTimeFormat , getTimeLocale, getDateTimeFormat
, validDateTimeFormats, dateTimeFormatOptions , validDateTimeFormats, dateTimeFormatOptions
) where ) where
@ -51,6 +50,12 @@ formatTime' fmtStr t = fmap fromString $ Time.formatTime <$> getTimeLocale <*> p
formatTime :: (HasLocalTime t, MonadHandler m, HandlerSite m ~ UniWorX) => SelDateTimeFormat -> t -> m Text formatTime :: (HasLocalTime t, MonadHandler m, HandlerSite m ~ UniWorX) => SelDateTimeFormat -> t -> m Text
formatTime proj t = flip formatTime' t =<< (unDateTimeFormat <$> getDateTimeFormat proj) formatTime proj t = flip formatTime' t =<< (unDateTimeFormat <$> getDateTimeFormat proj)
-- formatTimeH :: (HasLocalTime t) => SelDateTimeFormat -> t -> Handler Text
-- formatTimeH = formatTime
formatTimeW :: (HasLocalTime t) => SelDateTimeFormat -> t -> Widget
formatTimeW s t = toWidget =<< formatTime s t
getTimeLocale :: (MonadHandler m, HandlerSite m ~ UniWorX) => m TimeLocale getTimeLocale :: (MonadHandler m, HandlerSite m ~ UniWorX) => m TimeLocale
getTimeLocale = getTimeLocale' <$> languages getTimeLocale = getTimeLocale' <$> languages

View File

@ -88,6 +88,8 @@ pToI = fromPoints
fromPoints :: Integral a => Points -> a -- deprecated fromPoints :: Integral a => Points -> a -- deprecated
fromPoints = round fromPoints = round
instance DisplayAble Points
data SheetType data SheetType
= Bonus { maxPoints :: Points } = Bonus { maxPoints :: Points }
| Normal { maxPoints :: Points } | Normal { maxPoints :: Points }
@ -237,6 +239,8 @@ seasonFromChar c
where where
(~=) = (==) `on` CI.mk (~=) = (==) `on` CI.mk
instance DisplayAble Season
data TermIdentifier = TermIdentifier data TermIdentifier = TermIdentifier
{ year :: Integer -- ^ Using 'Integer' to model years is consistent with 'Data.Time.Calendar' { year :: Integer -- ^ Using 'Integer' to model years is consistent with 'Data.Time.Calendar'
, season :: Season , season :: Season
@ -337,6 +341,8 @@ data StudyFieldType = FieldPrimary | FieldSecondary
deriving (Eq, Ord, Enum, Show, Read, Bounded) deriving (Eq, Ord, Enum, Show, Read, Bounded)
derivePersistField "StudyFieldType" derivePersistField "StudyFieldType"
instance DisplayAble StudyFieldType
data Theme data Theme
= ThemeDefault = ThemeDefault
| ThemeLavender | ThemeLavender

View File

@ -1,5 +1,6 @@
{-# LANGUAGE NoImplicitPrelude #-} {-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE RankNTypes #-} {-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeSynonymInstances, FlexibleInstances, UndecidableInstances #-} {-# LANGUAGE TypeSynonymInstances, FlexibleInstances, UndecidableInstances #-}
@ -128,6 +129,9 @@ withFragment form html = (flip fmap) form $ \(x, widget) -> (x, toWidget html >>
-- Convert anything to Text, and I don't care how -- Convert anything to Text, and I don't care how
class DisplayAble a where class DisplayAble a where
display :: a -> Text display :: a -> Text
-- Default definitions for type belonging to Show (allows empty instanc declarations)
default display :: Show a => a -> Text
display = pack . show
instance DisplayAble Text where instance DisplayAble Text where
display = id display = id
@ -139,6 +143,10 @@ instance DisplayAble a => DisplayAble (Maybe a) where
display Nothing = "" display Nothing = ""
display (Just x) = display x display (Just x) = display x
instance DisplayAble Int
instance DisplayAble Int64
instance DisplayAble Integer
instance DisplayAble Rational where instance DisplayAble Rational where
display r = showFFloat (Just 2) (rat2float r) "" display r = showFFloat (Just 2) (rat2float r) ""
& pack & pack
@ -154,10 +162,15 @@ instance DisplayAble a => DisplayAble (E.Value a) where
instance DisplayAble a => DisplayAble (CI a) where instance DisplayAble a => DisplayAble (CI a) where
display = display . CI.original display = display . CI.original
{- We do not want DisplayAble for every Show-Class, we want to check that it looks good and explicitely add Instances only,
for example, UTCTime values were shown without proper rendering!
-- The easy way out of UndecidableInstances (TypeFamilies would have been proper, but are much more complicated) -- The easy way out of UndecidableInstances (TypeFamilies would have been proper, but are much more complicated)
instance {-# OVERLAPPABLE #-} Show a => DisplayAble a where instance {-# OVERLAPPABLE #-} Show a => DisplayAble a where
display = pack . show display = pack . show
-}
textPercent :: Double -> Text -- slow, maybe use Data.Double.Conversion.Text.toFixed instead? textPercent :: Double -> Text -- slow, maybe use Data.Double.Conversion.Text.toFixed instead?
textPercent x = lz <> (pack $ show rx) <> "%" textPercent x = lz <> (pack $ show rx) <> "%"
where where

View File

@ -10,7 +10,7 @@
$maybe time <- submissionRatingTime $maybe time <- submissionRatingTime
<tr .table__row> <tr .table__row>
<th .table__th>_{MsgRatingTime} <th .table__th>_{MsgRatingTime}
<td .table__td>#{display time} <td .table__td>^{formatTimeW SelFormatDateTime time}
$maybe points <- submissionRatingPoints $maybe points <- submissionRatingPoints
$case sheetType $case sheetType
$of Bonus{..} $of Bonus{..}

View File

@ -59,10 +59,10 @@
<dt .deflist__dt> Teilnehmer <dt .deflist__dt> Teilnehmer
<dd .deflist__dd> <dd .deflist__dd>
<dl .deflist> <dl .deflist>
$forall (E.Value tid, E.Value ssh, E.Value csh, regSince) <- participant $forall (E.Value tid, E.Value ssh, E.Value csh, E.Value regSince) <- participant
<dt .deflist__dt> <dt .deflist__dt>
<a href=@{CourseR tid ssh csh CShowR}>#{display tid}-#{display ssh}-#{display csh} <a href=@{CourseR tid ssh csh CShowR}>#{display tid}-#{display ssh}-#{display csh}
<dd .deflist__dd> <dd .deflist__dd>
seit #{display regSince} seit ^{formatTimeW SelFormatDateTime regSince}
^{settingsForm} ^{settingsForm}