main :: IO ()
main = do
Prelude.putStrLn "Please,enter date YYYY-MM-DD"
currentTime <- getCurrentTime
date <- Prelude.getLine
let sTime = show currentTime
let retrievedDate = toGregorian $ utctDay currentTime
let forecastDay = parseTimeM True defaultTimeLocale "%Y-%-m-%-d" date :: Maybe Day
let diifedDays = diffDays (fromJust forecastDay) retrievedDate
if date >= show retrievedDate && diifedDays > 0 && diifedDays <= 16
then print date
else print "Time Error!"
我需要retrieveDate(Integer,Int,Int)转换为Day。 任务是:减去(forecastDay - discoveredDay) 但我不能这样做,因为我需要retrieveDate转换为Day
错误讯息: 无法匹配预期类型'Day' 实际类型'(整数,整数,国际)'
42 |让diifedDays = diffDays(fromJust forecastDay)retrieveDate
答案 0 :(得分:2)
实际上你已经有了Day
个对象,但是使用toGregorian
,你可以将它转换为三个(Integer, Int, Int)
(年,月,日根据 Gregorian 日历)。所以你实际上可以放弃toGregorian
函数调用:
main :: IO ()
main = do
Prelude.putStrLn "Please,enter date YYYY-MM-DD"
currentTime <- getCurrentTime
date <- Prelude.getLine
let sTime = show currentTime
let retrievedDate = utctDay currentTime
let forecastDay = parseTimeM True defaultTimeLocale "%Y-%-m-%-d" date :: Maybe Day
let diifedDays = diffDays (fromJust forecastDay) retrievedDate
if date >= show retrievedDate && diifedDays > 0 && diifedDays <= 16
then print date
else print "Time Error!"
如果您需要转换三联,则可以使用fromGregorian :: Integer -> Int -> Int -> Day
功能。对于三(Integer, Int, Int)
,我们可以使用:
\(y, m, d) -> fromGregorian y m d
除此之外,您的代码会产生相当混乱的印象,包含大量let
语句,show
等等。所以我真的建议您清理它。
答案 1 :(得分:2)
修复错误所需要的只是删除toGregorian
,它会很好地解决问题。
你可能想要处理你的逻辑。