summaryrefslogtreecommitdiff
path: root/diddohs.hs
blob: b96b491ff8815e8fbd2517bd92bfb45d210a74e3 (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
import System.Environment( getArgs )
import Data.Time.Git( approxidate )
import Data.Time.LocalTime( utcToLocalTime, getTimeZone )
import Data.Time.Clock.POSIX( posixSecondsToUTCTime )
import Data.List.Split( splitOn )
import Data.List( zip4, intersperse )
import Data.Maybe( fromJust )
import Control.Monad( forM_ )
import Text.Printf

main = do
  logfile_name : _ <- getArgs
  logfile_content <- readFile logfile_name
  let loglines                = lines logfile_content
      loglines_split          = map (splitOn ";") loglines

      entries                 = map (head . tail) loglines_split

      timestrings_finish      = map head loglines_split
      timestrings_start       = "" : init timestrings_finish

      timestamps_finish       = map timestringToEpoch timestrings_finish
      timestamps_start        = 0 : init timestamps_finish

      timestamps_deltas       = zipWith (-) timestamps_finish timestamps_start
      timestamps_deltas_HMMSS = map secondsToHMMSS timestamps_deltas

      delta_entry_tuples      = zip timestamps_deltas_HMMSS entries
      summaries               = zip4 (map show timestrings_start) (map show timestrings_finish) timestamps_deltas_HMMSS entries

  forM_ summaries $ \(start, finish, delta, entry) ->
    putStrLn $ concat $ intersperse ";" [start, finish, delta, entry]

timestringToEpoch :: String -> Integer
timestringToEpoch = fromJust . approxidate

secondsToHMMSS :: (Num seconds, Show seconds, Integral seconds, Text.Printf.PrintfArg seconds) => seconds -> String
secondsToHMMSS seconds = printf "%d:%02d:%02d" h m s
  where
    (mLeft, s) = seconds `divMod` 60
    (h, m) = mLeft `divMod` 60

getStartOfDay :: Num t => t -> t
getStartOfDay time = 0

--epochToTimestring :: Num t => t -> String
--epochToTimestring epochtime = let getTZ = getTimeZone utctime in utcToLocalTime getTZ utctime
--  where
--    tz = getTimeZone utctime
--    utctime = posixSecondsToUTCTime epochtime

--getTZ time = do
--  getTimeZone time