Skip to content

Commit 4529064

Browse files
Fix formatting for PersistField (#1586)
* Fix formatting for PersistField * wait we don't even need that cpp * ok * ok maybe that'll do it * neat * Update persistent/Database/Persist/Class/PersistField.hs Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent b5ba120 commit 4529064

1 file changed

Lines changed: 32 additions & 18 deletions

File tree

persistent/Database/Persist/Class/PersistField.hs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,33 @@ instance PersistField TimeOfDay where
302302

303303
instance PersistField UTCTime where
304304
toPersistValue = PersistUTCTime
305-
fromPersistValue (PersistUTCTime d) = Right d
305+
fromPersistValue = utcTimeFromPersistValue
306+
306307
#ifdef HIGH_PRECISION_DATE
307-
fromPersistValue (PersistInt64 i) = Right $ posixSecondsToUTCTime $ (/ (1000 * 1000 * 1000)) $ fromIntegral $ i
308+
utcTimeFromPersistValue :: PersistValue -> Either Text UTCTime
309+
utcTimeFromPersistValue (PersistUTCTime d) = Right d
310+
utcTimeFromPersistValue (PersistInt64 i) = Right $ posixSecondsToUTCTime $ (/ (1000 * 1000 * 1000)) $ fromIntegral $ i
311+
utcTimeFromPersistValue (PersistText t) = utcTimeFromPersistText t
312+
utcTimeFromPersistValue x@(PersistByteString s) =
313+
case reads $ unpack s of
314+
(d, _):_ -> Right d
315+
_ -> Left $ fromPersistValueParseError "UTCTime" x
316+
utcTimeFromPersistValue x = Left $ fromPersistValueError "UTCTime" "time, integer, string, or bytestring" x
317+
#else
318+
utcTimeFromPersistValue :: PersistValue -> Either Text UTCTime
319+
utcTimeFromPersistValue (PersistUTCTime d) = Right d
320+
utcTimeFromPersistValue (PersistText t) = utcTimeFromPersistText t
321+
utcTimeFromPersistValue x@(PersistByteString s) =
322+
case reads $ unpack s of
323+
(d, _):_ -> Right d
324+
_ -> Left $ fromPersistValueParseError "UTCTime" x
325+
utcTimeFromPersistValue x = Left $ fromPersistValueError "UTCTime" "time, integer, string, or bytestring" x
308326
#endif
309-
fromPersistValue x@(PersistText t) =
310-
let s = T.unpack t
327+
328+
utcTimeFromPersistText :: Text -> Either Text UTCTime
329+
utcTimeFromPersistText t =
330+
let x = PersistText t
331+
s = T.unpack t
311332
in
312333
case NonEmpty.nonEmpty (reads s) of
313334
Nothing ->
@@ -322,27 +343,20 @@ instance PersistField UTCTime where
322343
-- precision parsed as posssible.
323344
Right $ fst $ NonEmpty.last matches
324345
where
325-
#if MIN_VERSION_time(1,5,0)
326-
-- Note: consider using `Data.Time.Format.ISO8601` iso8601ParseM when bumping
327-
-- lower "time" package bound to >=1.9; this function require the timezone "Z",
328-
-- so best suitable when e.g. dropping support non-canonial "notimezone" and "pretty" parse variants
329-
-- in persistent 3.0.
330-
parseTime' = parseTimeM True defaultTimeLocale
331-
#else
332-
parseTime' = parseTime defaultTimeLocale
333-
#endif
334346
parse8601 = parseTime' "%FT%T%QZ"
335347
parsePretty = parseTime' "%F %T%QZ"
336348
-- Before 2.13.3.1 persistent-sqlite was missing the timezone "Z" for UTC,
337349
-- which was only implicit, so these functions ensure backwards-compatibility.
338350
parse8601NoTimezone = parseTime' "%FT%T%Q"
339351
parsePrettyNoTimezone = parseTime' "%F %T%Q"
340-
fromPersistValue x@(PersistByteString s) =
341-
case reads $ unpack s of
342-
(d, _):_ -> Right d
343-
_ -> Left $ fromPersistValueParseError "UTCTime" x
344352

345-
fromPersistValue x = Left $ fromPersistValueError "UTCTime" "time, integer, string, or bytestring" x
353+
#if MIN_VERSION_time(1,5,0)
354+
parseTime' :: String -> String -> Maybe UTCTime
355+
parseTime' = parseTimeM True defaultTimeLocale
356+
#else
357+
parseTime' :: String -> String -> Maybe UTCTime
358+
parseTime' = parseTime defaultTimeLocale
359+
#endif
346360

347361
-- | Prior to @persistent-2.11.0@, we provided an instance of
348362
-- 'PersistField' for the 'Natural' type. This was in error, because

0 commit comments

Comments
 (0)