git.iko.soy/typeable/postgresql-simple
on master help sign in
Improve Timezone Handling
commit119f516f562c34fe40fa1dd2cc6ac283a0e8adde authorLeon P Smith <leon@melding-monads.com> dateSun 10 Jun 2012, 05:49 (14 years ago) parentc5571e3
2 files changed+11−5
src/Database/PostgreSQL/Simple/Time/Implementation.hs+6−4 test/Time.hs+5−1
src/Database/PostgreSQL/Simple/Time/Implementation.hs+6−4
@@ -136,10 +136,12 @@ getLocalTimestamp = getUnbounded getLocalTime
136136
137137getTimeZone :: A.Parser TimeZone
138138getTimeZone = do
139− sign <- A.satisfy (\c -> c == '+' || c == '-')
140− offset <- digits "timezone"
141− let !minutes = 60 * if sign == '+' then offset else -offset
142− return $! minutesToTimeZone minutes
139+ sign <- A.satisfy (\c -> c == '+' || c == '-')
140+ hours <- digits "timezone"
141+ mins <- (A.char ':' *> digits "timezone minutes") <|> pure 0
142+ let !absset = 60 * hours + mins
143+ !offset = if sign == '+' then absset else -absset
144+ return $! minutesToTimeZone offset
143145
144146getZonedTime :: A.Parser ZonedTime
145147getZonedTime = ZonedTime <$> getLocalTime <*> getTimeZone
test/Time.hs+5−1
@@ -39,6 +39,10 @@ testTime env@TestEnv{..} = TestCase $ do
3939 checkRoundTrips env
4040 execute_ conn "SET timezone TO 'Asia/Tokyo'"
4141 checkRoundTrips env
42+ execute_ conn "SET timezone TO 'Asia/Kathmandu'"
43+ checkRoundTrips env
44+ execute_ conn "SET timezone TO 'America/St_Johns'"
45+ checkRoundTrips env
4246
4347initializeTable :: TestEnv -> IO ()
4448initializeTable TestEnv{..} = withTransaction conn $ do
@@ -48,7 +52,7 @@ initializeTable TestEnv{..} = withTransaction conn $ do
4852 let pop :: ByteString -> Double -> IO () = \x y ->
4953 replicateM_ numTests $ execute conn
5054 [sql| INSERT INTO testtime (y) VALUES
51− ('1900-01-01 00:00:00+00'::timestamptz
55+ ('1936-01-01 00:00:00+00'::timestamptz
5256 + ?::interval * ROUND(RANDOM() * ?)) |] (x,y)
5357 pop "1 microsecond" 6.3113904e15
5458 pop "10 microseconds" 6.3113904e14