summaryrefslogtreecommitdiff
path: root/Diddo.hs
blob: e90e6503456d6898bd2262513721f1872d0e1ade (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
55
56
57
58
59
60
61
module Diddo
( LogEntry(LogEntry)
, DiddoEntry(DiddoEntry)
, parseDiddoLogline
, formatDiddoEntry
, timestamp
, logToDiddoEntry
) where

import HMSTime( secondsToHMS )
import Data.List( intercalate )
import Data.DateTime( diffSeconds )
import Data.Time.LocalTime( TimeZone() )
import Data.Time.Clock( UTCTime() )
import Data.Maybe( fromMaybe )
import Data.Time.Format( parseTime, formatTime )
import Data.Time.LocalTime( ZonedTime(..), zonedTimeToUTC, utcToZonedTime )
import Data.List.Split( splitOn )
import System.Locale

data LogEntry = LogEntry
    { timestamp     :: UTCTime
    , timezone      :: TimeZone
    , text          :: String
    }

instance Show LogEntry where
    show x = (show $ timestamp x) ++ (show $ timezone x) ++ text x

data DiddoEntry = DiddoEntry
    { startTime         :: ZonedTime
    , endTime           :: ZonedTime
    , comment           :: String
    }

instance Show DiddoEntry where
  show (DiddoEntry start finish entry) = intercalate ";" [show start,show finish,show $ diffSeconds (zonedTimeToUTC finish) (zonedTimeToUTC start),entry]

formatDiddoEntry :: String -> DiddoEntry -> String
formatDiddoEntry format entry = (formatTime defaultTimeLocale format (startTime entry)) ++ ";" ++ (formatTime defaultTimeLocale format (endTime entry)) ++ ";" ++ (show $ secondsToHMS $ diffSeconds (zonedTimeToUTC (endTime entry)) (zonedTimeToUTC (startTime entry))) ++ ";" ++ comment entry

logToDiddoEntry :: UTCTime -> LogEntry -> DiddoEntry
logToDiddoEntry startutc logentry = DiddoEntry startZoned endZoned $ text logentry
    where
        startZoned = utcToZonedTime (timezone logentry) startutc
        endZoned = utcToZonedTime (timezone logentry) $ timestamp logentry

parseDiddoLogline :: String -> LogEntry
parseDiddoLogline line = LogEntry ts tz string
    where
        splitLine           = splitOn ";" line
        string              = intercalate ";" $ tail splitLine
        time                = parseISOsecondsTime $ head splitLine
        (ts,tz)             = (zonedTimeToUTC time, zonedTimeZone time)

parseToZonedTime :: String -> String -> ZonedTime
parseToZonedTime format string = fromMaybe (error $ "Input data broken: " ++ string) $ parseTime defaultTimeLocale format string

parseISOsecondsTime :: String -> ZonedTime
parseISOsecondsTime = parseToZonedTime $ iso8601DateFormat $ Just "%T%z"