Skip to content

Commit

Permalink
fix parseTime warnings with time 1.5+ (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmichael committed Mar 29, 2015
1 parent e838ed0 commit f8a24cc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
16 changes: 13 additions & 3 deletions hledger-lib/Hledger/Data/Dates.hs
Original file line number Diff line number Diff line change
Expand Up @@ -427,13 +427,23 @@ nthdayofweekcontaining n d | d1 >= d = d1
-- parseTime defaultTimeLocale "%Y-%m-%d %H:%M:%S" s
-- ]

parsetime :: ParseTime t => TimeLocale -> String -> String -> Maybe t
parsetime =
#if MIN_VERSION_time(1,5,0)
parseTimeM True
#else
parseTime
#endif


-- | Parse a couple of date string formats to a time type.
parsedateM :: String -> Maybe Day
parsedateM s = firstJust [
parseTime defaultTimeLocale "%Y/%m/%d" s,
parseTime defaultTimeLocale "%Y-%m-%d" s
parsetime defaultTimeLocale "%Y/%m/%d" s,
parsetime defaultTimeLocale "%Y-%m-%d" s
]


-- -- | Parse a date-time string to a time type, or raise an error.
-- parsedatetime :: String -> LocalTime
-- parsedatetime s = fromMaybe (error' $ "could not parse timestamp \"" ++ s ++ "\"")
Expand All @@ -447,7 +457,7 @@ parsedate s = fromMaybe (error' $ "could not parse date \"" ++ s ++ "\"")
-- | Parse a time string to a time type using the provided pattern, or
-- return the default.
parsetimewith :: ParseTime t => String -> String -> t -> t
parsetimewith pat s def = fromMaybe def $ parseTime defaultTimeLocale pat s
parsetimewith pat s def = fromMaybe def $ parsetime defaultTimeLocale pat s

{-|
Parse a date in any of the formats allowed in ledger's period expressions,
Expand Down
9 changes: 7 additions & 2 deletions hledger-lib/Hledger/Data/TimeLog.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Data.Time.Calendar
import Data.Time.Clock
import Data.Time.Format
import Data.Time.LocalTime
#if !MIN_VERSION_time(1,5,0)
#if !(MIN_VERSION_time(1,5,0))
import System.Locale (defaultTimeLocale)
#endif
import Test.HUnit
Expand Down Expand Up @@ -112,7 +112,12 @@ tests_Hledger_Data_TimeLog = TestList [
nowstr = showtime now
yesterday = prevday today
clockin = TimeLogEntry nullsourcepos In
mktime d = LocalTime d . fromMaybe midnight . parseTime defaultTimeLocale "%H:%M:%S"
mktime d = LocalTime d . fromMaybe midnight .
#if MIN_VERSION_time(1,5,0)
parseTimeM True defaultTimeLocale "%H:%M:%S"
#else
parseTime defaultTimeLocale "%H:%M:%S"
#endif
showtime = formatTime defaultTimeLocale "%H:%M"
assertEntriesGiveStrings name es ss = assertEqual name ss (map tdescription $ timeLogEntriesToTransactions now es)

Expand Down
12 changes: 9 additions & 3 deletions hledger-lib/Hledger/Read/CsvReader.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ import Data.List
import Data.Maybe
import Data.Ord
import Data.Time.Calendar (Day)
import Data.Time.Format (parseTime)
#if MIN_VERSION_time(1,5,0)
import Data.Time.Format (defaultTimeLocale)
import Data.Time.Format (parseTimeM, defaultTimeLocale)
#else
import Data.Time.Format (parseTime)
import System.Locale (defaultTimeLocale)
#endif
import Safe
Expand Down Expand Up @@ -720,7 +720,13 @@ renderTemplate rules record t = regexReplaceBy "%[A-z0-9]+" replace t
parseDateWithFormatOrDefaultFormats :: Maybe DateFormat -> String -> Maybe Day
parseDateWithFormatOrDefaultFormats mformat s = firstJust $ map parsewith formats
where
parsewith = flip (parseTime defaultTimeLocale) s
parsetime =
#if MIN_VERSION_time(1,5,0)
parseTimeM True
#else
parseTime
#endif
parsewith = flip (parsetime defaultTimeLocale) s
formats = maybe
["%Y/%-m/%-d"
,"%Y-%-m-%-d"
Expand Down

0 comments on commit f8a24cc

Please sign in to comment.