{-# LANGUAGE GADTs #-}
{-# LANGUAGE NoRebindableSyntax #-}
{-# LANGUAGE OverloadedStrings #-}
module Duckling.Time.EN.US.Rules
( rules
, ruleMMDD
, ruleMMDDYYYY
, ruleMMDDYYYYDot
, ruleThanksgiving
) where
import Data.Maybe
import Prelude
import Duckling.Dimensions.Types
import Duckling.Numeral.Helpers (parseInt)
import Duckling.Regex.Types
import Duckling.Time.Helpers
import Duckling.Time.Types (TimeData (..))
import Duckling.Types
ruleMMDD :: Rule
ruleMMDD = Rule
{ name = "mm/dd"
, pattern =
[ regex "(1[0-2]|0?[1-9])\\s?[/-]\\s?(3[01]|[12]\\d|0?[1-9])"
]
, prod = \tokens -> case tokens of
(Token RegexMatch (GroupMatch (mm:dd:_)):_) -> do
m <- parseInt mm
d <- parseInt dd
tt $ monthDay m d
_ -> Nothing
}
ruleMMDDYYYY :: Rule
ruleMMDDYYYY = Rule
{ name = "mm/dd/yyyy"
, pattern =
[ regex "(1[0-2]|0?[1-9])[/-](3[01]|[12]\\d|0?[1-9])[/-](\\d{2,4})"
]
, prod = \tokens -> case tokens of
(Token RegexMatch (GroupMatch (mm:dd:yy:_)):_) -> do
y <- parseInt yy
m <- parseInt mm
d <- parseInt dd
tt $ yearMonthDay y m d
_ -> Nothing
}
ruleMMDDYYYYDot :: Rule
ruleMMDDYYYYDot = Rule
{ name = "mm.dd.yyyy"
, pattern =
[ regex "(1[0-2]|0?[1-9])\\.(3[01]|[12]\\d|0?[1-9])\\.(\\d{4})"
]
, prod = \tokens -> case tokens of
(Token RegexMatch (GroupMatch (mm:dd:yy:_)):_) -> do
y <- parseInt yy
m <- parseInt mm
d <- parseInt dd
tt $ yearMonthDay y m d
_ -> Nothing
}
ruleThanksgiving :: Rule
ruleThanksgiving = Rule
{ name = "Thanksgiving Day"
, pattern =
[ regex "thanks?giving( day)?"
]
, prod = \_ -> tt . mkOkForThisNext $ nthDOWOfMonth 4 4 11
}
rules :: [Rule]
rules =
[ ruleMMDD
, ruleMMDDYYYY
, ruleMMDDYYYYDot
, ruleThanksgiving
]