Follow upstream changes -- Bytestring updates
[git-darcs-import.git] / src / DateTester.lhs
blobe35413f05cd4ebcc8fe19508fd6c563f31d939e3
1 % Copyright (C) 2008 Eric Kow
3 % This program is free software; you can redistribute it and/or modify
4 % it under the terms of the GNU General Public License as published by
5 % the Free Software Foundation; either version 2, or (at your option)
6 % any later version.
8 % This program is distributed in the hope that it will be useful,
9 % but WITHOUT ANY WARRANTY; without even the implied warranty of
10 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 % GNU General Public License for more details.
13 % You should have received a copy of the GNU General Public License
14 % along with this program; see the file COPYING. If not, write to
15 % the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 % Boston, MA 02110-1301, USA.
18 To be loaded with GHCi
20 \begin{verbatim}
21 make ghci
22 Prelude>:l DateTester
23 *DateTester>testDate "2008/05/22 10:34"
24 *DateTester>testDateAt "2006-03-22 09:36" "2008/05/22 10:34"
25 \end{verbatim}
27 You could also just compile it if you want, but I don't see
28 the point.
30 \begin{code}
31 module DateTester where
33 import DateMatcher
34 import IsoDate
35 import System.Time
37 -- | 'testDate' @d@ shows the possible interpretations
38 -- for the date string @d@ and how they match against
39 -- the current date
40 testDate :: String -> IO ()
41 testDate d =
42 do now <- getClockTime >>= toCalendarTime
43 testDateAtCal now d
45 -- | 'testDate' @iso d@ shows the possible interpretations
46 -- for the date string @d@ and how they match against
47 -- the date represented by the ISO 8601 string @iso@
48 testDateAt :: String -> String -> IO ()
49 testDateAt iso d = testDateAtCal (readUTCDate iso) d
51 -- | helper function for 'testDate' and 'testDateAt'
52 testDateAtCal :: CalendarTime -> String -> IO ()
53 testDateAtCal c d =
54 do ms <- getMatchers d
55 putStr . unlines . map (showMatcher c) $ ms
57 -- | 'showMatcher' @c dm@ tells us if @dm@ applies to
58 -- 'CalendarTime' @c@; or if @dm@ just represents the
59 -- failure to parse a date, in which case @c@ is moot.
60 showMatcher :: CalendarTime -> DateMatcher -> String
61 showMatcher now (DM n p m) =
62 "==== " ++ n ++ " ====\n" ++
63 (case p of
64 Left err -> shows err ""
65 Right x -> show x ++ "\n" ++ (show $ m x now))
66 \end{code}