A
A
AshotBes2018-06-02 14:27:22
Haskell
AshotBes, 2018-06-02 14:27:22

How to subtract the current date from the date entered by the user?

The task is this: I have to compare two dates (date - from the user, and the current date currentTime) Take the date from the user and subtract the current date - and get the difference in days. And this difference should be no more than 16 That is, the date from the user should be not earlier than the current one (so as not to fly into the past), but not later than "current + 16 days"

getDateFromUser :: IO (Either UserError Date)
 getDateFromUser = do
     Prelude.putStrLn "Пожалуйста, укажите дату для прогноза в формате    ГГГГ-ММ-ДД:"
     date <- Prelude.getLine
     currentTime <- getCurrentTime
     let forecastDay = parseTimeM True defaultTimeLocale "%Y-%m-%d" date :: Maybe Day
     if date >= ...
        then return $ Right $ (Data.Text.pack date)
        else return $ Left InvalidDate

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
youngmysteriouslight, 2018-06-02
@AshotBes

compare two dates [...] subtract the current date - and get the difference in days. And this difference should be no more than 16. That is, the date from the user should not be earlier than the current one (so as not to fly into the past), but no later than "current + 16 days"

-- количество дней (но не меньше 0 и не больше 16) от сего дня (today) до указанного дня (day)
-- возвращаемое число лежит в интервале [0, 16]
strippedDayDiff :: Day -> Day -> Integer
strippedDayDiff day today = let d = diffDays day today in if d > 16 then 16 else if d < 0 then 0 else d

Usage:
if strippedDayDiff forecastDay (utctDay currentTime) >= ...

If you need to check that the difference lies exactly in the range [0, 16], then
PS Sergey , there are more highly specialized functions there. I think the main problem initially was that the types were not specified in the code and / or the choice of return values ​​did not correspond to the task set orally.

S
Sergey, 2018-06-02
@butteff

I do not know haskell
But the essence is this:
1. Take the current unixtimestamp
2. Convert the date from the user to unixtimestamp
3. Subtract the second from the first
4. Convert the date again from the resulting result.
As I understand it, there are functions for all this .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question