简体   繁体   English

如何将(Integer,Int,Int)转换为Day。 无法将预期的类型“天”与实际类型“((Integer,Int,Int)””匹配

[英]How to convert (Integer, Int, Int ) to Day. Couldn't match expected type ‘Day’ with actual type ‘(Integer, Int, Int)’

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!"

I need retrievedDate (Integer,Int,Int) conver to Day. 我需要retrieveDate(Integer,Int,Int)转换为Day。 The task is: subtract from (forecastDay - retrievedDay) But I can not do this, since I need retrievedDate convert to Day 任务是:从(forecastDay-UpdateddDay)中减去,但是我无法做到这一点,因为我需要将preparedDate转换为Day

Error Message: Couldn't match expected type 'Day' with actual type '(Integer, Int, Int)' 错误消息:无法将预期类型“ Day”与实际类型“(Integer,Int,Int)”匹配

42 | 42 | let diifedDays = diffDays (fromJust forecastDay) retrievedDate 让diifedDays = diffDays(fromJust ForecastDay)检索到日期

Well actually you already got a Day object, but by using toGregorian , you convert it into a triple (Integer, Int, Int) (the year, month, day according to the Gregorian calendar). 好吧,实际上您已经有一个Day对象,但是通过使用toGregorian ,您可以将其转换为三元组(Integer, Int, Int) (根据公历的年,月,日)。 So you can actually just drop the toGregorian function call: 因此,您实际上可以删除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!"

If you however need to convert the triple back, you can use the fromGregorian :: Integer -> Int -> Int -> Day function. 但是,如果需要将三元组转换回去,则可以使用fromGregorian :: Integer -> Int -> Int -> Day函数。 For a triple (Integer, Int, Int) , we can thus use: 因此,对于三元组(Integer, Int, Int) ,我们可以使用:

\(y, m, d) -> fromGregorian y m d

Besides that your code makes however a rather chaotic impression with a lot of let statements, show s, etc. So I would really advice you to clean it up. 除此之外,您的代码还带有很多let语句, show s等,给人留下了相当混乱的印象。所以我真的建议您清理一下。

What you need to fix your error is simply remove toGregorian and it will typecheck just fine. 您只需要删除toGregorian即可解决错误,只需进行类型toGregorian即可。

You might want to work on your logic though. 不过,您可能想根据自己的逻辑进行工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM