
Hi folks, I had some code using the oldtime package, and want to convert it to use the time package. One of the things I need to do is calculate the number of seconds since midnight. The easy part is getting a TimeDiff result: utc <- getCurrentTime tz <- getCurrentTimeZone let td = timeOfDayToTime $ localTimeOfDay $ utcToLocalTime tz utc Now td is a TimeDiff representation of the number of seconds since midnight. It prints nicely, but I'm having a heck of a time figuring out how to truncate it to an Int. The floor function is only supported by the RealFrac class. Although TimeDiff has an instance of RealFrac and Fractional, it doesn't have an instance of RealFrac. I looked up the various to* and from* functions and have come up short. fromEnum yields an Int but it's the wrong value. I know I could use show and then use readS to get an Integer, or use formatTime (and reparse that), but that's a hack. I can convert it to a TimeOfDay which gives me hours, minutes, and seconds, but then I have to do arithmetic on it, and the seconds are of type Pico, which I can't call floor on either. I can convert it to a Rational via timeOfDayToDayFraction, but a TimeDiff is already a Rational those don't have floor. What am I missing? There has got to be an easy way to count seconds! Thanks, Lyle

2008/11/12 Lyle Kopnicky
Hi folks,
I had some code using the oldtime package, and want to convert it to use the time package.
One of the things I need to do is calculate the number of seconds since midnight. The easy part is getting a TimeDiff result:
utc <- getCurrentTime tz <- getCurrentTimeZone let td = timeOfDayToTime $ localTimeOfDay $ utcToLocalTime tz utc
Now td is a TimeDiff representation of the number of seconds since midnight. It prints nicely, but I'm having a heck of a time figuring out how to truncate it to an Int.
The floor function is only supported by the RealFrac class. Although TimeDiff has an instance of RealFrac and Fractional, it doesn't have an instance of RealFrac. I looked up the various to* and from* functions and have come up short. fromEnum yields an Int but it's the wrong value. I know I could use show and then use readS to get an Integer, or use formatTime (and reparse that), but that's a hack.
I can convert it to a TimeOfDay which gives me hours, minutes, and seconds, but then I have to do arithmetic on it, and the seconds are of type Pico, which I can't call floor on either. I can convert it to a Rational via timeOfDayToDayFraction, but a TimeDiff is already a Rational those don't have floor.
What am I missing? There has got to be an easy way to count seconds!
Well, you could always (read . takeWhile (not . (=='.')) . show), but here's a better way: let x = toRational td in numerator x `div` denominator x This was just the first thing I could come up with. I bet there's a nicer way. - Phil
Thanks, Lyle
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Thanks Philip and Roger,
I think I'll use...
floor $ toRational td
...although I could have sworn that didn't work last night. I don't see that
TimeDiff has an instance of RealFrac, where floor is defined, though it does
have a instances of Real and Fractional.
Yes, the numeric classes are a bit hard to follow. Especially confusing is
the conversions. I'm thinking of making a multiparameter Coercible class,
with a coerce function, to help address that.
- Lyle
On Wed, Nov 12, 2008 at 9:43 AM, Philip Weaver
2008/11/12 Lyle Kopnicky
Hi folks,
I had some code using the oldtime package, and want to convert it to use the time package.
One of the things I need to do is calculate the number of seconds since midnight. The easy part is getting a TimeDiff result:
utc <- getCurrentTime tz <- getCurrentTimeZone let td = timeOfDayToTime $ localTimeOfDay $ utcToLocalTime tz utc
Now td is a TimeDiff representation of the number of seconds since midnight. It prints nicely, but I'm having a heck of a time figuring out how to truncate it to an Int.
The floor function is only supported by the RealFrac class. Although TimeDiff has an instance of RealFrac and Fractional, it doesn't have an instance of RealFrac. I looked up the various to* and from* functions and have come up short. fromEnum yields an Int but it's the wrong value. I know I could use show and then use readS to get an Integer, or use formatTime (and reparse that), but that's a hack.
I can convert it to a TimeOfDay which gives me hours, minutes, and seconds, but then I have to do arithmetic on it, and the seconds are of type Pico, which I can't call floor on either. I can convert it to a Rational via timeOfDayToDayFraction, but a TimeDiff is already a Rational those don't have floor.
What am I missing? There has got to be an easy way to count seconds!
Well, you could always (read . takeWhile (not . (=='.')) . show), but here's a better way:
let x = toRational td in numerator x `div` denominator x
This was just the first thing I could come up with. I bet there's a nicer way.
- Phil
Thanks, Lyle
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

