
How can I get the current system date-time in Haskell? I've used the library Time, but on MacOS X 10.6.4 the year is always 0. Luca.

On Wed, Jun 23, 2010 at 08:03:16PM +0200, Luca Ciciriello wrote:
How can I get the current system date-time in Haskell?
I've used the library Time, but on MacOS X 10.6.4 the year is always 0.
Luca.
Do you mean, you used getCurrentTime from the time package[1]? What does the following line do? (toGregorian . utctDay) `fmap` getCurrentTime On my system, GHCi, version 6.10.4: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer ... linking ... done. Loading package base ... linking ... done. Prelude> :m Data.Time Prelude Data.Time> (toGregorian . utctDay) `fmap` getCurrentTime Loading package old-locale-1.0.0.1 ... linking ... done. Loading package time-1.1.4 ... linking ... done. (2010,6,23) http://hackage.haskell.org/packages/archive/time/1.2.0.3/doc/html/Data-Time-... HTH, -- Felipe.

Thanks Felipe. Yes I use getCurrentTime. But I found I a stupid error in my code. Now all works fine. Thanks anyway for your answer. Luca. On Jun 23, 2010, at 8:13 PM, Felipe Lessa wrote:
On Wed, Jun 23, 2010 at 08:03:16PM +0200, Luca Ciciriello wrote:
How can I get the current system date-time in Haskell?
I've used the library Time, but on MacOS X 10.6.4 the year is always 0.
Luca.
Do you mean, you used getCurrentTime from the time package[1]? What does the following line do?
(toGregorian . utctDay) `fmap` getCurrentTime
On my system,
GHCi, version 6.10.4: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer ... linking ... done. Loading package base ... linking ... done. Prelude> :m Data.Time Prelude Data.Time> (toGregorian . utctDay) `fmap` getCurrentTime Loading package old-locale-1.0.0.1 ... linking ... done. Loading package time-1.1.4 ... linking ... done. (2010,6,23)
http://hackage.haskell.org/packages/archive/time/1.2.0.3/doc/html/Data-Time-...
HTH,
-- Felipe.

Just a question. How can I obtain a String from currentTime :: IO Day currentTime = utctDay `fmap` getCurrentTime Here currentTime returns to me 2010-06-24, but I want "2010-06-24". In another worlds I need a function currentTimeStr :: IO Day -> String Any Idea? Luca. On Jun 23, 2010, at 8:13 PM, Felipe Lessa wrote:
On Wed, Jun 23, 2010 at 08:03:16PM +0200, Luca Ciciriello wrote:
How can I get the current system date-time in Haskell?
I've used the library Time, but on MacOS X 10.6.4 the year is always 0.
Luca.
Do you mean, you used getCurrentTime from the time package[1]? What does the following line do?
(toGregorian . utctDay) `fmap` getCurrentTime
On my system,
GHCi, version 6.10.4: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer ... linking ... done. Loading package base ... linking ... done. Prelude> :m Data.Time Prelude Data.Time> (toGregorian . utctDay) `fmap` getCurrentTime Loading package old-locale-1.0.0.1 ... linking ... done. Loading package time-1.1.4 ... linking ... done. (2010,6,23)
http://hackage.haskell.org/packages/archive/time/1.2.0.3/doc/html/Data-Time-...
HTH,
-- Felipe.

