<div dir="ltr"><div class="gmail_default" style="font-size:small"><font face="arial, helvetica, sans-serif">Hi Kostiantyn,</font></div><div class="gmail_default" style="font-size:small"><font face="arial, helvetica, sans-serif"><br></font></div><div class="gmail_default" style="font-size:small"><font face="arial, helvetica, sans-serif">Thanks for introducing me to package "Safe" !!</font></div><div class="gmail_default" style="font-family:georgia,serif;font-size:small"><br></div><div class="gmail_default" style="font-family:georgia,serif;font-size:small"><div style="font-family:arial,sans-serif;font-size:12.8000001907349px">main = do<br></div><div style="font-family:arial,sans-serif;font-size:12.8000001907349px">    currentTime <- getCurrentTime<br></div><div style="font-family:arial,sans-serif;font-size:12.8000001907349px">    -- in all remaining code, currentTime has type "UTCTime"<br>    ...<br></div><div style="font-family:arial,sans-serif;font-size:12.8000001907349px">    getDifference currentTime userTime</div><div style="font-family:arial,sans-serif;font-size:12.8000001907349px"><br></div><div style="font-family:arial,sans-serif;font-size:12.8000001907349px">This doesn't work for me.</div><div style="font-family:arial,sans-serif;font-size:12.8000001907349px"><br></div><div style="font-family:arial,sans-serif;font-size:12.8000001907349px">Correct me if I'm doing something wrong here. Here's my code:</div><div style="font-family:arial,sans-serif;font-size:12.8000001907349px"><br></div><div style="font-family:arial,sans-serif;font-size:12.8000001907349px">           <span style="font-size:12.8000001907349px">main = do</span></div><div style="font-family:arial,sans-serif;font-size:12.8000001907349px">                  now <- getCurrentTime</div><div style="font-family:arial,sans-serif;font-size:12.8000001907349px">                  diffUTCTime now bree</div><div style="font-family:arial,sans-serif;font-size:12.8000001907349px"><span class="" style="white-space:pre"> </span></div><div style="font-family:arial,sans-serif;font-size:12.8000001907349px">          bree = readMay "1981-06-16 04:35:25" :: UTCTime</div><div style="font-family:arial,sans-serif;font-size:12.8000001907349px"><br></div><div style="font-family:arial,sans-serif;font-size:12.8000001907349px">This gives an error like this;</div><div style="font-family:arial,sans-serif;font-size:12.8000001907349px"><div style="font-size:12.8000001907349px">io.hs:28:5:</div><div style="font-size:12.8000001907349px">    Couldn't match expected type ‘IO b’</div><div style="font-size:12.8000001907349px">                with actual type ‘NominalDiffTime’</div><div style="font-size:12.8000001907349px">    Relevant bindings include main :: IO b (bound at io.hs:26:1)</div><div style="font-size:12.8000001907349px">    In a stmt of a 'do' block: diffUTCTime now bree</div><div style="font-size:12.8000001907349px">    In the expression:</div><div style="font-size:12.8000001907349px">      do { now <- getCurrentTime;</div><div style="font-size:12.8000001907349px">           diffUTCTime now bree }</div></div><div style="font-family:arial,sans-serif;font-size:12.8000001907349px"><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, May 14, 2015 at 3:55 PM, Kostiantyn Rybnikov <span dir="ltr"><<a href="mailto:k-bx@k-bx.com" target="_blank">k-bx@k-bx.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div>Hi Dananji!<br><br></div>First of all, for more explicit failure-handling I suggest using "readMay" from package "safe" [0] instead of "read" whenever possible.<br><br></div>Alternative to using "read", if you know date format of input-data, you can take a look at "UNIX-style parsing" section at Data.Time.Format module [1]. If you have older "time" package, you'd probably use parseTime, for newer versions it is recommended to use parseTimeM.<br><br></div>Here's an example:<br><br>```<br>➜  ~  ghci<br>GHCi, version 7.8.4: <a href="http://www.haskell.org/ghc/" target="_blank">http://www.haskell.org/ghc/</a>  :? for help<br>Prelude> import Data.Time.Format<br>Prelude Data.Time.Format> import System.Locale<br>Prelude Data.Time.Format System.Locale> import Data.Time.Clock<br>Prelude Data.Time.Format System.Locale Data.Time.Clock> parseTime defaultTimeLocale "%F %X" "2014-01-02 11:12:30" :: Maybe UTCTime<br>Just 2014-01-02 11:12:30 UTC<br>```<br><br><div><div>If you have a value of type "IO <something>", you can "extract" it when being in IO monad like this:<br><br></div><div>main = do<br></div><div>    currentTime <- getCurrentTime<br></div><div>    -- in all remaining code, currentTime has type "UTCTime"<br>    ...<br></div><div>    getDifference currentTime userTime<br></div><div><br></div><div>In terms of "sugar-less" way to use a value from "IO <something>", you can also use it like this:<br><br></div><div>getCurrentTime >>= \currentTime -> doSomething currentTime<br><br></div><div>Hope this helps.<br></div><div><br>[0]: <a href="https://hackage.haskell.org/package/safe" target="_blank">https://hackage.haskell.org/package/safe</a><br>[1]: <a href="http://hackage.haskell.org/package/time-1.5.0.1/docs/Data-Time-Format.html" target="_blank">http://hackage.haskell.org/package/time-1.5.0.1/docs/Data-Time-Format.html</a><br><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="h5">On Thu, May 14, 2015 at 10:20 AM, Dananji Liyanage <span dir="ltr"><<a href="mailto:dan9131@gmail.com" target="_blank">dan9131@gmail.com</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5"><div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif;font-size:small">Hi All,</div><div class="gmail_default" style="font-family:georgia,serif;font-size:small"><br></div><div class="gmail_default" style="font-family:georgia,serif;font-size:small">I'm trying out the date time conversions in Haskell.</div><div class="gmail_default" style="font-family:georgia,serif;font-size:small"><br></div><div class="gmail_default" style="font-family:georgia,serif;font-size:small">How do I convert a keyboard input (IO String) to UTCTime?</div><div class="gmail_default" style="font-family:georgia,serif;font-size:small"><br></div><div class="gmail_default" style="font-family:georgia,serif;font-size:small">This is how I'm doing (probably there's a better way), </div><div class="gmail_default" style="font-family:georgia,serif;font-size:small">              convDay x = read (x ++ " 00:00:00") :: UTCTime</div><div><div class="gmail_default" style="font-family:georgia,serif;font-size:small">​​</div></div><div class="gmail_default" style="font-family:georgia,serif;font-size:small"><br></div><div class="gmail_default" style="font-family:georgia,serif;font-size:small">Another thing, how do I convert getCurrentTime (IO UTCTime) to UTCTime in order to get the time difference between the keyboard input date and today?</div><span><font color="#888888"><div><br></div>-- <br><div><div dir="ltr"><div><div><div><font face="georgia,serif">Regards, <br>Dananji Liyanage</font></div></div></div></div></div>
</font></span></div>
<br></div></div>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>
<br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><div><div><font face="georgia,serif">Regards, <br>Dananji Liyanage</font></div></div></div></div></div>
</div>