[GHC] #9150: libraries/time: parseTime barfs on leading space in format string

#9150: libraries/time: parseTime barfs on leading space in format string --------------------------+------------------------------------------------ Reporter: mjo | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: | Version: 7.8.2 libraries (other) | Operating System: Unknown/Multiple Keywords: | Type of failure: Incorrect result at runtime Architecture: | Test Case: Unknown/Multiple | Blocking: Difficulty: | Unknown | Blocked By: | Related Tickets: | --------------------------+------------------------------------------------ This used to work around 1.4.0, one of my test suites caught it: {{{ module Main where import Data.Time.Clock import Data.Time.Format import System.Locale main :: IO () main = do putStrLn "Trailing space is handled:" print $ (parseTime defaultTimeLocale "%M " "15 " :: Maybe UTCTime) putStrLn "\nBut leading space isn't:" print $ (parseTime defaultTimeLocale " %M" " 15" :: Maybe UTCTime) }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9150 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#9150: libraries/time: parseTime barfs on leading space in format string ------------------------------------------------+-------------------------- Reporter: mjo | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: libraries (other) | Version: 7.8.2 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: Incorrect result at runtime | Unknown/Multiple Test Case: | Difficulty: Blocking: | Unknown | Blocked By: | Related Tickets: ------------------------------------------------+-------------------------- Changes (by thomie): * cc: ashley@… (added) -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9150#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#9150: libraries/time: parseTime barfs on leading space in format string -------------------------------------+------------------------------------- Reporter: mjo | Owner: Ashley Yakeley Type: bug | Status: new Priority: normal | Milestone: Component: libraries | Version: 7.8.2 (other) | Keywords: Resolution: | Operating System: Unknown/Multiple Differential Revisions: | Type of failure: Incorrect result Architecture: | at runtime Unknown/Multiple | Test Case: Difficulty: Unknown | Blocking: Blocked By: | Related Tickets: | -------------------------------------+------------------------------------- Changes (by Ashley Yakeley): * owner: => Ashley Yakeley Comment: This is awkward. The problem is that `parseTime` discards leading and trailing spaces in the input string when formatting. The obvious solution is to discard leading spaces in the format string. But here's a more difficult case: {{{ parseTime defaultTimeLocale "%Q " " " :: Maybe LocalTime }}} `%Q` matches the empty string, so one would expect this to match. However, `parseTime` discards leading spaces, `%Q` matches the empty string, and the following space can't be matched. Alternatively, instead of skipping initial spaces with `skipSpaces`, one can parse them with `skipMany (satisfy isSpace)`. But now this gives `Nothing`: {{{ parseTime defaultTimeLocale "%k" " 8" :: Maybe LocalTime }}} This ends up parsing two ways: the first when initial skipping grabs the space, and the second when `%k` grabs the space. They end up with the same value, but currently we need exactly one parse for `parseTime` to give a `Just` value. Another alternative would be to drop skipping of leading and trailing spaces. This would make the behaviour of the parsing functions more predictable. However, the current behaviour has been there since time-1.1 from 2007, so programs that depend on it will fail with stricter functions. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9150#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

Another alternative would be to drop skipping of leading and trailing spaces. This would make the behaviour of the parsing functions more
#9150: libraries/time: parseTime barfs on leading space in format string -------------------------------------+------------------------------------- Reporter: mjo | Owner: Ashley Yakeley Type: bug | Status: new Priority: normal | Milestone: Component: libraries | Version: 7.8.2 (other) | Keywords: Resolution: | Operating System: Unknown/Multiple Differential Revisions: | Type of failure: Incorrect result Architecture: | at runtime Unknown/Multiple | Test Case: Difficulty: Unknown | Blocking: Blocked By: | Related Tickets: | -------------------------------------+------------------------------------- Comment (by mjo): Replying to [comment:2 Ashley Yakeley]: predictable. However, the current behaviour has been there since time-1.1 from 2007, so programs that depend on it will fail with stricter functions. I assumed this was a bug because the behaviour changed between time-1.4.0.1 and time-1.4.2. I've narrowed down the change to time-1.4.0.1 -> time-1.4.0.2. Maybe I'm misunderstanding what you wrote, but your examples at least seem to parse fine with the older library. {{{ $ ghc-pkg list | grep time old-time-1.1.0.1 time-1.4.0.1 $ ghci ...
parseTime defaultTimeLocale "%Q " " " :: Maybe LocalTime Just 1970-01-01 00:00:00 parseTime defaultTimeLocale "%k" " 8" :: Maybe LocalTime Just 1970-01-01 08:00:00 }}}
The two examples in the description also both worked in earlier versions: {{{
parseTime defaultTimeLocale "%M " "15 " :: Maybe UTCTime Just 1970-01-01 00:15:00 UTC parseTime defaultTimeLocale " %M" " 15" :: Maybe UTCTime Just 1970-01-01 00:15:00 UTC }}}
Maybe there was a bug in time <= 1.4.0.1 that prevented the spaces from being skipped? For what it's worth, my use case is in pickling/unpickling XML. The package is now public at http://hackage.haskell.org/package/htsn-import. I have a test case that basically says, if we unpickle some XML and then repickle it, we should get what we started with. For that to work, the leading/trailing space needs to be preserved (I don't control the XML so I'm stuck with the spaces). I have a test case ensuring that, and with time-1.4.0.1 it passes. Once I upgraded the time library along with ghc, I noticed the failure. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9150#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#9150: libraries/time: parseTime barfs on leading space in format string -------------------------------------+------------------------------------- Reporter: mjo | Owner: Ashley Yakeley Type: bug | Status: closed Priority: normal | Milestone: Component: libraries | Version: 7.8.2 (other) | Keywords: Resolution: fixed | Architecture: Unknown/Multiple Operating System: | Difficulty: Unknown Unknown/Multiple | Blocked By: Type of failure: Incorrect | Related Tickets: result at runtime | Test Case: | Blocking: | Differential Revisions: | -------------------------------------+------------------------------------- Changes (by Ashley Yakeley): * status: new => closed * resolution: => fixed Comment: Fixed in HEAD for time-1.5. {{{ Prelude Data.Time> parseTime defaultTimeLocale "%Q " " " :: Maybe LocalTime Just 1970-01-01 00:00:00 Prelude Data.Time> parseTime defaultTimeLocale "%k" " 8" :: Maybe LocalTime Just 1970-01-01 08:00:00 Prelude Data.Time> parseTime defaultTimeLocale "%M " "15 " :: Maybe UTCTime Just 1970-01-01 00:15:00 UTC Prelude Data.Time> parseTime defaultTimeLocale " %M" " 15" :: Maybe UTCTime Just 1970-01-01 00:15:00 UTC }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9150#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#9150: libraries/time: parseTime barfs on leading space in format string -------------------------------------+------------------------------------- Reporter: mjo | Owner: Ashley Yakeley Type: bug | Status: closed Priority: normal | Milestone: Component: libraries | Version: 7.8.2 (other) | Keywords: Resolution: fixed | Architecture: Unknown/Multiple Operating System: | Difficulty: Unknown Unknown/Multiple | Blocked By: Type of failure: Incorrect | Related Tickets: result at runtime | Test Case: | Blocking: | Differential Revisions: | -------------------------------------+------------------------------------- Comment (by mjo): Replying to [comment:4 Ashley Yakeley]:
Fixed in HEAD for time-1.5.
Awesome, thank you. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9150#comment:5 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC