[Haskell-cafe] Help parsing dates and times

Bjorn Bringert bringert at cs.chalmers.se
Tue Oct 16 12:15:56 EDT 2007


On Oct 16, 2007, at 17:54 , Justin Bailey wrote:

> On 10/16/07, Bjorn Bringert <bringert at cs.chalmers.se> wrote:
>
> Hmm, perhaps I should clarify this: parsedate and time-1.1.1 (which
> comes with GHC 6.6.1) have different APIs. parsedate produces
> CalendarTimes, and the code in time-1.1.1 produces the new time and
> date data types. So I guess parsedate isn't actually obsolete,
> rather, it's for use with the package currently known as 'old-time'.
>
> Given this date string:
>
>   2008-06-26T11:00:00.000-07:00
>
> The problem is the parseTime function in Data.Time.Format is a  
> little too strict. The following GHCi session shows the different  
> behaviors. Notice how %Z is unable to parse the time zone offset in  
> any case. First we try parseTime:
>
>   > :m + Data.Time System.Time.Parse System.Locale
>   > let dateStr = "2008-06-26T11:00:00.000-07:00"
>   > parseTime defaultTimeLocale "%FT%X.000%z" dateStr :: Maybe UTCTime
>   Nothing
>   > parseTime defaultTimeLocale "%FT%X.000-%z" dateStr :: Maybe  
> UTCTime
>   Nothing
>   > parseTime defaultTimeLocale "%FT%X.000" dateStr :: Maybe UTCTime
>   Nothing

I guess you really want a ZonedTime here, if you want to retain the  
time zone info.

It seems like %z and %Z require 4 digits for a time zone offset,  
without a colon. This works:

 > parseTime defaultTimeLocale "%FT%X.000%z"  
"2008-06-26T11:00:00.000-0700" :: Maybe ZonedTime
Just 2008-06-26 11:00:00 -0700

Should we just add XX:XX as an alternative time zone offset format  
accepted by %z and %Z? Is this a standard format?

> Now parseCalendarTime from the parseDate package:
>
>   > parseCalendarTime defaultTimeLocale "%Y-%m-%dT%H:%M:%S" dateStr
>   Just (CalendarTime {ctYear = 2008, ctMonth = June, ctDay = 26,  
> ctHour = 11, ctMin = 0, ctSec = 0, ctPicosec = 0, ctWDay
> = Thursday, ctYDay = 1, ctTZName = "UTC", ctTZ = 0, ctIsDST = False})
>   > parseCalendarTime defaultTimeLocale "%Y-%m-%dT%H:%M:%S.000%Z"  
> dateStr
>   Nothing

Hmm, ok, parsedate allows garbage at the end. I wonder what is the  
right thing to do here.

/Björn





More information about the Haskell-Cafe mailing list