Fix timeclock format parsing

Fix presumably copy-paste errors

timeclock format has only timeclock lines or empty/comment lines

Update test format to v3, add new tests

Throw error on unexpected clock codes in timeclock format

Fix missing case in pattern matching
This commit is contained in:
Jakob Schöttl 2020-01-12 11:26:30 +01:00 committed by Simon Michael
parent 65efdea4c0
commit c7a88b50fb
3 changed files with 49 additions and 9 deletions

View File

@ -57,6 +57,7 @@ instance Read TimeclockCode where
timeclockEntriesToTransactions :: LocalTime -> [TimeclockEntry] -> [Transaction] timeclockEntriesToTransactions :: LocalTime -> [TimeclockEntry] -> [Transaction]
timeclockEntriesToTransactions _ [] = [] timeclockEntriesToTransactions _ [] = []
timeclockEntriesToTransactions now [i] timeclockEntriesToTransactions now [i]
| tlcode i /= In = errorExpectedCodeButGot In i
| odate > idate = entryFromTimeclockInOut i o' : timeclockEntriesToTransactions now [i',o] | odate > idate = entryFromTimeclockInOut i o' : timeclockEntriesToTransactions now [i',o]
| otherwise = [entryFromTimeclockInOut i o] | otherwise = [entryFromTimeclockInOut i o]
where where
@ -67,6 +68,8 @@ timeclockEntriesToTransactions now [i]
o' = o{tldatetime=itime{localDay=idate, localTimeOfDay=TimeOfDay 23 59 59}} o' = o{tldatetime=itime{localDay=idate, localTimeOfDay=TimeOfDay 23 59 59}}
i' = i{tldatetime=itime{localDay=addDays 1 idate, localTimeOfDay=midnight}} i' = i{tldatetime=itime{localDay=addDays 1 idate, localTimeOfDay=midnight}}
timeclockEntriesToTransactions now (i:o:rest) timeclockEntriesToTransactions now (i:o:rest)
| tlcode i /= In = errorExpectedCodeButGot In i
| tlcode o /= Out =errorExpectedCodeButGot Out o
| odate > idate = entryFromTimeclockInOut i o' : timeclockEntriesToTransactions now (i':o:rest) | odate > idate = entryFromTimeclockInOut i o' : timeclockEntriesToTransactions now (i':o:rest)
| otherwise = entryFromTimeclockInOut i o : timeclockEntriesToTransactions now rest | otherwise = entryFromTimeclockInOut i o : timeclockEntriesToTransactions now rest
where where
@ -76,6 +79,14 @@ timeclockEntriesToTransactions now (i:o:rest)
i' = i{tldatetime=itime{localDay=addDays 1 idate, localTimeOfDay=midnight}} i' = i{tldatetime=itime{localDay=addDays 1 idate, localTimeOfDay=midnight}}
{- HLINT ignore timeclockEntriesToTransactions -} {- HLINT ignore timeclockEntriesToTransactions -}
errorExpectedCodeButGot expected actual = errorWithSourceLine line $ "expected timeclock code " ++ (show expected) ++ " but got " ++ show (tlcode actual)
where
line = case tlsourcepos actual of
GenericSourcePos _ l _ -> l
JournalSourcePos _ (l, _) -> l
errorWithSourceLine line msg = error $ "line " ++ show line ++ ": " ++ msg
-- | Convert a timeclock clockin and clockout entry to an equivalent journal -- | Convert a timeclock clockin and clockout entry to an equivalent journal
-- transaction, representing the time expenditure. Note this entry is not balanced, -- transaction, representing the time expenditure. Note this entry is not balanced,
-- since we omit the \"assets:time\" transaction for simpler output. -- since we omit the \"assets:time\" transaction for simpler output.

View File

@ -99,7 +99,7 @@ timeclockfilep = do many timeclockitemp
timeclockitemp = choice [ timeclockitemp = choice [
void (lift emptyorcommentlinep) void (lift emptyorcommentlinep)
, timeclockentryp >>= \e -> modify' (\j -> j{jparsetimeclockentries = e : jparsetimeclockentries j}) , timeclockentryp >>= \e -> modify' (\j -> j{jparsetimeclockentries = e : jparsetimeclockentries j})
] <?> "timeclock entry, or default year or historical price directive" ] <?> "timeclock entry, comment line, or empty line"
-- | Parse a timeclock entry. -- | Parse a timeclock entry.
timeclockentryp :: JournalParser m TimeclockEntry timeclockentryp :: JournalParser m TimeclockEntry

View File

@ -1,6 +1,5 @@
# 1. a timeclock session is parsed as a similarly-named transaction with one virtual posting # 1. a timeclock session is parsed as a similarly-named transaction with one virtual posting
hledger -f - print <
<<<
i 2009/1/1 08:00:00 i 2009/1/1 08:00:00
o 2009/1/1 09:00:00 stuff on checkout record is ignored o 2009/1/1 09:00:00 stuff on checkout record is ignored
@ -8,7 +7,8 @@ i 2009/1/2 08:00:00 account name
o 2009/1/2 09:00:00 o 2009/1/2 09:00:00
i 2009/1/3 08:00:00 some:account name and a description i 2009/1/3 08:00:00 some:account name and a description
o 2009/1/3 09:00:00 o 2009/1/3 09:00:00
>>> $ hledger -f - print
>
2009-01-01 * 08:00-09:00 2009-01-01 * 08:00-09:00
() 1.00h () 1.00h
@ -18,11 +18,41 @@ o 2009/1/3 09:00:00
2009-01-03 * and a description 2009-01-03 * and a description
(some:account name) 1.00h (some:account name) 1.00h
>>>2 >2
>>>= 0 >= 0
# For a missing clock-out, now is implied
<
i 2020/1/1 08:00
$ hledger -f - balance
> /./
>= 0
# For a log not starting with clock-out, print error
<
o 2020/1/1 08:00
$ hledger -f - balance
>2 /line 1: expected timeclock code i/
>= !0
# For a different log starting not with clock-out, print error
<
o 2020/1/1 08:00
o 2020/1/1 09:00
$ hledger -f - balance
>2 /line 1: expected timeclock code i/
>= !0
# For two consecutive clock-in, print error
<
i 2020/1/1 08:00
i 2020/1/1 09:00
$ hledger -f - balance
>2 /line 2: expected timeclock code o/
>= !0
## TODO ## TODO
## 2. multi-day sessions get a new transaction for each day ## multi-day sessions get a new transaction for each day
#hledger -f- print #hledger -f- print
#<<< #<<<
#i 2017/04/20 09:00:00 A #i 2017/04/20 09:00:00 A
@ -35,7 +65,7 @@ o 2009/1/3 09:00:00
#>>>2 #>>>2
#>>>= #>>>=
# #
## 3. unclosed sessions are automatically closed at report time ## unclosed sessions are automatically closed at report time
## TODO this output looks wrong ## TODO this output looks wrong
#hledger -f- print #hledger -f- print
#<<< #<<<
@ -70,7 +100,6 @@ o 2009/1/3 09:00:00
#>>>2 #>>>2
#>>>= #>>>=
# 4.
# ledger timeclock example from #ledger # ledger timeclock example from #ledger
# ==== consulting.timeclock # ==== consulting.timeclock