friendly-time (empty) → 0.2
raw patch · 4 files changed
+133/−0 lines, 4 filesdep +basedep +old-localedep +timesetup-changed
Dependencies added: base, old-locale, time
Files
- Data/Time/Format/Human.hs +76/−0
- LICENSE +30/−0
- Setup.hs +2/−0
- friendly-time.cabal +25/−0
+ Data/Time/Format/Human.hs view
@@ -0,0 +1,76 @@+-------------------------------------------------------------------------------+-- |+-- Module : Data.Time.Format.Human+-- Copyright : (c) Patrick Brisbin 2010 +-- License : as-is+--+-- Maintainer : pbrisbin@gmail.com+-- Stability : unstable+-- Portability : unportable+--+-- Prints a @'UTCTime'@ as "a few seconds ago" or "3 days ago" and+-- similar.+--+-------------------------------------------------------------------------------+module Data.Time.Format.Human+ ( humanReadableTime+ , humanReadableTime'+ ) where++import Data.Time++import Data.Char (isSpace)+import System.Locale (defaultTimeLocale)++-- | Based on @humanReadableTimeDiff@ found in+-- <https://github.com/snoyberg/haskellers/blob/master/Haskellers.hs>,+-- <https://github.com/snoyberg/haskellers/blob/master/LICENSE>+humanReadableTime :: UTCTime -> IO String+humanReadableTime t = do+ now <- getCurrentTime+ return $ humanReadableTime' now t++-- | A pure form, takes the current time as an argument+humanReadableTime' :: UTCTime -- ^ current time+ -> UTCTime -> String+humanReadableTime' cur t = helper $ diffUTCTime cur t++ where+ minutes :: NominalDiffTime -> Double+ minutes n = realToFrac $ n / 60++ hours :: NominalDiffTime -> Double+ hours n = minutes n / 60++ days :: NominalDiffTime -> Double+ days n = hours n / 24++ weeks :: NominalDiffTime -> Double+ weeks n = days n / 7++ years :: NominalDiffTime -> Double+ years n = days n / 365++ i2s :: RealFrac a => a -> String+ i2s n = show m where m = truncate n :: Int++ trim = f . f where f = reverse . dropWhile isSpace++ old = utcToLocalTime utc t+ dow = trim $! formatTime defaultTimeLocale "%l:%M %p on %A" old+ thisYear = trim $! formatTime defaultTimeLocale "%b %e" old+ previousYears = trim $! formatTime defaultTimeLocale "%b %e, %Y" old++ helper d + | d < 1 = "just now"+ | d < 60 = i2s d ++ " seconds ago"+ | minutes d < 2 = "one minute ago"+ | minutes d < 60 = i2s (minutes d) ++ " minutes ago"+ | hours d < 2 = "one hour ago"+ | hours d < 24 = "about " ++ i2s (hours d) ++ " hours ago"+ | days d < 5 = "at " ++ dow+ | days d < 10 = i2s (days d) ++ " days ago"+ | weeks d < 2 = i2s (weeks d) ++ " week ago"+ | weeks d < 5 = i2s (weeks d) ++ " weeks ago"+ | years d < 1 = "on " ++ thisYear+ | otherwise = "on " ++ previousYears
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c)2010, Patrick Brisbin++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 Patrick Brisbin 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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ friendly-time.cabal view
@@ -0,0 +1,25 @@+name: friendly-time+version: 0.2+description: A yesod goody, friendly time+synopsis: Print time information in friendly ways+homepage: http://github.com/pbrisbin/yesod-goodies/friendly-time+license: BSD3+license-file: LICENSE+author: Patrick Brisbin+maintainer: me@pbrisbin.com+category: Web, Yesod+build-type: Simple+cabal-version: >=1.6++library+ exposed-modules: Data.Time.Format.Human++ build-depends: base >= 4 && < 5+ , time+ , old-locale++ ghc-options: -Wall+ +source-repository head+ type: git+ location: git://github.com/pbrisbin/friendly-time.git