<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/">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">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">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">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><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><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 class="HOEnZb"><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>_______________________________________________<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></div>