On 24 June 2010 10:52, Luca Ciciriello
Just a question. How can I obtain a String from
currentTime :: IO Day currentTime = utctDay `fmap` getCurrentTime
Here currentTime returns to me 2010-06-24, but I want "2010-06-24". In another worlds I need a function
I bet Day has a Show instance (that's why you get 2010-06-24 in ghci I suppose) Just use that.
currentTimeStr :: IO Day -> String
Getting out of IO? I would think again. Following might be what you really want: currentTimeStr :: IO Day -> IO String
Any Idea?
Luca.
Best, Ozgur

Starting from the beginning, my original problem is to create the string: "File creation date: " ++ currentSystemDate ++ " by MyCompany" to obtain the string "File creation date: 2010-06-24 by MyCompany" Probably this is really trivial, but I'm a real beginner in Haskell and this seems to me a big problem. Luca. On Jun 24, 2010, at 9:59 AM, Ozgur Akgun wrote:
On 24 June 2010 10:52, Luca Ciciriello
wrote: Just a question. How can I obtain a String from currentTime :: IO Day currentTime = utctDay `fmap` getCurrentTime
Here currentTime returns to me 2010-06-24, but I want "2010-06-24". In another worlds I need a function
I bet Day has a Show instance (that's why you get 2010-06-24 in ghci I suppose) Just use that.
currentTimeStr :: IO Day -> String
Getting out of IO? I would think again. Following might be what you really want:
currentTimeStr :: IO Day -> IO String
Any Idea?
Luca.
Best, Ozgur

Then you might want to use something like:
currentTimeStr :: IO String
currentTimeStr = do ct <- currentTime
return ("File creation date: " ++ show ct ++ " by
MyCompany")
Or more concisely,
currentTimeStr :: IO String
currentTimeStr = liftM (\ ct -> "File creation date: " ++ show ct ++ " by
MyCompany" ) currentTime
My point is, the type of currentTimeStr is IO String, not just String.
Because you are working on an IO value.
Best,
PS: If you are not confident with the piece of code I've suggested, please
have a look at the io/monads/do-notation sections of the Real World Haskell.
On 24 June 2010 11:17, Luca Ciciriello
Starting from the beginning, my original problem is to create the string:
"File creation date: " ++ currentSystemDate ++ " by MyCompany"
to obtain the string
"File creation date: 2010-06-24 by MyCompany"
Probably this is really trivial, but I'm a real beginner in Haskell and this seems to me a big problem.
Luca.
On Jun 24, 2010, at 9:59 AM, Ozgur Akgun wrote:
On 24 June 2010 10:52, Luca Ciciriello
wrote: Just a question. How can I obtain a String from
currentTime :: IO Day currentTime = utctDay `fmap` getCurrentTime
Here currentTime returns to me 2010-06-24, but I want "2010-06-24". In another worlds I need a function
I bet Day has a Show instance (that's why you get 2010-06-24 in ghci I suppose) Just use that.
currentTimeStr :: IO Day -> String
Getting out of IO? I would think again. Following might be what you really want:
currentTimeStr :: IO Day -> IO String
Any Idea?
Luca.
Best, Ozgur
-- Ozgur Akgun

Ok this solve my problem. I've just missed an IO before String in the type signature. Thanks. Luca. On Jun 24, 2010, at 11:16 AM, Ozgur Akgun wrote:
Then you might want to use something like:
currentTimeStr :: IO String currentTimeStr = do ct <- currentTime return ("File creation date: " ++ show ct ++ " by MyCompany")
Or more concisely,
currentTimeStr :: IO String currentTimeStr = liftM (\ ct -> "File creation date: " ++ show ct ++ " by MyCompany" ) currentTime
My point is, the type of currentTimeStr is IO String, not just String. Because you are working on an IO value.
Best,
PS: If you are not confident with the piece of code I've suggested, please have a look at the io/monads/do-notation sections of the Real World Haskell.
On 24 June 2010 11:17, Luca Ciciriello
wrote: Starting from the beginning, my original problem is to create the string: "File creation date: " ++ currentSystemDate ++ " by MyCompany"
to obtain the string
"File creation date: 2010-06-24 by MyCompany"
Probably this is really trivial, but I'm a real beginner in Haskell and this seems to me a big problem.
Luca.
On Jun 24, 2010, at 9:59 AM, Ozgur Akgun wrote:
On 24 June 2010 10:52, Luca Ciciriello
wrote: Just a question. How can I obtain a String from currentTime :: IO Day currentTime = utctDay `fmap` getCurrentTime
Here currentTime returns to me 2010-06-24, but I want "2010-06-24". In another worlds I need a function
I bet Day has a Show instance (that's why you get 2010-06-24 in ghci I suppose) Just use that.
currentTimeStr :: IO Day -> String
Getting out of IO? I would think again. Following might be what you really want:
currentTimeStr :: IO Day -> IO String
Any Idea?
Luca.
Best, Ozgur
-- Ozgur Akgun

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 6/24/10 03:52 , Luca Ciciriello wrote:
Just a question. How can I obtain a String from
currentTime :: IO Day currentTime = utctDay `fmap` getCurrentTime
Here currentTime returns to me 2010-06-24, but I want "2010-06-24". In another worlds I need a function
currentTimeStr :: IO Day -> String
You can't escape from IO, but you can lift a formatter into IO. Formatters are in Data.Time.Format; look at the documentation for your system's strftime() function to see how it works. - -- brandon s. allbery [linux,solaris,freebsd,perl] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAkwjZuIACgkQIn7hlCsL25UwpgCfRuhCW5sB9G8Ud9Ci3JUZbVTt vAIAn3CFOoX9Q4etASv3ggC8KFxm94W6 =ZUFJ -----END PGP SIGNATURE-----
participants (4)
-
Brandon S Allbery KF8NH
-
Felipe Lessa
-
Luca Ciciriello
-
Ozgur Akgun