Skip to content

Commit 357d03f

Browse files
committed
feat: add date parser
1 parent a89bd88 commit 357d03f

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/Text/Megaparsec/Time.hs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{-# LANGUAGE FlexibleContexts #-}
22

33
module Text.Megaparsec.Time
4-
( dayParser
4+
( dateParser
5+
, dayParser
56
, durationParser
67
, hoursParser
78
, minutesParser
@@ -19,13 +20,20 @@ import Data.Time (DayOfWeek (..), NominalDiffTime,
1920
secondsToNominalDiffTime)
2021
import Data.Void (Void)
2122
import Text.Megaparsec (Parsec, try)
22-
import Text.Megaparsec.Char (char, digitChar, space, string')
23+
import Text.Megaparsec.Char (char, digitChar, space, space1,
24+
string')
2325
import Text.Megaparsec.Utils (posNumParser)
2426

25-
dayParser :: Parsec Void String (Either Int DayOfWeek)
27+
type DayResult = Either Int DayOfWeek
28+
29+
dateParser :: Parsec Void String (Maybe DayResult, TimeOfDay)
30+
dateParser = (,) <$> optional (try (dayParser <* space1)) <*> timeParser
31+
32+
dayParser :: Parsec Void String DayResult
2633
dayParser = choice
2734
[ Right <$> shortDay
2835
, Right <$> longDay
36+
, Left <$> (try (string' "yesterday") $> -1)
2937
, Left <$> (try (string' "tomorrow") $> 1)
3038
, Right <$> absoluteDay
3139
, Left <$> relativeDay

test/Text/Megaparsec/TimeSpec.hs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ import Test.Hspec (Spec, context, describe, it, shouldBe,
1717
import Test.QuickCheck (Arbitrary (..), Gen, elements, forAll,
1818
property, suchThat)
1919
import Text.Megaparsec (Parsec, errorBundlePretty, runParser)
20-
import Text.Megaparsec.Time (dayParser, durationParser, hoursParser,
21-
minutesParser, secondsParser, timeParser)
20+
import Text.Megaparsec.Time (dateParser, dayParser, durationParser,
21+
hoursParser, minutesParser,
22+
secondsParser, timeParser)
2223
import Text.Printf (printf)
2324

2425
instance Arbitrary TimeOfDay where
@@ -45,6 +46,15 @@ parseOrPrettyError p = first errorBundlePretty . runParser p "test"
4546

4647
spec :: Spec
4748
spec = do
49+
describe "date" $ do
50+
it "time only" $
51+
parseOrPrettyError dateParser "11:03" `shouldBe`
52+
Right (Nothing, TimeOfDay 11 3 0)
53+
54+
it "with day" $
55+
parseOrPrettyError dateParser "yesterday 11:03" `shouldBe`
56+
Right (Just (Left (-1)), TimeOfDay 11 3 0)
57+
4858
describe "day" $ do
4959
let weekday d = d < Saturday
5060

@@ -67,10 +77,10 @@ spec = do
6777

6878
context "tomorrow" $ do
6979
it "capitalized" $
70-
parseOrPrettyError dayParser "Tomorrow "`shouldBe` Right (Left 1)
80+
parseOrPrettyError dayParser "Tomorrow" `shouldBe` Right (Left 1)
7181

7282
it "lowercase" $
73-
parseOrPrettyError dayParser "tomorrow "`shouldBe` Right (Left 1)
83+
parseOrPrettyError dayParser "tomorrow" `shouldBe` Right (Left 1)
7484

7585
it "future day" . forAll (abs <$> arbitrary) $ \x ->
7686
parseOrPrettyError dayParser (printf "+%d" x) `shouldBe` Right (Left x)

0 commit comments

Comments
 (0)