feat: more date & time formats

This commit is contained in:
Gregor Kleen 2020-03-07 11:19:17 +01:00
parent 30b687e56f
commit 936c3666fc
2 changed files with 43 additions and 19 deletions

View File

@ -132,7 +132,7 @@ user-defaults:
max-favourite-terms: 2
theme: Default
date-time-format: "%a %d %b %Y %R"
date-format: "%d.%m.%Y"
date-format: "%a %d %b %Y"
time-format: "%R"
download-files: false
warning-days: 1209600

View File

@ -34,6 +34,8 @@ import Data.Time.Clock.System (systemEpochDay)
import qualified Data.Csv as Csv
import qualified Data.Char as Char
-------------
-- UTCTime --
@ -84,7 +86,7 @@ instance HasLocalTime TimeOfDay where
toLocalTime = LocalTime systemEpochDay
formatTime' :: (HasLocalTime t, MonadHandler m) => String -> t -> m Text
formatTime' fmtStr t = fmap fromString $ Time.formatTime <$> getTimeLocale <*> pure fmtStr <*> pure (toLocalTime t)
formatTime' fmtStr t = fmap fromString $ Time.formatTime <$> getTimeLocale <*> pure fmtStr <*> pure (utcToZonedTime . localTimeToUTCTZ appTZ $ toLocalTime t)
-- formatTime :: (FormatTime t, MonadHandler m, HandlerSite m ~ UniWorX, IsString str) => (DateTimeFormat -> String) -> t -> m str
-- Restricted type for safety
@ -123,33 +125,50 @@ getDateTimeFormat sel = do
validDateTimeFormats :: TimeLocale -> SelDateTimeFormat -> Set DateTimeFormat
-- ^ We use a whitelist instead of just letting the user specify their own format string since vulnerabilities in printf-like functions are not uncommon
validDateTimeFormats _ SelFormatDateTime = Set.fromList
[ DateTimeFormat "%a %d %b %Y %R"
, DateTimeFormat "%a %b %d %Y %R"
, DateTimeFormat "%A, %d %B %Y %R"
, DateTimeFormat "%A, %B %d %Y %R"
, DateTimeFormat "%a %d %b %Y %T"
, DateTimeFormat "%a %b %d %Y %T"
, DateTimeFormat "%A, %d %B %Y %T"
, DateTimeFormat "%A, %B %d %Y %T"
, DateTimeFormat "%d.%m.%Y %R"
, DateTimeFormat "%d.%m.%Y %T"
, DateTimeFormat "%a %d.%m.%y %R"
, DateTimeFormat "%R %d.%m.%Y"
, DateTimeFormat "%T %d.%m.%Y"
, DateTimeFormat "%Y-%m-%d %R"
, DateTimeFormat "%Y-%m-%d %T"
validDateTimeFormats tl SelFormatDateTime = Set.fromList $
[ DateTimeFormat "%Y-%m-%dT%R"
, DateTimeFormat "%Y-%m-%dT%T"
] ++
[ DateTimeFormat $ unwords [firstF, secondF]
| DateTimeFormat tFormat <- Set.toList $ validDateTimeFormats tl SelFormatTime
, DateTimeFormat dFormat <- Set.toList $ validDateTimeFormats tl SelFormatDate
, (firstF, secondF) <- [(tFormat, dFormat), (dFormat, tFormat)]
] ++
[ DateTimeFormat $ unwords [dayFmt, timeFmt, yearFmt]
| dayFmt <- [ "%a %d %b"
, "%a %b %d"
, "%A, %d %B"
, "%A, %B %d"
, "%d.%m"
, "%a %d.%m"
, "%A %d.%m"
]
, timeFmt <- [ "%R"
, "%T"
]
, yearFmt <- [ "%y", "%Y" ]
]
validDateTimeFormats _ SelFormatDate = Set.fromList
[ DateTimeFormat "%a %d %b %Y"
, DateTimeFormat "%a %b %d %Y"
, DateTimeFormat "%d %b %Y"
, DateTimeFormat "%b %d %Y"
, DateTimeFormat "%d %B %Y"
, DateTimeFormat "%B %d %Y"
, DateTimeFormat "%d %b %y"
, DateTimeFormat "%b %d %y"
, DateTimeFormat "%d %B %y"
, DateTimeFormat "%B %d %y"
, DateTimeFormat "%A, %d %B %Y"
, DateTimeFormat "%A, %B %d %Y"
, DateTimeFormat "%A, %d %b %Y"
, DateTimeFormat "%A, %b %d %Y"
, DateTimeFormat "%d.%m.%y"
, DateTimeFormat "%d.%m.%Y"
, DateTimeFormat "%a %d.%m.%y"
, DateTimeFormat "%a %d.%m.%Y"
, DateTimeFormat "%A %d.%m.%y"
, DateTimeFormat "%A %d.%m.%Y"
, DateTimeFormat "%Y-%m-%d"
, DateTimeFormat "%y-%m-%d"
]
@ -162,8 +181,13 @@ validDateTimeFormats TimeLocale{..} SelFormatTime = Set.fromList . concat . catM
guard $ uncurry (/=) amPm
Just
[ DateTimeFormat "%I:%M %p"
, DateTimeFormat "%I:%M %P"
, DateTimeFormat "%I:%M:%S %p"
]
, do
guard $ uncurry (/=) amPm
guard $ any (any $ not . Char.isLower) [fst amPm, snd amPm]
Just
[ DateTimeFormat "%I:%M %P"
, DateTimeFormat "%I:%M:%S %P"
]
]