Skip to content

Commit

Permalink
ZonedTime: do not erase timezone information (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
snoyberg committed Jul 25, 2012
1 parent 0836112 commit 757049b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
3 changes: 1 addition & 2 deletions persistent-mongoDB/Database/Persist/MongoDB.hs
Expand Up @@ -57,7 +57,6 @@ import Control.Monad.IO.Class (liftIO)
import Data.Aeson (Value (Object), (.:), (.:?), (.!=))
import Control.Monad (mzero)
import Control.Monad.Trans.Control (MonadBaseControl)
import Data.Time (zonedTimeToUTC)

#ifdef DEBUG
import FileLocation (debug)
Expand Down Expand Up @@ -503,7 +502,7 @@ instance DB.Val PersistValue where
val (PersistDouble x) = DB.Float x
val (PersistBool x) = DB.Bool x
val (PersistUTCTime x) = DB.UTC x
val (PersistZonedTime (ZT x)) = DB.UTC (zonedTimeToUTC x)
val (PersistZonedTime (ZT x)) = DB.String $ T.pack $ show x
val (PersistNull) = DB.Null
val (PersistList l) = DB.Array $ map DB.val l
val (PersistMap m) = DB.Doc $ map (\(k, v)-> (DB.=:) k v) m
Expand Down
3 changes: 1 addition & 2 deletions persistent-mysql/Database/Persist/MySQL.hs
Expand Up @@ -28,7 +28,6 @@ import Data.Function (on)
import Data.IORef
import Data.List (find, intercalate, sort, groupBy)
import Data.Text (Text, pack)
import Data.Time.LocalTime (zonedTimeToUTC)
import System.Environment (getEnvironment)

import Data.Conduit
Expand Down Expand Up @@ -196,7 +195,7 @@ instance MySQL.Param P where
render (P (PersistDay d)) = MySQL.render d
render (P (PersistTimeOfDay t)) = MySQL.render t
render (P (PersistUTCTime t)) = MySQL.render t
render (P (PersistZonedTime (ZT t))) = MySQL.render $ zonedTimeToUTC t
render (P (PersistZonedTime (ZT t))) = MySQL.render $ show t
render (P PersistNull) = MySQL.render MySQL.Null
render (P (PersistList l)) = MySQL.render $ listToJSON l
render (P (PersistMap m)) = MySQL.render $ mapToJSON m
Expand Down
3 changes: 1 addition & 2 deletions persistent-sqlite/Database/Sqlite.hs
Expand Up @@ -34,7 +34,6 @@ import qualified Data.ByteString.Internal as BSI
import Foreign
import Foreign.C
import Database.Persist.Store (PersistValue (..), listToJSON, mapToJSON, ZT (ZT))
import Data.Time (zonedTimeToUTC)
import Data.Text (Text, pack, unpack)
import Data.Text.Encoding (encodeUtf8, decodeUtf8With)
import Data.Text.Encoding.Error (lenientDecode)
Expand Down Expand Up @@ -342,7 +341,7 @@ bind statement sqlData = do
PersistDay d -> bindText statement parameterIndex $ pack $ show d
PersistTimeOfDay d -> bindText statement parameterIndex $ pack $ show d
PersistUTCTime d -> bindText statement parameterIndex $ pack $ show d
PersistZonedTime (ZT d) -> bindText statement parameterIndex $ pack $ show $ zonedTimeToUTC d
PersistZonedTime (ZT d) -> bindText statement parameterIndex $ pack $ show d
PersistList l -> bindText statement parameterIndex $ listToJSON l
PersistMap m -> bindText statement parameterIndex $ mapToJSON m
PersistObjectId _ -> P.error "Refusing to serialize a PersistObjectId to a SQLite value"
Expand Down
11 changes: 10 additions & 1 deletion persistent-test/DataTypeTest.hs
Expand Up @@ -21,7 +21,7 @@ import Data.Text (Text)
import qualified Data.Text as T
import Data.ByteString (ByteString)
import qualified Data.ByteString as S
import Data.Time (Day, TimeOfDay (..), UTCTime (..), fromGregorian)
import Data.Time (Day, TimeOfDay (..), UTCTime (..), fromGregorian, ZonedTime (..), LocalTime (..), TimeZone (..))
import System.Random (randomIO, randomRIO, Random)
import Control.Applicative ((<$>), (<*>))
import Control.Monad (when)
Expand All @@ -46,6 +46,7 @@ DataTypeTable no-json
time TimeOfDay
#endif
utc UTCTime
zonedTime ZonedTime
|]

cleanDB :: PersistQuery b m => b m ()
Expand Down Expand Up @@ -77,6 +78,7 @@ specs = describe "data type specs" $ do
check "time" dataTypeTableTime
#endif
check "utc" dataTypeTableUtc
check "zoned" dataTypeTableZonedTime

-- Do a special check for Double since it may
-- lose precision when serialized.
Expand All @@ -99,6 +101,7 @@ randomValue = DataTypeTable
<*> randomTime
#endif
<*> randomUTC
<*> randomZonedTime
where forbidden = [NotAssigned, PrivateUse]

asIO :: IO a -> IO a
Expand All @@ -118,6 +121,12 @@ randomDay = fromGregorian <$> randomRIO (1900, 9400) <*> randomIO <*> randomIO
randomUTC :: IO UTCTime
randomUTC = UTCTime <$> randomDay <*> return 0 -- precision issues

randomZonedTime :: IO ZonedTime
randomZonedTime = ZonedTime <$> (LocalTime <$> randomDay <*> randomTime) <*> (TimeZone <$> randomRIO (-600, 600) <*> randomIO <*> return "")

instance Eq ZonedTime where
a == b = show a == show b

randomTime :: IO TimeOfDay
randomTime = TimeOfDay <$> randomRIO (0, 23)
<*> randomRIO (0, 59)
Expand Down

0 comments on commit 757049b

Please sign in to comment.