summaryrefslogtreecommitdiff
path: root/Diddo.hs
diff options
context:
space:
mode:
authorHendrik Jaeger <henk@frustcomp>2014-03-11 00:21:54 +0100
committerHendrik Jaeger <henk@frustcomp>2014-03-11 00:23:04 +0100
commit1e3285599922c3b6ec009ac641464f3232d59bb8 (patch)
tree4b6c4e29f58574182f3b53e327e269d59a1ad0a2 /Diddo.hs
parent62ba998ef05a79c1fd60386f7ec8dbef49f8a78f (diff)
On branch master
modified: Diddo.hs modified: HMSTime.hs modified: diddohs.hs
Diffstat (limited to 'Diddo.hs')
-rw-r--r--Diddo.hs47
1 files changed, 25 insertions, 22 deletions
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