From 1e3285599922c3b6ec009ac641464f3232d59bb8 Mon Sep 17 00:00:00 2001 From: Hendrik Jaeger Date: Tue, 11 Mar 2014 00:21:54 +0100 Subject: On branch master modified: Diddo.hs modified: HMSTime.hs modified: diddohs.hs --- Diddo.hs | 47 +++++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 22 deletions(-) (limited to 'Diddo.hs') diff --git a/Diddo.hs b/Diddo.hs index e90e650..745d036 100644 --- a/Diddo.hs +++ b/Diddo.hs @@ -7,37 +7,34 @@ module Diddo , 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.Clock( UTCTime(), NominalDiffTime(), diffUTCTime ) import Data.Time.Format( parseTime, formatTime ) +import Data.Time.LocalTime( TimeZone() ) import Data.Time.LocalTime( ZonedTime(..), zonedTimeToUTC, utcToZonedTime ) -import Data.List.Split( splitOn ) import System.Locale +import qualified Data.Text as T data LogEntry = LogEntry { timestamp :: UTCTime , timezone :: TimeZone - , text :: String + , text :: T.Text } -instance Show LogEntry where - show x = (show $ timestamp x) ++ (show $ timezone x) ++ text x - data DiddoEntry = DiddoEntry - { startTime :: ZonedTime - , endTime :: ZonedTime - , comment :: String + { startTime :: ZonedTime + , endTime :: ZonedTime + , comment :: T.Text } -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 +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 + ] logToDiddoEntry :: UTCTime -> LogEntry -> DiddoEntry logToDiddoEntry startutc logentry = DiddoEntry startZoned endZoned $ text logentry @@ -45,17 +42,23 @@ logToDiddoEntry startutc logentry = DiddoEntry startZoned endZoned $ text logent startZoned = utcToZonedTime (timezone logentry) startutc endZoned = utcToZonedTime (timezone logentry) $ timestamp logentry -parseDiddoLogline :: String -> LogEntry +parseDiddoLogline :: T.Text -> LogEntry parseDiddoLogline line = LogEntry ts tz string where - splitLine = splitOn ";" line - string = intercalate ";" $ tail splitLine + splitLine = T.splitOn (T.pack ";") line + string = T.intercalate (T.pack ";") $ 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" +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 + where + (mLeft, s) = floor delta `divMod` 60 :: (Int, Int) + (h, m) = mLeft `divMod` 60 -- cgit v1.2.3