Skip to content

Commit 35e6408

Browse files
committed
Parse UTCTime with 8601 format yesodweb#339
1 parent f569c4c commit 35e6408

5 files changed

Lines changed: 26 additions & 4 deletions

File tree

persistent-sqlite/persistent-sqlite.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: persistent-sqlite
2-
version: 2.1.1.1
2+
version: 2.1.1.2
33
license: MIT
44
license-file: LICENSE
55
author: Michael Snoyman <michael@snoyman.com>

persistent-sqlite/test/Spec.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,9 @@ main = hspec $ do
2828
insert . Test $ read "2014-11-30 05:15:25.123"
2929
[Single x] <- rawSql "select strftime('%s%f',time) from test" []
3030
liftIO $ x `shouldBe` Just ("141732452525.123" :: String)
31+
it "issue #339" $ asIO $ runSqlite ":memory:" $ do
32+
runMigration migrateAll
33+
now <- liftIO getCurrentTime
34+
tid <- insert $ Test now
35+
Just (Test now') <- get tid
36+
liftIO $ now' `shouldBe` now

persistent/ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.1.1.3
2+
3+
Parse UTCTime in 8601 format [#339](https://github.com/yesodweb/persistent/issues/339)
4+
15
## 2.1.1.1
26

37
Support for monad-control 1.0

persistent/Database/Persist/Class/PersistField.hs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module Database.Persist.Class.PersistField
1515
) where
1616

1717
import Database.Persist.Types.Base
18-
import Data.Time (Day(..), TimeOfDay, UTCTime)
18+
import Data.Time (Day(..), TimeOfDay, UTCTime, parseTime)
1919
#ifdef HIGH_PRECISION_DATE
2020
import Data.Time.Clock.POSIX (posixSecondsToUTCTime)
2121
#endif
@@ -48,6 +48,12 @@ import qualified Data.Map as M
4848
import qualified Data.Text.Encoding as TE
4949
import qualified Data.Vector as V
5050

51+
#if MIN_VERSION_time(1,5,0)
52+
import Data.Time (defaultTimeLocale)
53+
#else
54+
import System.Locale (defaultTimeLocale)
55+
#endif
56+
5157
-- | A value which can be marshalled to and from a 'PersistValue'.
5258
class PersistField a where
5359
toPersistValue :: a -> PersistValue
@@ -221,7 +227,12 @@ instance PersistField UTCTime where
221227
fromPersistValue x@(PersistText t) =
222228
case reads $ T.unpack t of
223229
(d, _):_ -> Right d
224-
_ -> Left $ T.pack $ "Expected UTCTime, received " ++ show x
230+
_ ->
231+
case parse8601 $ T.unpack t of
232+
Nothing -> Left $ T.pack $ "Expected UTCTime, received " ++ show x
233+
Just x -> Right x
234+
where
235+
parse8601 = parseTime defaultTimeLocale "%FT%T%Q"
225236
fromPersistValue x@(PersistByteString s) =
226237
case reads $ unpack s of
227238
(d, _):_ -> Right d

persistent/persistent.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: persistent
2-
version: 2.1.1.2
2+
version: 2.1.1.3
33
license: MIT
44
license-file: LICENSE
55
author: Michael Snoyman <michael@snoyman.com>
@@ -26,6 +26,7 @@ library
2626
, bytestring >= 0.9
2727
, transformers >= 0.2.1
2828
, time >= 1.1.4
29+
, old-locale
2930
, text >= 0.8
3031
, containers >= 0.2
3132
, conduit >= 1.0

0 commit comments

Comments
 (0)