
Hello, I am trying to use N. Mitchell's CmdArgs and I would like one of the fields to store the current time. A minimal example below: {-# LANGUAGE DeriveDataTypeable #-} import System.Console.CmdArgs import System.Time data FuzzyTimeConf = FuzzyTimeConf { time :: String } deriving (Show, Data, Typeable) ftConf = FuzzyTimeConf { time = confDefaultTime } confDefaultTime = "it is here that I would like to get the current time" main :: IO () main = do print =<< cmdArgs ftConf I want confDefaultTime to store the current time as HH:MM, so I guess I will also need to extract it in some way. I've tried many different versions and can't come up with anything working. confDefaultTime can't be an IO String because than it can't derive Show and Data which is required for cmdArgs to work in FuzzyTimeConf. However, I don't seem to be able to find a way to make it a String and hold the actual current time. Can you help me, please?

On Friday 14 January 2011 19:18:58, Kamil Stachowski wrote:
Hello,
I am trying to use N. Mitchell's CmdArgs and I would like one of the fields to store the current time. A minimal example below:
{-# LANGUAGE DeriveDataTypeable #-}
import System.Console.CmdArgs import System.Time
data FuzzyTimeConf = FuzzyTimeConf { time :: String } deriving (Show, Data, Typeable)
ftConf = FuzzyTimeConf { time = confDefaultTime }
confDefaultTime = "it is here that I would like to get the current time"
main :: IO () main = do print =<< cmdArgs ftConf
I want confDefaultTime to store the current time as HH:MM, so I guess I will also need to extract it in some way.
I've tried many different versions and can't come up with anything working. confDefaultTime can't be an IO String because than it can't derive Show and Data which is required for cmdArgs to work in FuzzyTimeConf. However, I don't seem to be able to find a way to make it a String and hold the actual current time.
Can you help me, please?
Two things spring to mind: a) don't use a top-level ftConf, get it inside IO in main: getFTConf :: IO FuzzyTimeConf getFTConf = do now <- getClockTime -- or whichever time you need return $ FuzzyTimeConf { time = convert now } main = do ftConf <- getFTConf print =<< cmdArgs ftConf b) if you absolutely have to have ftConf as a top-level name and it is safe here: import System.IO.Unsafe (unsafePerformIO) {-# NOINLINE ftConf #-} ftConf :: FuzzyTimeConf ftConf = unsafePerformIO $ do now <- getClockTime return $ FuzzyTimeConf { time = convert now } a) is preferable if it's possible to do thus. Cheers, Daniel

Wow, that was a quick answer! And it does just what I want. Thanks a lot!
Now, I'll just have to sit down and try to understand it :)
Thanks again,
Kamil Stachowski
On 14 January 2011 19:41, Daniel Fischer
On Friday 14 January 2011 19:18:58, Kamil Stachowski wrote:
Hello,
I am trying to use N. Mitchell's CmdArgs and I would like one of the fields to store the current time. A minimal example below:
{-# LANGUAGE DeriveDataTypeable #-}
import System.Console.CmdArgs import System.Time
data FuzzyTimeConf = FuzzyTimeConf { time :: String } deriving (Show, Data, Typeable)
ftConf = FuzzyTimeConf { time = confDefaultTime }
confDefaultTime = "it is here that I would like to get the current time"
main :: IO () main = do print =<< cmdArgs ftConf
I want confDefaultTime to store the current time as HH:MM, so I guess I will also need to extract it in some way.
I've tried many different versions and can't come up with anything working. confDefaultTime can't be an IO String because than it can't derive Show and Data which is required for cmdArgs to work in FuzzyTimeConf. However, I don't seem to be able to find a way to make it a String and hold the actual current time.
Can you help me, please?
Two things spring to mind: a) don't use a top-level ftConf, get it inside IO in main:
getFTConf :: IO FuzzyTimeConf getFTConf = do now <- getClockTime -- or whichever time you need return $ FuzzyTimeConf { time = convert now }
main = do ftConf <- getFTConf print =<< cmdArgs ftConf
b) if you absolutely have to have ftConf as a top-level name and it is safe here:
import System.IO.Unsafe (unsafePerformIO)
{-# NOINLINE ftConf #-} ftConf :: FuzzyTimeConf ftConf = unsafePerformIO $ do now <- getClockTime return $ FuzzyTimeConf { time = convert now }
a) is preferable if it's possible to do thus.
Cheers, Daniel