"Lyle Kopnicky"
I had some code using the oldtime package, and want to convert it to use the time package. One of the things I need to do is calculate the number of seconds since midnight. The easy part is getting a TimeDiff result:
You mean DiffTime?
utc <- getCurrentTime tz <- getCurrentTimeZone let td = timeOfDayToTime $ localTimeOfDay $ utcToLocalTime tz utc Now td is a TimeDiff representation of the number of seconds since midnight. It prints nicely, but I'm having a heck of a time figuring out how to truncate it to an Int.
You could do something like fromEnum td `div` fromEnum (secondsToDiffTime 1) which says that you are computing a whole number of seconds. -- Jón Fairbairn Jon.Fairbairn@cl.cam.ac.uk http://www.chaos.org.uk/~jf/Stuff-I-dont-want.html (updated 2008-04-26)

Thanks, but that doesn't seem to work. I got an answer of -3. I tried it
again a minute later and it was still -3. I tried again a minute later and
it was -1. It's just after 9am here, so I have no idea what to make of those
numbers.
I have settled on this code:
secondsSinceMidnight :: IO Int
secondsSinceMidnight = do
zonedTime <- getZonedTime
return $ floor $ toRational $ timeOfDayToTime $ localTimeOfDay $
zonedTimeToLocalTime zonedTime
On Thu, Nov 13, 2008 at 2:29 AM, Jon Fairbairn
"Lyle Kopnicky"
writes: I had some code using the oldtime package, and want to convert it to use the time package. One of the things I need to do is calculate the number of seconds since midnight. The easy part is getting a TimeDiff result:
You mean DiffTime?
utc <- getCurrentTime tz <- getCurrentTimeZone let td = timeOfDayToTime $ localTimeOfDay $ utcToLocalTime tz utc Now td is a TimeDiff representation of the number of seconds since midnight. It prints nicely, but I'm having a heck of a time figuring out how to truncate it to an Int.
You could do something like fromEnum td `div` fromEnum (secondsToDiffTime 1) which says that you are computing a whole number of seconds.
-- Jón Fairbairn Jon.Fairbairn@cl.cam.ac.uk http://www.chaos.org.uk/~jf/Stuff-I-dont-want.html (updated 2008-04-26)
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

"Lyle Kopnicky"
Thanks, but that doesn't seem to work. I got an answer of -3. I tried it again a minute later and it was still -3. I tried again a minute later and it was -1. It's just after 9am here, so I have no idea what to make of those numbers.
That's most strange. The only difference between what I wrote and what you had before was the way the conversion to Integer was done.
I have settled on this code:
secondsSinceMidnight :: IO Int secondsSinceMidnight = do zonedTime <- getZonedTime return $ floor $ toRational $ timeOfDayToTime $ localTimeOfDay $ zonedTimeToLocalTime zonedTime
So what happens with do now <- getZonedTime; print $ (fromEnum $ timeOfDayToTime $ localTimeOfDay $ zonedTimeToLocalTime now)`div`fromEnum (secondsToDiffTime 1) where you are? -- Jón Fairbairn Jon.Fairbairn@cl.cam.ac.uk
participants (3)
-
Jon Fairbairn
-
Lyle Kopnicky
-
Philip Weaver