packages feed

timecalc (empty) → 0.1

raw patch · 4 files changed

+125/−0 lines, 4 filesdep +basedep +haskelinedep +uu-parsinglibsetup-changed

Dependencies added: base, haskeline, uu-parsinglib

Files

+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2011 Chris Eidhof++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in+all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN+THE SOFTWARE.+
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ TimeCalc.hs view
@@ -0,0 +1,60 @@+module Main where++import Text.ParserCombinators.UU+import Text.ParserCombinators.UU.Utils+import Text.ParserCombinators.UU.BasicInstances hiding (Parser, input)+import System.Console.Haskeline+import System.Environment (getArgs)++type Parser a = P (Str Char String LineColPos) a++main :: IO ()+main = do+  args <- getArgs+  if null args then interactive +               else putStrLn $ run timeExpr (unwords args)++interactive :: IO ()+interactive = runInputT defaultSettings loop+  where +   loop :: InputT IO ()+   loop = do minput <- getInputLine "> "+             case minput of+               Nothing -> return ()+               Just "quit" -> return ()+               Just input -> do outputStrLn $ run timeExpr input+                                loop+++run :: Parser String -> String -> String+run p inp = do  let (a, errors) =  parse ( (,) <$> p <*> pEnd) (createStr (LineColPos 0 0 0) inp)+                if null errors then  a+                               else  "Error in expression"++timeExpr :: Parser String+timeExpr = format <$> expr+ where format :: Double -> String+       format x = let minutes :: Integer+                      seconds :: Integer+                      (minutes,secondsMultiplier) = properFraction x+                      seconds = round $ secondsMultiplier * 60+                      showSeconds s | s < 10    = "0" ++ show s+                                    | otherwise = show s+                  in  show minutes ++ ":" ++ showSeconds seconds++expr :: Parser Double+expr = foldr pChainl ( pDouble <|> pTime <|>pParens expr) (map same_prio operators) + where+  operators       = [[('+', (+)), ('-', (-))],  [('*' , (*))], [('/' , (/))]]+  same_prio  ops  = foldr (<|>) empty [ op <$ lexeme (pSym c) | (c, op) <- ops]++pTime :: Parser Double+pTime = lexeme pRawMinuteTime <|> lexeme pRawHourTime++pRawMinuteTime :: Parser Double+pRawMinuteTime = makeTime <$> pIntegerRaw <* pSym ':' <*> pIntegerRaw <?> "min:sec"+ where makeTime x y = x + (y / 60.0)++pRawHourTime :: Parser Double+pRawHourTime = makeTime <$> pIntegerRaw <* pSym ':' <*> pIntegerRaw <* pSym ':' <*> pIntegerRaw <?> "hour:min:sec"+ where makeTime x y z = x * 60 + y + z / 60.0
+ timecalc.cabal view
@@ -0,0 +1,43 @@+Name:                timecalc+Version:             0.1+Description:         A calculator for running times+License:             MIT+License-file:        LICENSE+Category:            TODO+Author:              Chris Eidhof+Maintainer:          chris+hackage@eidhof.nl+Build-Type:          Simple+Cabal-Version:       >=1.6+Homepage:            https://github.com/chriseidhof/TimeCalc+Description:+    TimeCalc is a very simple utility for calculating times. I use it for calculating splits or predicting race times. It is a calculator that supports doubles and times. For example:+   .+    If I run 5 kilometers at 3:30/kilometer, the race will take me 17:30.+   .+        > 5*3:30+        17:30+   .+    If I want to know how fast my splits need to be in order to run a 10K in 37:00.+   .+        > 37:00/10+        3:42+   .+    If I want to add two times:+   .+        > 3:42 + 3:30 + 3:49+        11:01+   .+    Or calculating the splits Bekele needs to run on the marathon:+   .+        > 2:02:13 / 42.195+        2:54+   .+    Which is very fast, indeed.++source-repository head+  type:     git+  location: http://github.com/chriseidhof/TimeCalc++Executable timecalc+  Main-is:           TimeCalc.hs+  Build-Depends:     base >= 3 && < 5, haskeline >= 0.6, uu-parsinglib >= 2.7