aboutsummaryrefslogtreecommitdiff
path: root/common/src/Common/View/Format.hs
diff options
context:
space:
mode:
authorJoris2017-11-08 23:47:26 +0100
committerJoris2017-11-08 23:47:26 +0100
commit27e11b20b06f2f2dbfb56c0998a63169b4b8abc4 (patch)
tree845f54d7fe876c9a3078036975ba85ec21d224a1 /common/src/Common/View/Format.hs
parenta3601b5e6f5a3e41fa31752a2c704ccd3632790e (diff)
downloadbudget-27e11b20b06f2f2dbfb56c0998a63169b4b8abc4.tar.gz
budget-27e11b20b06f2f2dbfb56c0998a63169b4b8abc4.tar.bz2
budget-27e11b20b06f2f2dbfb56c0998a63169b4b8abc4.zip
Use a better project structure
Diffstat (limited to 'common/src/Common/View/Format.hs')
-rw-r--r--common/src/Common/View/Format.hs69
1 files changed, 69 insertions, 0 deletions
diff --git a/common/src/Common/View/Format.hs b/common/src/Common/View/Format.hs
new file mode 100644
index 0000000..7165965
--- /dev/null
+++ b/common/src/Common/View/Format.hs
@@ -0,0 +1,69 @@
+{-# LANGUAGE OverloadedStrings #-}
+
+module Common.View.Format
+ ( shortDay
+ , longDay
+ , price
+ , number
+ ) where
+
+import Data.Text (Text)
+import qualified Data.Text as T
+import Data.List (intersperse)
+import Data.Maybe (fromMaybe)
+import Data.Time.Calendar (Day, toGregorian)
+
+import qualified Common.Message as Message
+import qualified Common.Message.Key as Key
+import Common.Model (Currency(..))
+
+shortDay :: Day -> Text
+shortDay date =
+ Message.get $ Key.Date_Short
+ day
+ month
+ (fromIntegral year)
+ where (year, month, day) = toGregorian date
+
+longDay :: Day -> Text
+longDay date =
+ Message.get $ Key.Date_Long
+ day
+ (fromMaybe "−" . fmap Message.get . monthToKey $ month)
+ (fromIntegral year)
+ where (year, month, day) = toGregorian date
+
+ monthToKey 1 = Just Key.Month_January
+ monthToKey 2 = Just Key.Month_February
+ monthToKey 3 = Just Key.Month_March
+ monthToKey 4 = Just Key.Month_April
+ monthToKey 5 = Just Key.Month_May
+ monthToKey 6 = Just Key.Month_June
+ monthToKey 7 = Just Key.Month_July
+ monthToKey 8 = Just Key.Month_August
+ monthToKey 9 = Just Key.Month_September
+ monthToKey 10 = Just Key.Month_October
+ monthToKey 11 = Just Key.Month_November
+ monthToKey 12 = Just Key.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))