Haskell:
Parsing a date from a string
How to:
Out of the box, Haskell offers basic tools for parsing dates, but leveraging libraries like time
for core functionality and date-parse
or time-parse
for more flexible parsing can significantly simplify the task.
First, ensure you have the time
library available; it’s often included with GHC, but if you need to specify it as a dependency, add time
to your project’s cabal file or use cabal install time
to manually install it.
import Data.Time.Format
import Data.Time.Clock
import System.Locale (defaultTimeLocale)
-- Using the time library to parse a date in a standard format
parseBasicDate :: String -> Maybe UTCTime
parseBasicDate = parseTimeM True defaultTimeLocale "%Y-%m-%d"
Example usage and output:
main :: IO ()
main = print $ parseBasicDate "2023-04-01"
-- Output: Just 2023-03-31 22:00:00 UTC
For more complex scenarios, where you need to handle multiple formats or locales, third-party libraries like date-parse
can be more convenient:
Assuming you’ve added date-parse
to your dependencies and installed it, here’s how you might use it:
import Data.Time.Calendar
import Text.Date.Parse (parseDate)
-- Parsing a date string with the date-parse library supports multiple formats
parseFlexibleDate :: String -> Maybe Day
parseFlexibleDate = parseDate
Example usage with date-parse
:
main :: IO ()
main = print $ parseFlexibleDate "April 1, 2023"
-- Output: Just 2023-04-01
Each example demonstrates the fundamental approach to taking a string and turning it into a usable date object in Haskell. The choice between using the time
library’s built-in functions and opting for a third-party solution like date-parse
depends on the specific needs of your application, such as the range of input formats you need to handle.