aboutsummaryrefslogtreecommitdiff
path: root/common/src/Common/View/Format.hs
blob: 0597d1703c09eaf977bf4d1e7f0f7e2d2fb05a8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
module Common.View.Format
  ( shortDay
  , longDay
  , price
  , number
  ) where

import           Data.List          (intersperse)
import           Data.Maybe         (fromMaybe)
import           Data.Text          (Text)
import qualified Data.Text          as T
import           Data.Time.Calendar (Day, toGregorian)

import           Common.Model       (Currency (..))
import qualified Common.Msg         as Msg

shortDay :: Day -> Text
shortDay date =
  Msg.get $ Msg.Date_Short
    day
    month
    (fromIntegral year)
  where (year, month, day) = toGregorian date

longDay :: Day -> Text
longDay date =
  Msg.get $ Msg.Date_Long
    day
    (fromMaybe "−" . fmap Msg.get . monthToKey $ month)
    (fromIntegral year)
  where (year, month, day) = toGregorian date

        monthToKey 1  = Just Msg.Month_January
        monthToKey 2  = Just Msg.Month_February
        monthToKey 3  = Just Msg.Month_March
        monthToKey 4  = Just Msg.Month_April
        monthToKey 5  = Just Msg.Month_May
        monthToKey 6  = Just Msg.Month_June
        monthToKey 7  = Just Msg.Month_July
        monthToKey 8  = Just Msg.Month_August
        monthToKey 9  = Just Msg.Month_September
        monthToKey 10 = Just Msg.Month_October
        monthToKey 11 = Just Msg.Month_November
        monthToKey 12 = Just Msg.Month_December
        monthToKey _  = Nothing

price :: Currency -> Int -> Text
price (Currency currency) amount = T.concat [ number amount, " ", currency ]

number :: Int -> Text
number n =
  T.pack
  . (++) (if n < 0 then "-" else "")
  . reverse
  . concat
  . intersperse " "
  . group 3
  . reverse
  . show
  . abs $ n

group :: Int -> [a] -> [[a]]
group n xs =
  if length xs <= n
    then [xs]
    else (take n xs) : (group n (drop n xs))