packages feed

timing-convenience (empty) → 0.1

raw patch · 8 files changed

+290/−0 lines, 8 filesdep +basedep +timesetup-changed

Dependencies added: base, time

Files

+ Data/Time/Convenience.hs view
@@ -0,0 +1,45 @@+module Data.Time.Convenience (+ timeFor+,timeSince+,module Data.Time.Convenience.Data+) where++import Data.Time.Clock+import Control.Applicative ((<$>))+import Data.Time.Convenience.Data+import Data.Time.Convenience.Calculators++-- | Produce the time following the specified offset. For example, to get+-- the date and time from two weeks from right now:+--+-- > timeFor 1 Fortnight FromNow+timeFor :: NominalDiffTime -> Unit -> Direction -> IO UTCTime+timeFor n unit direction = do+  currentTime <- getCurrentTime+  return $ timeSince currentTime n unit direction++-- | Given a time, produce a new time offset from that time. For example,+-- to get the date and time from a month after two weeks ago:+--+-- > do+-- >   twoWeeksAgo <- timeFor 1 Fortnight Ago+-- >   return $ timeSince twoWeeksAgo 1 Month FromThat+timeSince :: UTCTime -> NominalDiffTime -> Unit -> Direction -> UTCTime+timeSince base n unit direction =+  addUTCTime ((calculator unit) n direction) base++-- | Given a Unit, produce the function that takes a number and a direction and+-- produces the number of seconds to offset the current time.+calculator :: (Num i) => Unit -> (i -> Direction -> i)+calculator Second = seconds+calculator Seconds = seconds+calculator Minute = minutes+calculator Minutes = minutes+calculator Hour = hours+calculator Hours = hours+calculator Day = days+calculator Days = days+calculator Week = weeks+calculator Weeks = weeks+calculator Fortnight = fortnights+calculator Fortnights = fortnights
+ Data/Time/Convenience/Calculators.hs view
@@ -0,0 +1,61 @@+module Data.Time.Convenience.Calculators (+ seconds+,minutes+,hours+,days+,weeks+,fortnights+) where++import Data.Time.Convenience.Data++-- | The number of seconds from the current time in the specified direction.+-- For example, three seconds in the past is:+--+-- > seconds 3 Ago+--+-- And three seconds into the future is:+--+-- > seconds 3 FromNow+--+-- This only produces seconds, it does not produce the actual time. See the+-- functions in "Data.Time.Convenience" for 'timeFor' and 'timeSince'.+seconds :: (Num i) => i -> Direction -> i+seconds n Ago = negate n+seconds n BeforeThat = negate n+seconds n FromNow = n+seconds n FromThat = n++-- | Similar to 'seconds', this produces the number of minutes from the current+-- time in the specified direction, in seconds. It is most useful as an offset+-- for functions in the "Data.Time.Convenience" module.+minutes :: (Num i) => i -> Direction -> i+minutes n direction = (seconds n direction) * 60++-- | Similar to 'seconds', this produces the number of hours from the current+-- time in the specified direction, in seconds. It is most useful as an offset+-- for functions in the "Data.Time.Convenience" module. See 'seconds' for an+-- example.+hours :: (Num i) => i -> Direction -> i+hours n direction = (minutes n direction) * 60++-- | Similar to 'seconds', this produces the number of days from the current+-- time in the specified direction, in seconds. It is most useful as an offset+-- for functions in the "Data.Time.Convenience" module. See 'seconds' for an+-- example.+days :: (Num i) => i -> Direction -> i+days n direction = (hours n direction) * 24++-- | Similar to 'seconds', this produces the number of weeks from the current+-- time in the specified direction, in seconds. It is most useful as an offset+-- for functions in the "Data.Time.Convenience" module. See 'seconds' for an+-- example.+weeks :: (Num i) => i -> Direction -> i+weeks n direction = (days n direction) * 7++-- | Similar to 'seconds', this produces the number of fortnights (two weeks)+-- from the current time in the specified direction, in seconds. It is most+-- useful as an offset for functions in the "Data.Time.Convenience" module. See+-- 'seconds' for an example.+fortnights :: (Num i) => i -> Direction -> i+fortnights n direction = (weeks n direction) * 2
+ Data/Time/Convenience/Data.hs view
@@ -0,0 +1,22 @@+module Data.Time.Convenience.Data (+Unit(..)+,Direction(..)+) where++-- | The number of units to count. Singulars are provided to make your code+-- read nicely.+data Unit =+    Second    | Seconds+  | Minute    | Minutes+  | Hour      | Hours+  | Day       | Days+  | Week      | Weeks+  | Fortnight | Fortnights+  deriving (Eq, Show, Ord)++-- | The direction in which to travel from the current time (or, in the case+-- of FromThat, from the given time). As just hinted at, @FromNow@ and+-- @FromThat@ are the same thing, but one reads better sometimes. Same with+-- @Ago@ and @BeforeThat@.+data Direction = Ago | BeforeThat | FromNow | FromThat+  deriving (Eq, Show, Ord)
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c)2011, Mike Burns++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Mike Burns nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,39 @@+timing-convenience+------------------++Functions to make it a little more convenient to work with time offsets.+Inspired by Ruby's ActiveSupport time methods on Fixnum.++Usage+=====++See the [Sample.hs](http://github.com/mike-burns/timing-convenience/blob/master/Sample.hs) for a more full sample.++    main = do+      t <- timeFor 3 Days Ago+      putStrLn $ show t++Installation+============++In your project's cabal file:++    -- Packages needed in order to build this package.+    Build-depends:       timing-convenience++Or from the command line:++    cabal install timing-convenience++Documentation+=============++For details see [the reference documentation on Hackage](http://hackage.haskell.org/packages/archive/timing-convenience/lastest/doc/html/Data-Time-Convenience.html).++Copyright+=========++Copyright 2011 Mike Burns.++Available under the BSD 3-clause license.+
+ Sample.hs view
@@ -0,0 +1,26 @@+module Sample where++import Data.Time.Convenience+import Data.Time.Format (formatTime)+import System.Locale (defaultTimeLocale)++main = do+  aWeekAgo     <- timeFor 1 Week Ago+  aWeekFromNow <- timeFor 1 Week FromNow+  aTickAgo     <- timeFor 3 Seconds Ago+  tomorrow     <- timeFor 1 Day FromNow++  putStrLn $ "A week ago was          " ++ formatting aWeekAgo+  putStrLn $ "A week from now will be " ++ formatting aWeekFromNow+  putStrLn $ "A bit ago was           " ++ formatting aTickAgo+  putStrLn $ "Tomorrow will be        " ++ formatting tomorrow++  putStrLn ""++  let twoWeeksBeforeThat = timeSince aWeekAgo 1 Fortnight BeforeThat+      aBitFromTomorrow   = timeSince tomorrow 3 Seconds FromThat++  putStrLn $ "A fortnight before a week ago was " ++ formatting twoWeeksBeforeThat+  putStrLn $ "A bit after tomorrow will be      " ++ formatting aBitFromTomorrow++formatting = formatTime defaultTimeLocale "%c %T"
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ timing-convenience.cabal view
@@ -0,0 +1,65 @@+-- timing-convenience.cabal auto-generated by cabal init. For+-- additional options, see+-- http://www.haskell.org/cabal/release/cabal-latest/doc/users-guide/authors.html#pkg-descr.+-- The name of the package.+Name:                timing-convenience++-- The package version. See the Haskell package versioning policy+-- (http://www.haskell.org/haskellwiki/Package_versioning_policy) for+-- standards guiding when and how versions should be incremented.+Version:             0.1++-- A short (one-line) description of the package.+Synopsis:            Convenient functions for getting times.++-- A longer description of the package.+Description:         Inspired by Rails' ActiveSupport, this package provides+                     nice looking convenience functions for getting a time and+                     date, offset from today.++-- The license under which the package is released.+License:             BSD3++-- The file containing the license text.+License-file:        LICENSE++-- The package author(s).+Author:              Mike Burns++-- An email address to which users can send suggestions, bug reports,+-- and patches.+Maintainer:          mike@mike-burns.com++-- A copyright notice.+Copyright:           2011 Mike Burns++Category:            System++Build-type:          Simple++-- Extra files to be distributed with the package, such as examples or+-- a README.+Extra-source-files:  Sample.hs, README.md++-- Constraint on the version of Cabal needed to build this package.+Cabal-version:       >=1.6++source-repository head+  type: git+  location: git://github.com/mike-burns/timing-convenience.git++Library+  -- Modules exported by the library.+  Exposed-modules:     Data.Time.Convenience+                      ,Data.Time.Convenience.Data+                      ,Data.Time.Convenience.Calculators+  +  -- Packages needed in order to build this package.+  Build-depends:       time, base >= 4.0 && < 5.0+  +  -- Modules not exported by this package.+  -- Other-modules:       +  +  -- Extra tools (e.g. alex, hsc2hs, ...) needed to build the source.+  -- Build-tools:         +