diff options
author | Hendrik Jaeger <henk@frustcomp> | 2013-12-31 15:07:08 +0100 |
---|---|---|
committer | Hendrik Jaeger <henk@frustcomp> | 2013-12-31 15:07:08 +0100 |
commit | 79eb0216f15e1a8570cfae7ae5ed83da74ca30b8 (patch) | |
tree | 98ef54056c23107725c30b5b641b98889ffaf95c | |
parent | d38cc424b83a0240b466f41ec1fe7521aba2d64e (diff) |
On branch master
Changes to be committed:
modified: diddohs.hs
CHANGE: use ZonedTime and UTCTime instead of DateTime
-rw-r--r-- | diddohs.hs | 62 |
1 files changed, 47 insertions, 15 deletions
@@ -5,43 +5,75 @@ import Data.List( zipWith4, transpose ) import Data.Maybe( fromJust, fromMaybe ) import Data.Monoid( mempty ) import Data.Time.Clock( secondsToDiffTime ) -import Options.Applicative( execParser, info, strOption, long ) -import System.Environment( getArgs ) -import HMSTime( HMSTime(..), secondsToHMS ) +import Data.Time.Format( parseTime, formatTime ) +import Data.Time.LocalTime( LocalTime(..), ZonedTime(..), zonedTimeToUTC, midnight, localDay ) import Diddo.Entry( DiddoEntry(..) ) +import HMSTime( HMSTime(..), secondsToHMS ) +import Options.Applicative( execParser, info, strOption, long, help, helper, briefDesc, fullDesc, progDesc, header, (<>) ) +import System.Environment( getArgs ) +import System.Locale data DiddoOpts = DiddoOpts { inDateFmt :: String , inFile :: String } -calculateDeltas :: [DateTime] -> [Integer] -calculateDeltas dateTimes = zipWith diffSeconds dateTimes ((startOfDay $ head dateTimes) : init dateTimes) +parseToZonedTime :: String -> String -> ZonedTime +parseToZonedTime format = fromMaybe (error "Input data broken.") . parseTime defaultTimeLocale format -parseDateTimeFormat :: String -> String -> DateTime -parseDateTimeFormat format = fromMaybe (error "Input data broken.") . parseDateTime format +parseISOsecondsTime :: String -> ZonedTime +parseISOsecondsTime = parseToZonedTime $ iso8601DateFormat $ Just "%T%z" -startOfDay :: DateTime -> DateTime -startOfDay = fromJust . parseDateTime "%x" . formatDateTime "%x" +parseRFC822Time :: String -> ZonedTime +parseRFC822Time = parseToZonedTime rfc822DateFormat + +formatZonedTime :: String -> ZonedTime -> String +formatZonedTime format = formatTime defaultTimeLocale format + +zonedTimesDeltas :: ZonedTime -> [ZonedTime] -> [Integer] +zonedTimesDeltas startTime timestamps = + let + startTimeUTC = zonedTimeToUTC startTime + relevantTimestamps = dropWhile (< startTimeUTC) $ map zonedTimeToUTC timestamps + in + zipWith diffSeconds relevantTimestamps $ startTimeUTC : init relevantTimestamps + +startOfZonedDay :: ZonedTime -> ZonedTime +startOfZonedDay time = ZonedTime (LocalTime day midnight) $ zonedTimeZone time + where + day = localDay $ zonedTimeToLocalTime time mainWithOpts :: DiddoOpts -> IO () mainWithOpts opts = do - [ timeStrings - , entries + [ + timeStamps + , entryText ] <- transpose . map (splitOn ";") . lines <$> readFile (inFile opts) let - deltasHMS = map secondsToHMS $ calculateDeltas $ map (parseDateTimeFormat (inDateFmt opts)) timeStrings - diddos_summarized = zipWith4 DiddoEntry ("" : init timeStrings) timeStrings deltasHMS entries + parseCustomTime = parseToZonedTime $ inDateFmt opts +-- parsedTimes = map (parseToZonedTime $ inDateFmt opts) timeStamps + parsedTimes = map parseCustomTime timeStamps + deltasHMS = map secondsToHMS $ zonedTimesDeltas (startOfZonedDay $ head parsedTimes) parsedTimes + diddos_summarized = zipWith4 DiddoEntry + ((formatZonedTime (inDateFmt opts) $ startOfZonedDay $ head parsedTimes) : init timeStamps) + timeStamps deltasHMS entryText mapM_ print diddos_summarized main :: IO () main = execParser opts >>= mainWithOpts where - opts = info parser mempty + opts = info (helper <*> parser) + ( header "diddohs - A Time/Task Tracker" + <> progDesc "Diddoh Desc" +-- <> briefDesc + <> fullDesc + ) parser = DiddoOpts - <$> strOption ( long "indateform" ) + <$> strOption ( long "indateform" + <> help "Input date format, see date(1)" + ) <*> strOption ( long "infile" ) |