2

I want to add/subtract given number of minutes to/from given given time and find out the resulting time.

e.g. Suppose the given time is 11:30AM and number of minutes to add are 100 then the resulting time is 01:10PM

How to do it in Haskell using the Data-Time library? I tried reading the docs on the Haskell site but couldn't get almost anything out of it.

There is no example shown on that documentation site. Also the cookbook on the Haskell site doesn't contain any example for time calculations.

Edit: no need for current time, it should work for any given time. Time may be given as a string like "11:30".

Chris Stryczynski
34.8k60 gold badges210 silver badges334 bronze badges
asked Oct 2, 2016 at 9:46
2
  • 4
    Time is a prime example of where you'd need vector-space, which distinguishes between points (timestamps) and vectors (time intervals). I suspect this library is lacking this basic functionality. It seems you could use dayFractionToTimeOfDay and its inverse, but you can't cross midnights that way. Commented Oct 2, 2016 at 9:55
  • As pointed by Sibi there is a distinction between UTCTime (timestamp) and NominalDiffTime (duration). Maybe they indeed could do with vector-spaces instances. Commented Oct 2, 2016 at 11:20

2 Answers 2

7

A sample demo of adding 100 minutes:

λ> import Data.Time
λ> currentTime <- getCurrentTime
λ> currentTime
2016年10月02日 10:27:03.30961 UTC
λ> currentZone <- getCurrentTimeZone
λ> currentZone
IST
λ> utcToLocalTime currentZone currentTime
2016年10月02日 15:57:03.30961
λ> let hundredMinutes = 100 * 60
λ> addUTCTime hundredMinutes currentTime
2016年10月02日 12:07:03.30961 UTC
λ> let newTime = addUTCTime hundredMinutes currentTime
λ> utcToLocalTime currentZone newTime
2016年10月02日 17:37:03.30961

The addUTCTime function is used for adding 100 minutes. (削除) An equivalent function for subtracting is also available. (削除ここまで)

answered Oct 2, 2016 at 10:31
Sign up to request clarification or add additional context in comments.

8 Comments

Note diffUtcTime as a different signature than addUTCTime .
@mb14 Oh yeah, you are right. Well, then for subtracting just have hundredMinutes as - 100 * 60. :)
I don't want to use IO monad, I have a time value in the program itself. Also I don't need only the current time. Edited to reflect this. Sorry for not making it clear earlier.
@mntk123 You don't need to use IO monad. But you need a function for converting your time to either LocalTime or UTCTime.
@Sibi Thanks again. The most frustrating thing with the docs found on Haskell site is that they don't show even a single example in most cases :( It sucks. Compare that to Python. Anyway, I will try. I will ask a separate question for this.
|
1

You can use Data.Time.Clock.getCurrentTime to fetch the current time. This returns IO UTCTime.

You can then use Data.Time.Clock.addUTCTime to add minutes to this previously fetched time.

import Data.Time
currentTimePlusMinutes minutes = getCurrentTime 
 >>= (\time -> return (addUTCTime (minutes * 60) time))

If you don't want to use the current time, you can then simply do

 addMinutes :: NominalDiffTime -> UTCTime -> UTCTime
 addMinutes minutes = addUTCTime (minutes * 60)
 λ> addMinutes 10 time
 2016年10月02日 12:41:09.68297 UTC
answered Oct 2, 2016 at 12:13

7 Comments

I'm still a beginner with haskell, and I'm not sure the >>= (\t -> ...) notation is the best way to write this. Anyone has recommandations?
what is time in addMinutes 10 time?
The "given time" mentioned in the question.
You can figure that out yourself using :t addMinutes within ghci. In this instance, it outputs addMinutes :: NominalDiffTime -> UTCTime -> UTCTime, so time needs to be a UTCTime.
This is a complex topic. You probably should post another question if you want an answer to that.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.