From 95d1857e437aa09e1161be075c654c50afd91dd8 Mon Sep 17 00:00:00 2001 From: Hendrik Jaeger Date: Sun, 30 Mar 2014 23:25:26 +0200 Subject: On branch master modified: Diddo.hs modified: diddohs.hs --- Diddo.hs | 70 +++++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 43 insertions(+), 27 deletions(-) (limited to 'Diddo.hs') diff --git a/Diddo.hs b/Diddo.hs index 745d036..2100b25 100644 --- a/Diddo.hs +++ b/Diddo.hs @@ -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 -- cgit v1.2.3