packages feed

strptime (empty) → 0.1

raw patch · 4 files changed

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

Dependencies added: base, bytestring, time

Files

+ Data/Time/Parse.hsc view
@@ -0,0 +1,98 @@+{-# LANGUAGE ForeignFunctionInterface, FlexibleInstances #-}+------------------------------------------------------------+-- |+-- Copyright    :   (c) 2009 Eugene Kirpichov+-- License      :   BSD-style+--+-- Maintainer   :   ekirpichov@gmail.com+-- Stability    :   experimental+-- Portability  :   portable (H98 + FFI)+--+-- strptime wrapper+--+------------------------------------------------------------++module Data.Time.Parse (+    Strptime(..)+) where++import Foreign+import Foreign.C.Types+import Foreign.C.String+import Foreign.ForeignPtr+import Foreign.Marshal.Alloc+import GHC.Ptr++import Data.Time++import qualified Data.ByteString.Char8 as S+import qualified Data.ByteString.Internal as BI+import qualified Data.ByteString.Lazy.Char8 as L+import qualified Data.ByteString.Lazy.Internal as LI++#include <time.h>++-- | The class of values from which time may be parsed+class Strptime a where+    -- | Given a format string in the format of C's strptime (see <http://linux.die.net/man/3/strptime>) +    -- and a data string, parse a date+time value from the data string and also return the remainder+    -- of the data string.+    strptime  :: a -> a -> Maybe (LocalTime, a)++instance Strptime [Char] where+    strptime f = \s -> do+        (t, n) <- ff s+        return (t, drop n s)+      where ff = strptime_ f  +instance Strptime L.ByteString where+    strptime f = \s -> do+        (t, n) <- ff s+        return (t, L.drop (fromIntegral n) s)+      where ff = strptime_ f  ++instance Strptime S.ByteString where+    strptime f = \s -> do+        (t, n) <- ff s+        return (t, S.drop (fromIntegral n) s)+      where ff = strptime_ f  ++class Strptime_ a where+    strptime_ :: a -> a -> Maybe (LocalTime, Int)++instance Strptime_ [Char] where+    strptime_ f s = strptime_ (S.pack f) (S.pack s)++instance Strptime_ L.ByteString where+    strptime_ f s = strptime_ (S.concat . L.toChunks $ f) (S.concat . L.toChunks $ s)++instance Strptime_ S.ByteString where+    strptime_ f = unsafePerformIO $ do+      -- Avoid memcpy-ing the format string every time.+      let (pf, ofs, len) = BI.toForeignPtr f+      ztf <- mallocBytes (len+1)+      copyBytes ztf (unsafeForeignPtrToPtr pf) len+      pokeByteOff ztf len (0::Word8)+      fztf <- newForeignPtr_ ztf+      addForeignPtrFinalizer finalizerFree fztf++      return $ \s -> unsafePerformIO $ S.useAsCString s $ \cs -> do+        allocaBytes (#const sizeof(struct tm)) $ \p_tm -> do+          last <- strptime_c cs (castPtr ztf) p_tm+          if last == nullPtr +            then return Nothing+            else do+              sec   <-  (#peek struct tm,tm_sec  ) p_tm :: IO CInt+              min   <-  (#peek struct tm,tm_min  ) p_tm :: IO CInt+              hour  <-  (#peek struct tm,tm_hour ) p_tm :: IO CInt+              mday  <-  (#peek struct tm,tm_mday ) p_tm :: IO CInt+              month <-  (#peek struct tm,tm_mon  ) p_tm :: IO CInt+              year  <-  (#peek struct tm,tm_year ) p_tm :: IO CInt+              let day = fromGregorian (fromIntegral (year+1900)) (fromIntegral month) (fromIntegral mday)+              let tod = TimeOfDay (fromIntegral hour) (fromIntegral min) (fromIntegral sec)+              +              return $ Just (LocalTime day tod, last `minusPtr` cs)++type CTm = () -- struct tm++foreign import ccall unsafe "time.h strptime"+    strptime_c :: CString -> CString -> Ptr CTm -> IO CString
+ LICENSE view
@@ -0,0 +1,23 @@+Copyright (c) 2009, Eugene Kirpichov+All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++1. Redistributions of source code must retain the above copyright notice,+   this list of conditions and the following disclaimer.+2. 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.++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
+ strptime.cabal view
@@ -0,0 +1,25 @@+Name: strptime+Category: System, Data, Parsing+Version: 0.1+Cabal-version: >= 1.2+Build-type: Simple+Copyright: Eugene Kirpichov+Maintainer: ekirpichov@gmail.com    +Author: Eugene Kirpichov+License: BSD3+License-File: LICENSE+Synopsis: Efficient parsing of CalendarTime using a binding to C's strptime+Description:+  This library provides a binding to strptime, that allows to parse +  dates and times from strings and strict/lazy bytestrings. The library+  creates LocalTime values.++Flag split-base++Library+  Build-Depends: time, bytestring+  if flag(split-base)+    Build-depends: base >= 3.0 && < 4.0+  else+    Build-depends: base < 3.0+  Exposed-modules: Data.Time.Parse