summaryrefslogtreecommitdiffstats
path: root/app/Import/Ing/Shared.hs
diff options
context:
space:
mode:
Diffstat (limited to 'app/Import/Ing/Shared.hs')
-rw-r--r--app/Import/Ing/Shared.hs49
1 files changed, 23 insertions, 26 deletions
diff --git a/app/Import/Ing/Shared.hs b/app/Import/Ing/Shared.hs
index c70f225..b5d1703 100644
--- a/app/Import/Ing/Shared.hs
+++ b/app/Import/Ing/Shared.hs
@@ -13,35 +13,32 @@ import Data.Time.Zones (TZ, localTimeToUTCTZ)
13 13
14data DebitCredit = Debit | Credit deriving (Show) 14data DebitCredit = Debit | Credit deriving (Show)
15 15
16readDecimal :: T.Text -> Either String Decimal
17readDecimal = AP.parseOnly $ do
18 decPart <- AP.decimal
19 _ <- AP.char ','
20 f1 <- AP.digit
21 f2 <- AP.digit
22 AP.endOfInput
23 let fracPart = fromIntegral $ digitToInt f1 * 10 + digitToInt f2
24 return $ normalizeDecimal (Decimal 2 (decPart * 100 + fracPart))
25
26scsvOptions :: C.DecodeOptions 16scsvOptions :: C.DecodeOptions
27scsvOptions = C.defaultDecodeOptions {C.decDelimiter = fromIntegral (ord ';')} 17scsvOptions = C.defaultDecodeOptions {C.decDelimiter = fromIntegral (ord ';')}
28 18
29eitherToCP :: Either String a -> C.Parser a
30eitherToCP = either fail return
31
32decimalCP :: T.Text -> C.Parser Decimal
33decimalCP = eitherToCP . readDecimal
34
35dateCP :: String -> T.Text -> C.Parser Day
36dateCP fmt = parseTimeM False defaultTimeLocale fmt . T.unpack
37
38maybeCP :: (T.Text -> C.Parser a) -> T.Text -> C.Parser (Maybe a) 19maybeCP :: (T.Text -> C.Parser a) -> T.Text -> C.Parser (Maybe a)
39maybeCP p t = if T.null t then return Nothing else Just <$> p t 20maybeCP p t = if T.null t then return Nothing else Just <$> p t
40 21
41ibanCP :: T.Text -> C.Parser Iban 22parseDecimalM :: (MonadFail m) => T.Text -> m Decimal
42ibanCP = eitherToCP . mkIban 23parseDecimalM =
43 24 either fail return
44timestampCP :: String -> TZ -> T.Text -> C.Parser UTCTime 25 . AP.parseOnly
45timestampCP fmt amsTz t = do 26 ( do
46 localTime <- parseTimeM False defaultTimeLocale fmt (T.unpack t) 27 decPart <- AP.decimal
47 return $ localTimeToUTCTZ amsTz localTime 28 _ <- AP.char ','
29 f1 <- AP.digit
30 f2 <- AP.digit
31 AP.endOfInput
32 let fracPart = fromIntegral $ digitToInt f1 * 10 + digitToInt f2
33 return $ normalizeDecimal (Decimal 2 (decPart * 100 + fracPart))
34 )
35
36parseIbanM :: (MonadFail m) => T.Text -> m Iban
37parseIbanM = either fail return . mkIban
38
39parseDateM :: (MonadFail m) => String -> T.Text -> m Day
40parseDateM fmt = parseTimeM False defaultTimeLocale fmt . T.unpack
41
42parseTimestampM :: (MonadFail m) => String -> TZ -> T.Text -> m UTCTime
43parseTimestampM fmt amsTz t = do
44 localTimeToUTCTZ amsTz <$> parseTimeM False defaultTimeLocale fmt (T.unpack t)