Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Date Time in Haskell (Dananji Liyanage)
2. Re: Date Time in Haskell (Kostiantyn Rybnikov)
3. Re: Date Time in Haskell (Dananji Liyanage)
4. Re: Date Time in Haskell (Max Voit)
----------------------------------------------------------------------
Message: 1
Date: Thu, 14 May 2015 12:50:13 +0530
From: Dananji Liyanage <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] Date Time in Haskell
Message-ID:
<CAAPsY8wowgq0Mtbo89iY=t=ueswz1fmxj7o1vu9v-xu8-ej...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Hi All,
I'm trying out the date time conversions in Haskell.
How do I convert a keyboard input (IO String) to UTCTime?
This is how I'm doing (probably there's a better way),
convDay x = read (x ++ " 00:00:00") :: UTCTime
??
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?
--
Regards,
Dananji Liyanage
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150514/a7ae7af5/attachment-0001.html>
------------------------------
Message: 2
Date: Thu, 14 May 2015 13:25:21 +0300
From: Kostiantyn Rybnikov <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Date Time in Haskell
Message-ID:
<CAAbahfQnri=zoct3gzd048qbetdgdocgxz-jnznfhhc-pa4...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Hi Dananji!
First of all, for more explicit failure-handling I suggest using "readMay"
from package "safe" [0] instead of "read" whenever possible.
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.
Here's an example:
```
? ~ ghci
GHCi, version 7.8.4: http://www.haskell.org/ghc/ :? for help
Prelude> import Data.Time.Format
Prelude Data.Time.Format> import System.Locale
Prelude Data.Time.Format System.Locale> import Data.Time.Clock
Prelude Data.Time.Format System.Locale Data.Time.Clock> parseTime
defaultTimeLocale "%F %X" "2014-01-02 11:12:30" :: Maybe UTCTime
Just 2014-01-02 11:12:30 UTC
```
If you have a value of type "IO <something>", you can "extract" it when
being in IO monad like this:
main = do
currentTime <- getCurrentTime
-- in all remaining code, currentTime has type "UTCTime"
...
getDifference currentTime userTime
In terms of "sugar-less" way to use a value from "IO <something>", you can
also use it like this:
getCurrentTime >>= \currentTime -> doSomething currentTime
Hope this helps.
[0]: https://hackage.haskell.org/package/safe
[1]:
http://hackage.haskell.org/package/time-1.5.0.1/docs/Data-Time-Format.html
On Thu, May 14, 2015 at 10:20 AM, Dananji Liyanage <[email protected]>
wrote:
> Hi All,
>
> I'm trying out the date time conversions in Haskell.
>
> How do I convert a keyboard input (IO String) to UTCTime?
>
> This is how I'm doing (probably there's a better way),
> convDay x = read (x ++ " 00:00:00") :: UTCTime
> ??
>
> 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?
>
> --
> Regards,
> Dananji Liyanage
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150514/59ebba36/attachment-0001.html>
------------------------------
Message: 3
Date: Thu, 14 May 2015 16:27:46 +0530
From: Dananji Liyanage <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Date Time in Haskell
Message-ID:
<caapsy8zxyirizgvmafq7_mzccmcsyc5mx0yt-uoxmuucb9e...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Hi Kostiantyn,
Thanks for introducing me to package "Safe" !!
main = do
currentTime <- getCurrentTime
-- in all remaining code, currentTime has type "UTCTime"
...
getDifference currentTime userTime
This doesn't work for me.
Correct me if I'm doing something wrong here. Here's my code:
main = do
now <- getCurrentTime
diffUTCTime now bree
bree = readMay "1981-06-16 04:35:25" :: UTCTime
This gives an error like this;
io.hs:28:5:
Couldn't match expected type ?IO b?
with actual type ?NominalDiffTime?
Relevant bindings include main :: IO b (bound at io.hs:26:1)
In a stmt of a 'do' block: diffUTCTime now bree
In the expression:
do { now <- getCurrentTime;
diffUTCTime now bree }
On Thu, May 14, 2015 at 3:55 PM, Kostiantyn Rybnikov <[email protected]> wrote:
> Hi Dananji!
>
> First of all, for more explicit failure-handling I suggest using "readMay"
> from package "safe" [0] instead of "read" whenever possible.
>
> 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.
>
> Here's an example:
>
> ```
> ? ~ ghci
> GHCi, version 7.8.4: http://www.haskell.org/ghc/ :? for help
> Prelude> import Data.Time.Format
> Prelude Data.Time.Format> import System.Locale
> Prelude Data.Time.Format System.Locale> import Data.Time.Clock
> Prelude Data.Time.Format System.Locale Data.Time.Clock> parseTime
> defaultTimeLocale "%F %X" "2014-01-02 11:12:30" :: Maybe UTCTime
> Just 2014-01-02 11:12:30 UTC
> ```
>
> If you have a value of type "IO <something>", you can "extract" it when
> being in IO monad like this:
>
> main = do
> currentTime <- getCurrentTime
> -- in all remaining code, currentTime has type "UTCTime"
> ...
> getDifference currentTime userTime
>
> In terms of "sugar-less" way to use a value from "IO <something>", you can
> also use it like this:
>
> getCurrentTime >>= \currentTime -> doSomething currentTime
>
> Hope this helps.
>
> [0]: https://hackage.haskell.org/package/safe
> [1]:
> http://hackage.haskell.org/package/time-1.5.0.1/docs/Data-Time-Format.html
>
>
> On Thu, May 14, 2015 at 10:20 AM, Dananji Liyanage <[email protected]>
> wrote:
>
>> Hi All,
>>
>> I'm trying out the date time conversions in Haskell.
>>
>> How do I convert a keyboard input (IO String) to UTCTime?
>>
>> This is how I'm doing (probably there's a better way),
>> convDay x = read (x ++ " 00:00:00") :: UTCTime
>> ??
>>
>> 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?
>>
>> --
>> Regards,
>> Dananji Liyanage
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
--
Regards,
Dananji Liyanage
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150514/727546b0/attachment-0001.html>
------------------------------
Message: 4
Date: Thu, 14 May 2015 13:27:49 +0200
From: Max Voit <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Date Time in Haskell
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII
On Thu, 14 May 2015 16:27:46 +0530
Dananji Liyanage <[email protected]> wrote:
> Correct me if I'm doing something wrong here. Here's my code:
>
> main = do
> now <- getCurrentTime
> diffUTCTime now bree
> bree = readMay "1981-06-16 04:35:25" :: UTCTime
There are several problems with that code. First what the compiler is
complaining about: Last statement in a do-block must have type of the
block, in this case IO () - so print it or return ()
For the readMay - this is expected to return Maybe UTCTime. To stuff
this into diffUTCTime you need to get it out of the Maybe.
For my formulation "is expected to" - it won't, as UTCTime has no Read
instance (so you can't call read on it). Take a look at ParseTime and
its buildTime method.
cheers, max
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 83, Issue 37
*****************************************