diff options
Diffstat (limited to 'Diddo.hs')
-rw-r--r-- | Diddo.hs | 70 |
1 files changed, 43 insertions, 27 deletions
@@ -1,19 +1,21 @@ +{-# LANGUAGE OverloadedStrings #-} + module Diddo ( LogEntry(LogEntry) -, DiddoEntry(DiddoEntry) +, Diddo(Diddo) +, formatDiddo , parseDiddoLogline -, formatDiddoEntry -, timestamp -, logToDiddoEntry +, logToDiddo +, parseToZonedTime ) where -import Data.DateTime( diffSeconds ) import Data.Maybe( fromMaybe ) import Data.Time.Clock( UTCTime(), NominalDiffTime(), diffUTCTime ) -import Data.Time.Format( parseTime, formatTime ) -import Data.Time.LocalTime( TimeZone() ) -import Data.Time.LocalTime( ZonedTime(..), zonedTimeToUTC, utcToZonedTime ) +import Data.Time.Format( FormatTime(), parseTime, formatTime ) +import Data.Time.LocalTime( TimeZone(), ZonedTime(..), zonedTimeToUTC, utcToZonedTime ) +import Text.Parsec import System.Locale +import Text.Printf( printf ) import qualified Data.Text as T data LogEntry = LogEntry @@ -22,42 +24,56 @@ data LogEntry = LogEntry , text :: T.Text } -data DiddoEntry = DiddoEntry +data Diddo = Diddo { startTime :: ZonedTime , endTime :: ZonedTime , comment :: T.Text } -formatDiddoEntry :: String -> DiddoEntry -> T.Text -formatDiddoEntry format entry = T.intercalate (T.pack ";") - [ T.pack $ formatTime defaultTimeLocale format (startTime entry) - , T.pack $ formatTime defaultTimeLocale format (endTime entry) - , T.pack $ diffTimeToHMSString $ diffUTCTime (zonedTimeToUTC (endTime entry)) (zonedTimeToUTC (startTime entry)) - , comment entry - ] +getTimestamp :: Diddo -> UTCTime +getTimestamp = zonedTimeToUTC . endTime + +formatDiddo :: String -> Diddo -> T.Text +formatDiddo format (Diddo start end text) = T.intercalate ";" diddoline + where + diddoline = [startZonedString, endZonedString, delta, text] + startZonedString = timeToText format start + endZonedString = timeToText format end + startUTC = zonedTimeToUTC start + endUTC = zonedTimeToUTC end + delta = diffTimeToHMSString $ diffUTCTime endUTC startUTC + +timeToText :: FormatTime a => String -> a -> T.Text +timeToText format = T.pack . formatTime defaultTimeLocale format -logToDiddoEntry :: UTCTime -> LogEntry -> DiddoEntry -logToDiddoEntry startutc logentry = DiddoEntry startZoned endZoned $ text logentry +logToDiddo :: UTCTime -> LogEntry -> Diddo +logToDiddo startutc logentry = Diddo startZoned endZoned $ text logentry where startZoned = utcToZonedTime (timezone logentry) startutc endZoned = utcToZonedTime (timezone logentry) $ timestamp logentry -parseDiddoLogline :: T.Text -> LogEntry -parseDiddoLogline line = LogEntry ts tz string +-- parseDiddoLogline' :: T.Text -> (UTCTime, LogEntry) +-- parseDiddoLogline' line = (ts, LogEntry ts tz string) + +parseDiddoLogline :: T.Text -> (UTCTime, LogEntry) +parseDiddoLogline line = (ts, LogEntry ts tz string) where - splitLine = T.splitOn (T.pack ";") line - string = T.intercalate (T.pack ";") $ tail splitLine - time = parseISOsecondsTime $ head splitLine - (ts,tz) = (zonedTimeToUTC time, zonedTimeZone time) + (timestring:strings) = T.splitOn ";" line + string = T.intercalate ";" strings + time = parseISOsecondsTime timestring + (ts,tz) = (zonedTimeToUTC time, zonedTimeZone time) parseToZonedTime :: String -> String -> ZonedTime -parseToZonedTime format string = fromMaybe (error $ "Input data broken: " ++ string) $ parseTime defaultTimeLocale format string +parseToZonedTime format string = zt + where + zt = fromMaybe (error $ "Input data broken: " ++ string) parsedTime + parsedTime = parseTime defaultTimeLocale format string parseISOsecondsTime :: T.Text -> ZonedTime parseISOsecondsTime timestring = parseToZonedTime (iso8601DateFormat $ Just "%T%z") $ T.unpack timestring -diffTimeToHMSString :: NominalDiffTime -> String -diffTimeToHMSString delta = printf "%d:%02d:%02d" h m s +diffTimeToHMSString :: NominalDiffTime -> T.Text +diffTimeToHMSString delta = T.pack $ printf "%d:%02d:%02d" h m s where (mLeft, s) = floor delta `divMod` 60 :: (Int, Int) (h, m) = mLeft `divMod` 60 |