diff options
-rw-r--r-- | Diddo.hs | 47 | ||||
-rw-r--r-- | HMSTime.hs | 34 | ||||
-rw-r--r-- | diddohs.hs | 10 |
3 files changed, 38 insertions, 53 deletions
@@ -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 @@ -1,33 +1,13 @@ module HMSTime -( HMSTime(HMSTime, hours, minutes, seconds) -, secondsToHMS -, hmsTimeStringToHMSTime -, hmsTimeStringToSeconds +( diffTimeToHMSString ) where +import Data.Time.Clock( NominalDiffTime() ) import Text.Printf( printf ) -import Data.List.Split( splitOn ) -data HMSTime = HMSTime { hours :: Integer, minutes :: Integer, seconds :: Integer } - -instance Show HMSTime where - show (HMSTime h m s) = printf "%d:%02d:%02d" h m s - -secondsToHMS :: Integer -> HMSTime -secondsToHMS numSeconds = HMSTime h m s where - (mLeft, s) = numSeconds `divMod` 60 - (h, m) = mLeft `divMod` 60 - -hmsTimeStringToHMSTime :: String -> HMSTime -hmsTimeStringToHMSTime hmsString = HMSTime h m s where - h:m:s:_ = map readInteger $ splitOn ":" hmsString - -hmsTimeToSeconds :: HMSTime -> Integer -hmsTimeToSeconds (HMSTime {hours = h, minutes = m, seconds = s}) = h*3600 + m*60 + s - -hmsTimeStringToSeconds :: String -> Integer -hmsTimeStringToSeconds = hmsTimeToSeconds . hmsTimeStringToHMSTime - -readInteger :: String -> Integer -readInteger x = read x :: Integer +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 @@ -1,12 +1,14 @@ import Control.Applicative( (<$>) ) import Control.Monad( unless ) -import qualified Data.Map as Map import Data.Time.Clock( UTCTime(..) ) import Diddo( DiddoEntry(..), LogEntry(..), parseDiddoLogline, formatDiddoEntry, timestamp, logToDiddoEntry ) import System.Console.GetOpt import System.Environment( getArgs ) import System.Exit( exitSuccess, exitFailure ) import System.IO( stderr, hPutStr ) +import qualified Data.Map as Map +import qualified Data.Text as T +import qualified Data.Text.IO as TIO data Opt = Opt { optVerbose :: Bool @@ -88,8 +90,8 @@ main = do dddLogEntries <- map Diddo.parseDiddoLogline <$> case optInputFiles effectiveOptions of - files@(_:_) -> lines . concat <$> mapM readFile files - [] -> lines <$> getContents + files@(_:_) -> T.lines . T.concat <$> mapM TIO.readFile files + [] -> T.lines <$> TIO.getContents let dddLogEntryMap = Map.fromList $ map (\diddo -> (Diddo.timestamp diddo, diddo)) dddLogEntries @@ -101,5 +103,5 @@ main = do mapM_ putStrLn args -- DEBUG - mapM_ (putStrLn . snd) $ Map.toAscList $ Map.map (Diddo.formatDiddoEntry outDateFmt) diddoEntriesMap + mapM_ (TIO.putStrLn . snd) $ Map.toAscList $ Map.map (Diddo.formatDiddoEntry outDateFmt) diddoEntriesMap |