Commit 875c30e5 authored by Mael NICOLAS's avatar Mael NICOLAS

add lose precision

parent 57da5f95
...@@ -40,6 +40,7 @@ library ...@@ -40,6 +40,7 @@ library
, extra , extra
, filepath , filepath
, http-conduit , http-conduit
, hxt
, ini , ini
, lens , lens
, logging-effect , logging-effect
...@@ -162,16 +163,22 @@ test-suite garg-test ...@@ -162,16 +163,22 @@ test-suite garg-test
build-depends: build-depends:
QuickCheck QuickCheck
, base , base
, duckling
, extra , extra
, gargantext , gargantext
, hspec , hspec
, parsec
, quickcheck-instances
, text , text
, time
other-modules: other-modules:
Ngrams.Lang Ngrams.Lang
Ngrams.Lang.En Ngrams.Lang.En
Ngrams.Lang.Fr Ngrams.Lang.Fr
Ngrams.Lang.Occurrences Ngrams.Lang.Occurrences
Ngrams.Metrics Ngrams.Metrics
Parsers.Date
Parsers.Types
Parsers.WOS Parsers.WOS
Paths_gargantext Paths_gargantext
default-language: Haskell2010 default-language: Haskell2010
...@@ -8,6 +8,8 @@ import Test.Hspec ...@@ -8,6 +8,8 @@ import Test.Hspec
import Test.QuickCheck import Test.QuickCheck
import Parsers.Types import Parsers.Types
import Control.Applicative ((<*>))
import Data.Tuple (uncurry)
import Data.Either (Either(..)) import Data.Either (Either(..))
import Data.Time (ZonedTime(..)) import Data.Time (ZonedTime(..))
import Data.Text (pack, Text) import Data.Text (pack, Text)
...@@ -18,11 +20,15 @@ import Gargantext.Parsers.Date (fromRFC3339) ...@@ -18,11 +20,15 @@ import Gargantext.Parsers.Date (fromRFC3339)
fromRFC3339Inv :: Either ParseError ZonedTime -> Text fromRFC3339Inv :: Either ParseError ZonedTime -> Text
fromRFC3339Inv (Right z) = toRFC3339 z fromRFC3339Inv (Right z) = toRFC3339 z
fromRFC3339Inv (Left pe) = pack $ show pe fromRFC3339Inv (Left pe) = panic . pack $ show pe
{-testFromRFC3339 :: IO () testFromRFC3339 :: IO ()
testFromRFC3339 = hspec $ do testFromRFC3339 = hspec $ do
describe "Test fromRFC3339: " $ do describe "Test fromRFC3339: " $ do
it "is the inverse of Duckling's toRFC3339" $ property $ it "is the inverse of Duckling's toRFC3339" $ property $
\x -> (fromRFC3339 . fromRFC3339Inv) x == (x :: Either ParseError ZonedTime) ((==) <*> (fromRFC3339 . fromRFC3339Inv)) . Right . looseZonedTimePrecision
--}
-- \x -> uncurry (==) $ (,) <*> (fromRFC3339 . fromRFC3339Inv) $ Right $ looseZonedTimePrecision x
-- \x -> let e = Right x :: Either ParseError ZonedTime
-- in fmap looseZonedTimePrecision e == (fromRFC3339 . fromRFC3339Inv ) (fmap looseZonedTimePrecision e)
...@@ -4,16 +4,35 @@ ...@@ -4,16 +4,35 @@
module Parsers.Types where module Parsers.Types where
import Gargantext.Prelude import Gargantext.Prelude
import Prelude (floor, fromIntegral)
import Test.QuickCheck import Test.QuickCheck
import Test.QuickCheck.Instances() import Test.QuickCheck.Instances ()
import Text.Parsec.Pos import Text.Parsec.Pos
import Text.Parsec.Error (ParseError, Message(..), newErrorMessage) import Text.Parsec.Error (ParseError, Message(..), newErrorMessage)
import Data.Time.LocalTime (ZonedTime (..)) import Data.Time.LocalTime (ZonedTime (..), TimeZone (..), TimeOfDay(..), LocalTime(..))
import Data.Eq (Eq(..)) import Data.Eq (Eq(..))
import Data.Either (Either(..))
deriving instance Eq ZonedTime deriving instance Eq ZonedTime
looseTimeOfDayPrecision :: TimeOfDay -> TimeOfDay
looseTimeOfDayPrecision (TimeOfDay h m s) = TimeOfDay h m 0
looseLocalTimePrecision :: LocalTime -> LocalTime
looseLocalTimePrecision (LocalTime ld ltd) = LocalTime ld $ looseTimeOfDayPrecision ltd
looseTimeZonePrecision :: TimeZone -> TimeZone
looseTimeZonePrecision (TimeZone zm _ _) = TimeZone zm False "CET"
looseZonedTimePrecision :: ZonedTime -> ZonedTime
looseZonedTimePrecision (ZonedTime lt tz) = ZonedTime (looseLocalTimePrecision lt) $ looseTimeZonePrecision tz
loosePrecisionEitherPEZT :: Either ParseError ZonedTime -> Either ParseError ZonedTime
loosePrecisionEitherPEZT (Right zt) = Right $ looseZonedTimePrecision zt
loosePrecisionEitherPEZT pe = pe
instance Arbitrary Message where instance Arbitrary Message where
arbitrary = do arbitrary = do
msgContent <- arbitrary msgContent <- arbitrary
......
...@@ -36,7 +36,7 @@ import Duckling.Types (jsonValue, Entity) ...@@ -36,7 +36,7 @@ import Duckling.Types (jsonValue, Entity)
import Duckling.Api (analyze, parse) import Duckling.Api (analyze, parse)
import qualified Data.HashSet as HashSet import qualified Data.HashSet as HashSet
import qualified Data.Aeson as Json import qualified Data.Aeson as Json
import Data.Time (ZonedTime(..), LocalTime(..), TimeZone(..), TimeOfDay(..), getCurrentTimeZone) import Data.Time (ZonedTime(..), LocalTime(..), TimeZone(..), TimeOfDay(..))
import Data.Time.Calendar (Day, fromGregorian) import Data.Time.Calendar (Day, fromGregorian)
import Data.Fixed (Fixed (MkFixed)) import Data.Fixed (Fixed (MkFixed))
import Data.Foldable (length) import Data.Foldable (length)
...@@ -52,7 +52,6 @@ import Data.Text (Text, unpack) ...@@ -52,7 +52,6 @@ import Data.Text (Text, unpack)
import Duckling.Types (ResolvedToken) import Duckling.Types (ResolvedToken)
import Safe (headMay) import Safe (headMay)
import System.IO.Unsafe (unsafePerformIO)
import Text.Parsec.Error (ParseError) import Text.Parsec.Error (ParseError)
import Text.Parsec.String (Parser) import Text.Parsec.String (Parser)
...@@ -144,7 +143,7 @@ parseTimeOfDay = do ...@@ -144,7 +143,7 @@ parseTimeOfDay = do
_ <- char '.' _ <- char '.'
dec <- many1NoneOf ['+', '-'] dec <- many1NoneOf ['+', '-']
let (nb, l) = (decimalStringToInt $ r ++ dec, length dec) let (nb, l) = (decimalStringToInt $ r ++ dec, length dec)
seconds = nb * 10^(12-l) seconds = nb * 10^(12-l)
return $ TimeOfDay h m (MkFixed . toInteger $ seconds) return $ TimeOfDay h m (MkFixed . toInteger $ seconds)
...@@ -155,9 +154,8 @@ parseTimeZone = do ...@@ -155,9 +154,8 @@ parseTimeZone = do
h <- wrapDST =<< many1NoneOf [':'] h <- wrapDST =<< many1NoneOf [':']
_ <- char ':' _ <- char ':'
m <- wrapDST =<< (many1 $ anyChar) m <- wrapDST =<< (many1 $ anyChar)
let (TimeZone _ s n) = unsafePerformIO getCurrentTimeZone
let timeInMinute = if sign == '+' then h * 60 + m else -h * 60 - m let timeInMinute = if sign == '+' then h * 60 + m else -h * 60 - m
in return $ TimeZone timeInMinute s n in return $ TimeZone timeInMinute False "CET"
-- | Parser which use parseGregorian, parseTimeOfDay and parseTimeZone to create a ZonedTime -- | Parser which use parseGregorian, parseTimeOfDay and parseTimeZone to create a ZonedTime
parseZonedTime :: Parser ZonedTime parseZonedTime :: Parser ZonedTime
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment