0
votes
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. The task is: subtract from (forecastDay - retrievedDay) But I can not do this, since I need retrievedDate convert to Day

Error Message: Couldn't match expected type ‘Day’ with actual type ‘(Integer, Int, Int)’

42 | let diifedDays = diffDays (fromJust forecastDay) retrievedDate

2
@WillemVanOnsem type Day from Data.Time I need convert (Integer,Int,Int) --->>>> DayAshot Bes

2 Answers

2
votes

What you need to fix your error is simply remove toGregorian and it will typecheck just fine.

You might want to work on your logic though.

2
votes

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). So you can actually just drop the toGregorian function call:

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. For a triple (Integer, Int, Int), we can thus use:

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

Besides that your code makes however a rather chaotic impression with a lot of let statements, shows, etc. So I would really advice you to clean it up.