2008-10-03 06:04:15 +04:00
|
|
|
{-|
|
|
|
|
|
2009-04-05 03:15:42 +04:00
|
|
|
A 'TimeLogEntry' is a clock-in, clock-out, or other directive in a timelog
|
|
|
|
file (see timeclock.el or the command-line version). These can be
|
2009-12-16 11:07:26 +03:00
|
|
|
converted to 'Transactions' and queried like a ledger.
|
2008-10-03 06:04:15 +04:00
|
|
|
|
|
|
|
-}
|
|
|
|
|
2010-05-20 03:08:53 +04:00
|
|
|
module Hledger.Data.TimeLog
|
2007-03-12 10:40:33 +03:00
|
|
|
where
|
2011-05-28 08:11:44 +04:00
|
|
|
import Data.Maybe
|
|
|
|
import Data.Time.Calendar
|
|
|
|
import Data.Time.Clock
|
2010-05-22 23:00:20 +04:00
|
|
|
import Data.Time.Format
|
2011-05-28 08:11:44 +04:00
|
|
|
import Data.Time.LocalTime
|
2010-05-22 23:00:20 +04:00
|
|
|
import System.Locale (defaultTimeLocale)
|
2011-05-28 08:11:44 +04:00
|
|
|
import Test.HUnit
|
|
|
|
import Text.Printf
|
|
|
|
|
|
|
|
import Hledger.Utils
|
2010-05-20 03:08:53 +04:00
|
|
|
import Hledger.Data.Types
|
|
|
|
import Hledger.Data.Dates
|
|
|
|
import Hledger.Data.Commodity
|
|
|
|
import Hledger.Data.Transaction
|
2007-03-12 10:40:33 +03:00
|
|
|
|
|
|
|
instance Show TimeLogEntry where
|
2008-11-11 15:34:05 +03:00
|
|
|
show t = printf "%s %s %s" (show $ tlcode t) (show $ tldatetime t) (tlcomment t)
|
2007-03-12 10:40:33 +03:00
|
|
|
|
2009-04-04 00:04:51 +04:00
|
|
|
instance Show TimeLogCode where
|
|
|
|
show SetBalance = "b"
|
|
|
|
show SetRequiredHours = "h"
|
|
|
|
show In = "i"
|
|
|
|
show Out = "o"
|
|
|
|
show FinalOut = "O"
|
|
|
|
|
|
|
|
instance Read TimeLogCode where
|
|
|
|
readsPrec _ ('b' : xs) = [(SetBalance, xs)]
|
|
|
|
readsPrec _ ('h' : xs) = [(SetRequiredHours, xs)]
|
|
|
|
readsPrec _ ('i' : xs) = [(In, xs)]
|
|
|
|
readsPrec _ ('o' : xs) = [(Out, xs)]
|
|
|
|
readsPrec _ ('O' : xs) = [(FinalOut, xs)]
|
|
|
|
readsPrec _ _ = []
|
|
|
|
|
2010-07-13 10:30:06 +04:00
|
|
|
-- | Convert time log entries to journal transactions. When there is no
|
2009-01-26 23:22:42 +03:00
|
|
|
-- clockout, add one with the provided current time. Sessions crossing
|
2009-03-15 15:42:03 +03:00
|
|
|
-- midnight are split into days to give accurate per-day totals.
|
2010-05-22 23:00:20 +04:00
|
|
|
timeLogEntriesToTransactions :: LocalTime -> [TimeLogEntry] -> [Transaction]
|
|
|
|
timeLogEntriesToTransactions _ [] = []
|
|
|
|
timeLogEntriesToTransactions now [i]
|
|
|
|
| odate > idate = entryFromTimeLogInOut i o' : timeLogEntriesToTransactions now [i',o]
|
2009-03-15 15:42:03 +03:00
|
|
|
| otherwise = [entryFromTimeLogInOut i o]
|
|
|
|
where
|
2009-04-04 00:04:51 +04:00
|
|
|
o = TimeLogEntry Out end ""
|
2009-04-01 09:00:08 +04:00
|
|
|
end = if itime > now then itime else now
|
2009-03-15 15:42:03 +03:00
|
|
|
(itime,otime) = (tldatetime i,tldatetime o)
|
|
|
|
(idate,odate) = (localDay itime,localDay otime)
|
|
|
|
o' = o{tldatetime=itime{localDay=idate, localTimeOfDay=TimeOfDay 23 59 59}}
|
|
|
|
i' = i{tldatetime=itime{localDay=addDays 1 idate, localTimeOfDay=midnight}}
|
2010-05-22 23:00:20 +04:00
|
|
|
timeLogEntriesToTransactions now (i:o:rest)
|
|
|
|
| odate > idate = entryFromTimeLogInOut i o' : timeLogEntriesToTransactions now (i':o:rest)
|
|
|
|
| otherwise = entryFromTimeLogInOut i o : timeLogEntriesToTransactions now rest
|
2009-01-26 23:22:42 +03:00
|
|
|
where
|
|
|
|
(itime,otime) = (tldatetime i,tldatetime o)
|
|
|
|
(idate,odate) = (localDay itime,localDay otime)
|
|
|
|
o' = o{tldatetime=itime{localDay=idate, localTimeOfDay=TimeOfDay 23 59 59}}
|
|
|
|
i' = i{tldatetime=itime{localDay=addDays 1 idate, localTimeOfDay=midnight}}
|
2007-03-12 10:40:33 +03:00
|
|
|
|
2010-07-13 10:30:06 +04:00
|
|
|
-- | Convert a timelog clockin and clockout entry to an equivalent journal
|
|
|
|
-- transaction, representing the time expenditure. Note this entry is not balanced,
|
2008-10-15 23:11:06 +04:00
|
|
|
-- since we omit the \"assets:time\" transaction for simpler output.
|
2009-12-16 11:07:26 +03:00
|
|
|
entryFromTimeLogInOut :: TimeLogEntry -> TimeLogEntry -> Transaction
|
2008-12-11 04:35:07 +03:00
|
|
|
entryFromTimeLogInOut i o
|
2009-04-03 14:58:05 +04:00
|
|
|
| otime >= itime = t
|
2008-12-11 04:35:07 +03:00
|
|
|
| otherwise =
|
2010-09-05 22:18:50 +04:00
|
|
|
error' $ "clock-out time less than clock-in time in:\n" ++ showTransaction t
|
2007-03-12 10:40:33 +03:00
|
|
|
where
|
2009-12-16 11:07:26 +03:00
|
|
|
t = Transaction {
|
2009-12-16 20:58:51 +03:00
|
|
|
tdate = idate,
|
|
|
|
teffectivedate = Nothing,
|
|
|
|
tstatus = True,
|
|
|
|
tcode = "",
|
|
|
|
tdescription = showtime itod ++ "-" ++ showtime otod,
|
|
|
|
tcomment = "",
|
2010-11-14 01:17:32 +03:00
|
|
|
tmetadata = [],
|
2009-12-16 20:58:51 +03:00
|
|
|
tpostings = ps,
|
|
|
|
tpreceding_comment_lines=""
|
2008-12-11 04:35:07 +03:00
|
|
|
}
|
2009-01-25 10:06:59 +03:00
|
|
|
showtime = take 5 . show
|
2008-10-09 01:08:42 +04:00
|
|
|
acctname = tlcomment i
|
2009-01-25 10:06:59 +03:00
|
|
|
itime = tldatetime i
|
|
|
|
otime = tldatetime o
|
|
|
|
itod = localTimeOfDay itime
|
|
|
|
otod = localTimeOfDay otime
|
|
|
|
idate = localDay itime
|
|
|
|
hrs = elapsedSeconds (toutc otime) (toutc itime) / 3600 where toutc = localTimeToUTC utc
|
|
|
|
amount = Mixed [hours hrs]
|
2009-12-19 06:44:52 +03:00
|
|
|
ps = [Posting{pstatus=False,paccount=acctname,pamount=amount,
|
2010-11-14 01:17:32 +03:00
|
|
|
pcomment="",ptype=VirtualPosting,pmetadata=[],ptransaction=Just t}]
|
2010-05-22 23:00:20 +04:00
|
|
|
|
2010-12-27 23:26:22 +03:00
|
|
|
tests_Hledger_Data_TimeLog = TestList [
|
2010-05-22 23:00:20 +04:00
|
|
|
|
|
|
|
"timeLogEntriesToTransactions" ~: do
|
|
|
|
today <- getCurrentDay
|
|
|
|
now' <- getCurrentTime
|
|
|
|
tz <- getCurrentTimeZone
|
|
|
|
let now = utcToLocalTime tz now'
|
|
|
|
nowstr = showtime now
|
|
|
|
yesterday = prevday today
|
|
|
|
clockin = TimeLogEntry In
|
|
|
|
mktime d = LocalTime d . fromMaybe midnight . parseTime defaultTimeLocale "%H:%M:%S"
|
|
|
|
showtime = formatTime defaultTimeLocale "%H:%M"
|
|
|
|
assertEntriesGiveStrings name es ss = assertEqual name ss (map tdescription $ timeLogEntriesToTransactions now es)
|
|
|
|
|
|
|
|
assertEntriesGiveStrings "started yesterday, split session at midnight"
|
|
|
|
[clockin (mktime yesterday "23:00:00") ""]
|
|
|
|
["23:00-23:59","00:00-"++nowstr]
|
|
|
|
assertEntriesGiveStrings "split multi-day sessions at each midnight"
|
|
|
|
[clockin (mktime (addDays (-2) today) "23:00:00") ""]
|
|
|
|
["23:00-23:59","00:00-23:59","00:00-"++nowstr]
|
|
|
|
assertEntriesGiveStrings "auto-clock-out if needed"
|
|
|
|
[clockin (mktime today "00:00:00") ""]
|
|
|
|
["00:00-"++nowstr]
|
|
|
|
let future = utcToLocalTime tz $ addUTCTime 100 now'
|
|
|
|
futurestr = showtime future
|
|
|
|
assertEntriesGiveStrings "use the clockin time for auto-clockout if it's in the future"
|
|
|
|
[clockin future ""]
|
|
|
|
[printf "%s-%s" futurestr futurestr]
|
|
|
|
|
|
|
|
]
|