packages feed

timespan (empty) → 0.1.0.0

raw patch · 4 files changed

+105/−0 lines, 4 filesdep +basedep +timesetup-changed

Dependencies added: base, time

Files

+ LICENSE view
@@ -0,0 +1,7 @@+Copyright (c) 2016 Alexander Thiemann++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
+ src/Data/Time/TimeSpan.hs view
@@ -0,0 +1,71 @@+{-# LANGUAGE BangPatterns #-}+module Data.Time.TimeSpan+  ( TimeSpan+  , milliseconds, seconds, minutes, hours, days+  , toMilliseconds, toSeconds, toMinutes, toHours, toDays+  , diffUTCTimeTS, addUTCTimeTS+  , sleepTS+  , timeAction+  )+where++import Control.Concurrent+import Data.Time+import System.CPUTime++-- | An abstract timespan. Use the provided smart constructors to create+-- a meaningful timespan+newtype TimeSpan+  = TimeSpan { unTimeSpan :: Double } -- as milliseconds+  deriving (Eq, Ord)++instance Monoid TimeSpan where+    mempty = TimeSpan 0+    mappend (TimeSpan a) (TimeSpan b) = TimeSpan (a + b)++milliseconds :: Double -> TimeSpan+milliseconds = TimeSpan++seconds :: Double -> TimeSpan+seconds = milliseconds . (* 1000)++minutes :: Double -> TimeSpan+minutes = seconds . (* 60)++hours :: Double -> TimeSpan+hours = minutes . (* 60)++days :: Double -> TimeSpan+days = hours . (* 24)++toMilliseconds :: TimeSpan -> Double+toMilliseconds = unTimeSpan++toSeconds :: TimeSpan -> Double+toSeconds = (/1000) . toMilliseconds++toMinutes :: TimeSpan -> Double+toMinutes = (/60) . toSeconds++toHours :: TimeSpan -> Double+toHours = (/60) . toMinutes++toDays :: TimeSpan -> Double+toDays = (/24) . toHours++diffUTCTimeTS :: UTCTime -> UTCTime -> TimeSpan+diffUTCTimeTS a b = seconds $ fromRational $ toRational $ diffUTCTime a b++addUTCTimeTS :: TimeSpan -> UTCTime -> UTCTime+addUTCTimeTS a b = addUTCTime (fromRational $ toRational $ toSeconds a) b++sleepTS :: TimeSpan -> IO ()+sleepTS ts = threadDelay (round $ toMilliseconds ts * 1000)++timeAction :: IO a -> IO (TimeSpan, a)+timeAction action =+    do !t1 <- getCPUTime+       !a <- action+       !t2 <- getCPUTime+       let secs = fromIntegral (t2-t1) * 1e-12+       return (seconds secs, a)
+ timespan.cabal view
@@ -0,0 +1,25 @@+name:                timespan+version:             0.1.0.0+synopsis:            Useful timespan datatype and functions+description:         Please see README.md+homepage:            https://github.com/agrafix/timespan#readme+license:             MIT+license-file:        LICENSE+author:              Alexander Thiemann+maintainer:          mail@athiemann.net+copyright:           2016 Alexander Thiemann+category:            Web+build-type:          Simple+-- extra-source-files:+cabal-version:       >=1.10++library+  hs-source-dirs:      src+  exposed-modules:     Data.Time.TimeSpan+  build-depends:       base >= 4.7 && < 5,+                       time >= 1.2+  default-language:    Haskell2010++source-repository head+  type:     git+  location: https://github.com/agrafix/timespan