From 62ba998ef05a79c1fd60386f7ec8dbef49f8a78f Mon Sep 17 00:00:00 2001 From: Hendrik Jaeger Date: Sat, 8 Mar 2014 19:55:18 +0100 Subject: On branch master modified: Diddo.hs modified: diddohs.hs CHANGED: code cleanup --- Diddo.hs | 68 ++++++++++++++++++++-------------------------------------------- 1 file changed, 21 insertions(+), 47 deletions(-) (limited to 'Diddo.hs') diff --git a/Diddo.hs b/Diddo.hs index cbbbce0..e90e650 100644 --- a/Diddo.hs +++ b/Diddo.hs @@ -1,84 +1,58 @@ module Diddo -( LogLine(LogLine) -, LogEntry(LogEntry) +( LogEntry(LogEntry) , DiddoEntry(DiddoEntry) -, DiddoEntry2(DiddoEntry2) , parseDiddoLogline , formatDiddoEntry , timestamp , logToDiddoEntry ) where -import HMSTime( HMSTime, secondsToHMS ) +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, fromJust ) +import Data.Maybe( fromMaybe ) import Data.Time.Format( parseTime, formatTime ) -import Data.Time.LocalTime( LocalTime(..), ZonedTime(..), TimeZone(..), zonedTimeToUTC, utcToZonedTime, midnight, localDay ) +import Data.Time.LocalTime( ZonedTime(..), zonedTimeToUTC, utcToZonedTime ) import Data.List.Split( splitOn ) import System.Locale -data LogLine - = LogLine String - -instance Show LogLine where - show (LogLine x) = x - -data LogEntry - = LogEntry - { timestamp :: UTCTime - , timezone :: TimeZone - , text :: String - } +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 String String HMSTime String - -data DiddoEntry2 = DiddoEntry2 - { startTime :: ZonedTime - , endTime :: ZonedTime - , comment :: String - } +data DiddoEntry = DiddoEntry + { startTime :: ZonedTime + , endTime :: ZonedTime + , comment :: String + } instance Show DiddoEntry where - show (DiddoEntry start finish delta entry) = intercalate ";" [start,finish,(show delta),entry] - -instance Show DiddoEntry2 where - show (DiddoEntry2 start finish entry) = intercalate ";" [show start,show finish,show $ diffSeconds (zonedTimeToUTC finish) (zonedTimeToUTC start),entry] + show (DiddoEntry start finish entry) = intercalate ";" [show start,show finish,show $ diffSeconds (zonedTimeToUTC finish) (zonedTimeToUTC start),entry] -formatDiddoEntry :: String -> DiddoEntry2 -> String -formatDiddoEntry format (DiddoEntry2 start end comment) = (formatTime defaultTimeLocale format start) ++ ";" ++ (formatTime defaultTimeLocale format end) ++ ";" ++ (show $ secondsToHMS $ diffSeconds (zonedTimeToUTC end) (zonedTimeToUTC start)) ++ ";" ++ comment +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 -> DiddoEntry2 -logToDiddoEntry startutc logentry = DiddoEntry2 startZoned endZoned $ text logentry +logToDiddoEntry :: UTCTime -> LogEntry -> DiddoEntry +logToDiddoEntry startutc logentry = DiddoEntry startZoned endZoned $ text logentry where startZoned = utcToZonedTime (timezone logentry) startutc endZoned = utcToZonedTime (timezone logentry) $ timestamp logentry - -lineToEntry :: LogLine -> LogEntry -lineToEntry (LogLine line) = LogEntry ts tz text - where - splitLine = splitOn ";" line - text = intercalate ";" $ tail splitLine - time = parseISOsecondsTime $ head splitLine - (ts,tz) = (zonedTimeToUTC time, zonedTimeZone time) - parseDiddoLogline :: String -> LogEntry -parseDiddoLogline line = LogEntry ts tz text +parseDiddoLogline line = LogEntry ts tz string where splitLine = splitOn ";" line - text = intercalate ";" $ tail splitLine + 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 -- cgit v1.2.3