packages feed

hourglass 0.1.1 → 0.1.2

raw patch · 4 files changed

+103/−72 lines, 4 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

Files

README.md view
@@ -1,74 +1,80 @@ hourglass =========- Hourglass is a simple time library.  Documentation: [hourglass on hackage](http://hackage.haskell.org/package/hourglass)  Design --------One of the key design are the Timeable and Time type classes.-Time representation of the same time value are interchangeable and easy to convert-between each other.  This also allow the user to define new time types that+Key parts of the design are the Timeable and Time typeclasses.+Time representations of the same time values are interchangeable and easy to convert+between each other. This also allows the user to define new time types that interact with the same functions as the built-in types.  For example:--    let dateTime0 = DateTime { dtDate = Date { dateYear = 1970, dateMonth = January, dateDay = 1 }-                             , dtTime = TimeOfDay {todHour = 0, todMin = 0, todSec = 0, todNSec = 0 } }-        elapsed0 = Elasped 0+```haskell+let dateTime0 =+      DateTime { dtDate = Date { dateYear = 1970, dateMonth = January, dateDay = 1 }+               , dtTime = TimeOfDay {todHour = 0, todMin = 0, todSec = 0, todNSec = 0 }}+    elapsed0 = Elasped 0 -    > timeGetElapsed elapsed0 == timeGetElapsed dateTime0-    True-    > timeGetDate elapsed0 == timeGetDate dateTime0-    True-    > timePrint "YYYY-MM" elapsed0-    "1970-01"-    > timePrint "YYYY-MM" dateTime0-    "1970-01"+> timeGetElapsed elapsed0 == timeGetElapsed dateTime0+True+> timeGetDate elapsed0 == timeGetDate dateTime0+True+> timePrint "YYYY-MM" elapsed0+"1970-01"+> timePrint "YYYY-MM" dateTime0+"1970-01"+``` -Hourglass has the same limitation as your system:+Hourglass has the same limitations as your system:  * On 32 bit linux, you can't get a date after the year 2038. * In Windows 7, you can't get the date before the year 1601.  Comparaison with time ---------------------+* Getting posix time:+```haskell+-- With time+import Data.Time.Clock.POSIX -* getting posix time:+ptime <- getPOSIXTime -    ------- with time-    import Data.Time.Clock.POSIX-    ptime <- getPOSIXTime+-- With hourglass+import System.Hourglass -    ------- with hourglass-    import System.Hourglass-    ptime <- timeCurrent+ptime <- timeCurrent+``` -* getting current date year:+* Getting the current year:+```haskell+-- With time+import Data.Time.Clock+import Data.Time.Calendar -    ------- with time-    import Data.Time.Clock-    import Data.Time.Calendar-    currentYear <- (\(y,_,_) -> y) . toGregorian . utcDay <$> getCurrentTime+currentYear <- (\(y,_,_) -> y) . toGregorian . utcDay <$> getCurrentTime -    ------- with hourglass-    import System.Hourglass-    import Data.Time-    currentYear <- dateYear . timeGetDate <$> timeCurrent+-- With hourglass+import System.Hourglass+import Data.Time -* creating a time representation of "4th May 1970 15:12:24"+currentYear <- dateYear . timeGetDate <$> timeCurrent+``` -    ------- with time-    import Data.Time.Clock-    import Date.Time.Calendar+* Representating "4th May 1970 15:12:24"+```haskell+-- With time+import Data.Time.Clock+import Date.Time.Calendar -    let day = fromGregorian 1970 5 4-        diffTime = secondsToDiffTime (15 * 3600 + 12 * 60 + 24)-     in UTCTime day diffTime+let day = fromGregorian 1970 5 4+    diffTime = secondsToDiffTime (15 * 3600 + 12 * 60 + 24)+in UTCTime day diffTime -    ------- with hourglass-    import Date.Time+-- With hourglass+import Date.Time -    DateTime (Date 1970 May 4) (TimeOfDay 15 12 24 0)+DateTime (Date 1970 May 4) (TimeOfDay 15 12 24 0)+```
cbits/unix.c view
@@ -15,7 +15,7 @@  */ void hourglass_clock_calendar(struct timespec *timespec) {-#ifdef MACH+#ifdef __MACH__ 	clock_serv_t cclock; 	mach_timespec_t mts; 
hourglass.cabal view
@@ -1,5 +1,5 @@ Name:                hourglass-Version:             0.1.1+Version:             0.1.2 Synopsis:            simple performant time related library Description:     Simple time library focusing on simple but powerful and performant API
tests/Bench.hs view
@@ -1,8 +1,10 @@ {-# LANGUAGE ViewPatterns #-}+{-# LANGUAGE BangPatterns #-} module Main (main) where  import Criterion.Main import Data.Hourglass+import System.Hourglass import TimeDB  import Data.List (intercalate)@@ -13,50 +15,64 @@  timeToTuple :: T.UTCTime -> (Int, Int, Int, Int, Int, Int) timeToTuple utcTime = (fromIntegral y, m, d, h, mi, sec)- where (y,m,d) = T.toGregorian (T.utctDay utcTime)-       daytime    = floor $ T.utctDayTime utcTime-       (dt, sec)= daytime `divMod` 60-       (h , mi) = dt `divMod` 60-       --(DateTimeP (Date y m d) (TimeOfDayP (TimeOfDay h mi sec) _)) = localTimeToGlobal localtime+ where (!y,!m,!d)  = T.toGregorian (T.utctDay utcTime)+       !daytime    = floor $ T.utctDayTime utcTime+       (!dt, !sec) = daytime `divMod` 60+       (!h , !mi)  = dt `divMod` 60 +timeToTupleDate :: T.UTCTime -> (Int, Int, Int)+timeToTupleDate utcTime = (fromIntegral y, m, d)+  where (!y,!m,!d)  = T.toGregorian (T.utctDay utcTime)+ elapsedToPosixTime :: Elapsed -> T.POSIXTime elapsedToPosixTime (Elapsed (Seconds s)) = fromIntegral s  timePosixDict :: [ (Elapsed, T.POSIXTime) ] timePosixDict =-    [ (Elapsed 0, 0)-    , (Elapsed 1000000, 1000000)-    , (Elapsed 9000099, 9000099)-    , (Elapsed 1398232846, 1398232846) -- currentish time (at the time of writing)-    , (Elapsed 5134000099, 5134000099)-    , (Elapsed 10000000000000, 10000000000000) -- year 318857 ..+    [-- (Elapsed 0, 0)+    --, (Elapsed 1000000, 1000000)+    --, (Elapsed 9000099, 9000099)+    {-,-} (Elapsed 1398232846, 1398232846) -- currentish time (at the time of writing)+    --, (Elapsed 5134000099, 5134000099)+    --, (Elapsed 10000000000000, 10000000000000) -- year 318857 ..     ]  dateDict :: [ (Int, Int, Int, Int, Int, Int) ] dateDict =-    [ (1970, 1, 1, 1, 1, 1)-    , (2014, 5, 5, 5, 5, 5)-    , (2114, 11, 5, 5, 5, 5)+    [{- (1970, 1, 1, 1, 1, 1)+    , -}(2014, 5, 5, 5, 5, 5)+    --, (2114, 11, 5, 5, 5, 5)     ] --- T.posixSecondsToUTCTime timePosix-+main :: IO () main = defaultMain-    [ bgroup "highlevel" $ concatMap toHighLevel timePosixDict-    , bgroup "to-calendar"  $ concatMap toCalendar timePosixDict-    , bgroup "to-posix" $ concatMap toPosix dateDict+    [ bgroup "highlevel"   $ concatMap toHighLevel timePosixDict+    , bgroup "to-dateTime" $ concatMap toCalendar timePosixDict+    , bgroup "to-date"     $ concatMap toCalendarDate timePosixDict+    , bgroup "utc-to-date" $ concatMap toCalendarUTC timePosixDict+    , bgroup "to-posix"    $ concatMap toPosix dateDict+    , bgroup "system"      fromSystem     ]   where toHighLevel (posixHourglass, posixTime) =-            [ bench (show $ posixHourglass) $ nf timeGetDateTimeOfDay posixHourglass-            , bench (show $ posixTime) $ nf T.posixSecondsToUTCTime posixTime+            [ bench (showH posixHourglass) $ nf timeGetDateTimeOfDay posixHourglass+            , bench (showT posixTime) $ nf T.posixSecondsToUTCTime posixTime             ]         toCalendar (posixHourglass, posixTime) =-            [ bench (show $ posixHourglass) $ nf timeGetDateTimeOfDay posixHourglass-            , bench (show $ posixTime) $ nf (timeToTuple . T.posixSecondsToUTCTime) posixTime+            [ bench (showH posixHourglass) $ nf timeGetDateTimeOfDay posixHourglass+            , bench (showT posixTime) $ nf (timeToTuple . T.posixSecondsToUTCTime) posixTime             ]+        toCalendarDate (posixHourglass, posixTime) =+            [ bench (showH posixHourglass) $ nf timeGetDate posixHourglass+            , bench (showT posixTime) $ nf (timeToTupleDate . T.posixSecondsToUTCTime) posixTime+            ]+        toCalendarUTC (posixHourglass, posixTime) = +            [ bench (showH posixHourglass) $ nf timeGetDateTimeOfDay posixHourglass+            , bench (showT utcTime) $ nf timeToTuple utcTime+            ]+          where !utcTime = T.posixSecondsToUTCTime posixTime         toPosix v =-            [ bench (n v) $ nf hourglass v-            , bench (n v) $ nf time v+            [ bench ("hourglass/" ++ n v) $ nf hourglass v+            , bench ("time/" ++ n v) $ nf time v             ]           where n (y,m,d,h,mi,s) = (intercalate "-" $ map show [y,m,d]) ++ " " ++ (intercalate ":" $ map show [h,mi,s])                 hourglass (y,m,d,h,mi,s) = timeGetElapsed $ DateTime (Date y (toEnum (m-1)) d) (TimeOfDay h mi s 0)@@ -64,4 +80,13 @@                                                diffTime = T.secondsToDiffTime $ fromIntegral (h * 3600 + mi * 60 + s)                                             in T.utcTimeToPOSIXSeconds (T.UTCTime day diffTime) -            --bench (show $ fromIntegral posixHourglass) $ nf (+        fromSystem =+            [ bench ("hourglass/p")    $ nfIO timeCurrent+            , bench ("hourglass/ns")   $ nfIO timeCurrentP+            , bench ("time/posixTime") $ nfIO T.getPOSIXTime+            ]++        showH :: Show a => a -> String+        showH a = "hourglass/" ++ show a+        showT :: Show a => a -> String+        showT a = "time/" ++ show a