git.iko.soy/typeable/postgresql-simple
on master help sign in
Time Parser Tweaks
81bf589Leon P Smith
src/Database/PostgreSQL/Simple/Time/Parser.hs+6−9
@@ -63,8 +63,6 @@ timeOfDay = do
63 then return (Local.TimeOfDay h m s)
63 then return (Local.TimeOfDay h m s)
64 else fail "invalid time"
64 else fail "invalid time"
65
65
66−data T = T {-# UNPACK #-} !Int {-# UNPACK #-} !Int64
67−
68-- | Parse a count of seconds, with the integer part being two digits
66-- | Parse a count of seconds, with the integer part being two digits
69-- long.
67-- long.
70seconds :: Parser Pico
68seconds :: Parser Pico
@@ -78,16 +76,15 @@ seconds = do
78 _ -> return $! fromIntegral real
76 _ -> return $! fromIntegral real
79 where
77 where
80 parsePicos a0 t = toPico (fromIntegral (t' * 10^n))
78 parsePicos a0 t = toPico (fromIntegral (t' * 10^n))
81− where T n t' = B8.foldl' step (T 12 (fromIntegral a0)) t
79+ where n = 12 - B8.length t
82− step ma@(T m a) c
83− | m <= 0 = ma
84− | otherwise = T (m-1) (10 * a + fromIntegral (ord c) .&. 15)
80+ t' = B8.foldl' (\a c -> 10 * a + fromIntegral (ord c) .&. 15) a0
81+ (B8.take 12 t)
85
82
86-- | Parse a time zone, and return 'Nothing' if the offset from UTC is
83-- | Parse a time zone, and return 'Nothing' if the offset from UTC is
87-- zero. (This makes some speedups possible.)
84-- zero. (This makes some speedups possible.)
88timeZone :: Parser (Maybe Local.TimeZone)
85timeZone :: Parser (Maybe Local.TimeZone)
89timeZone = do
86timeZone = do
90− ch <- satisfy $ \c -> c == 'Z' || c == '+' || c == '-'
87+ ch <- satisfy $ \c -> c == '+' || c == '-' || c == 'Z'
91 if ch == 'Z'
88 if ch == 'Z'
92 then return Nothing
89 then return Nothing
93 else do
90 else do
@@ -114,7 +111,7 @@ data UTCOffsetHMS = UTCOffsetHMS {-# UNPACK #-} !Int {-# UNPACK #-} !Int {-# UNP
114-- zero. (This makes some speedups possible.)
111-- zero. (This makes some speedups possible.)
115timeZoneHMS :: Parser (Maybe UTCOffsetHMS)
112timeZoneHMS :: Parser (Maybe UTCOffsetHMS)
116timeZoneHMS = do
113timeZoneHMS = do
117− ch <- satisfy $ \c -> c == 'Z' || c == '+' || c == '-'
114+ ch <- satisfy $ \c -> c == '+' || c == '-' || c == 'Z'
118 if ch == 'Z'
115 if ch == 'Z'
119 then return Nothing
116 then return Nothing
120 else do
117 else do
@@ -163,7 +160,7 @@ localToUTCTimeOfDayHMS (UTCOffsetHMS dh dm ds) (Local.TimeOfDay h m s) =
163-- followed by a fractional component.
160-- followed by a fractional component.
164localTime :: Parser Local.LocalTime
161localTime :: Parser Local.LocalTime
165localTime = Local.LocalTime <$> day <* daySep <*> timeOfDay
162localTime = Local.LocalTime <$> day <* daySep <*> timeOfDay
166− where daySep = satisfy (\c -> c == 'T' || c == ' ')
163+ where daySep = satisfy (\c -> c == ' ' || c == 'T')
167
164
168-- | Behaves as 'zonedTime', but converts any time zone offset into a
165-- | Behaves as 'zonedTime', but converts any time zone offset into a
169-- UTC time.
166-- UTC time.