On Fri, Jan 14, 2011 at 07:41:07PM +0100, Daniel Fischer wrote:
b) if you absolutely have to have ftConf as a top-level name and it is safe here:
import System.IO.Unsafe (unsafePerformIO)
{-# NOINLINE ftConf #-} ftConf :: FuzzyTimeConf ftConf = unsafePerformIO $ do now <- getClockTime return $ FuzzyTimeConf { time = convert now }
There's already a Hackage package to accomplish this: http://hackage.haskell.org/package/acme-now =) -Brent

@Daniel Fischer
Thanks for the offer, it's very kind of you, but I'm afraid I'll just need
to try to understand monads for the hundredth time :/
@Brent Yorgey
Ah, thank you! But that would be the unsafe way, right? I think I should try
to do it the "right" way, but I'm sure this'll come in handy when I get
lost.
On 14 January 2011 20:00, Brent Yorgey
On Fri, Jan 14, 2011 at 07:41:07PM +0100, Daniel Fischer wrote:
b) if you absolutely have to have ftConf as a top-level name and it is
safe
here:
import System.IO.Unsafe (unsafePerformIO)
{-# NOINLINE ftConf #-} ftConf :: FuzzyTimeConf ftConf = unsafePerformIO $ do now <- getClockTime return $ FuzzyTimeConf { time = convert now }
There's already a Hackage package to accomplish this:
http://hackage.haskell.org/package/acme-now
=)
-Brent
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Fri, Jan 14, 2011 at 08:14:38PM +0100, Kamil Stachowski wrote:
@Daniel Fischer Thanks for the offer, it's very kind of you, but I'm afraid I'll just need to try to understand monads for the hundredth time :/
@Brent Yorgey Ah, thank you! But that would be the unsafe way, right? I think I should try to do it the "right" way, but I'm sure this'll come in handy when I get lost.
Yes, I was mostly linking to that package just because it is funny, it's probably not what you really want to do. =) -Brent
On 14 January 2011 20:00, Brent Yorgey
wrote: On Fri, Jan 14, 2011 at 07:41:07PM +0100, Daniel Fischer wrote:
b) if you absolutely have to have ftConf as a top-level name and it is
safe
here:
import System.IO.Unsafe (unsafePerformIO)
{-# NOINLINE ftConf #-} ftConf :: FuzzyTimeConf ftConf = unsafePerformIO $ do now <- getClockTime return $ FuzzyTimeConf { time = convert now }
There's already a Hackage package to accomplish this:
http://hackage.haskell.org/package/acme-now
=)
-Brent
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

It is funny, indeed :)
In fact, I am after all going to get unsafe because what Daniel Fischer
wrote above worked perfectly until I wanted to add annotations to ftConf
(the "&= summary" bit in http://community.haskell.org/~ndm/cmdargs/). The
only way I can go around the errors I get is precisely unsafe, so ah, you
only live once ;)
On 14 January 2011 21:36, Brent Yorgey
@Daniel Fischer Thanks for the offer, it's very kind of you, but I'm afraid I'll just need to try to understand monads for the hundredth time :/
@Brent Yorgey Ah, thank you! But that would be the unsafe way, right? I think I should
On Fri, Jan 14, 2011 at 08:14:38PM +0100, Kamil Stachowski wrote: try
to do it the "right" way, but I'm sure this'll come in handy when I get lost.
Yes, I was mostly linking to that package just because it is funny, it's probably not what you really want to do. =)
-Brent
On 14 January 2011 20:00, Brent Yorgey
wrote: On Fri, Jan 14, 2011 at 07:41:07PM +0100, Daniel Fischer wrote:
b) if you absolutely have to have ftConf as a top-level name and it
is
safe
here:
import System.IO.Unsafe (unsafePerformIO)
{-# NOINLINE ftConf #-} ftConf :: FuzzyTimeConf ftConf = unsafePerformIO $ do now <- getClockTime return $ FuzzyTimeConf { time = convert now }
There's already a Hackage package to accomplish this:
http://hackage.haskell.org/package/acme-now
=)
-Brent
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Friday 14 January 2011 21:45:49, Kamil Stachowski wrote:
It is funny, indeed :)
In fact, I am after all going to get unsafe because what Daniel Fischer wrote above worked perfectly until I wanted to add annotations to ftConf (the "&= summary" bit in http://community.haskell.org/~ndm/cmdargs/). The only way I can go around the errors I get is precisely unsafe, so ah, you only live once ;)
Well, you could try setTime :: FuzzyTimeConf -> IO FuzzyTimeConf setTime ftc = do now <- getClockTime return $ ftc{ time = convert now } -- put annotations here defaultFuzzyTimeConf = FuzzyTimeConf { ... } main = do realFTConf <- setTime defaultTimeConf print =<< cmdArgs realFTConf I haven't looked at cmdargs, so I've no idea whether that'll work, but it may be worth a try.

It works! Thank you!
I didn't know Haskell has such a great community.
Thanks again,
Kamil Stachowski
On 14 January 2011 23:40, Daniel Fischer
On Friday 14 January 2011 21:45:49, Kamil Stachowski wrote:
It is funny, indeed :)
In fact, I am after all going to get unsafe because what Daniel Fischer wrote above worked perfectly until I wanted to add annotations to ftConf (the "&= summary" bit in http://community.haskell.org/~ndm/cmdargs/). The only way I can go around the errors I get is precisely unsafe, so ah, you only live once ;)
Well, you could try
setTime :: FuzzyTimeConf -> IO FuzzyTimeConf setTime ftc = do now <- getClockTime return $ ftc{ time = convert now }
-- put annotations here defaultFuzzyTimeConf = FuzzyTimeConf { ... }
main = do realFTConf <- setTime defaultTimeConf print =<< cmdArgs realFTConf
I haven't looked at cmdargs, so I've no idea whether that'll work, but it may be worth a try.
participants (3)
-
Brent Yorgey
-
Daniel Fischer
-
Kamil Stachowski