Converting EpochTime to Integer

Dear Haskell cafe, I want to convert form mtime :: System.Posix.Types.EpochTime to something aeson can eat without having to define an instance. I choose Integer. My current solution[1] is just using (read . show). I know, ugly, but the only thing I could get going. What is the proper way to convert System.Posix.Types.EpochTime to Integer or something else aeson will automatically convert without making a ToJSON instance? As a beginner I wonder: is there a general way to go about finding conversion paths between types? Greetings, Bram Neijt [1] https://github.com/bneijt/httpmtimeline/blob/master/src/Main.hs#L21

type EpochTime = CTime
newtype CTime = CTime Int64
You can just pattern match.
On Tue, Sep 15, 2015 at 5:42 PM, Bram Neijt
Dear Haskell cafe,
I want to convert form
mtime :: System.Posix.Types.EpochTime
to something aeson can eat without having to define an instance. I choose Integer.
My current solution[1] is just using (read . show). I know, ugly, but the only thing I could get going.
What is the proper way to convert System.Posix.Types.EpochTime to Integer or something else aeson will automatically convert without making a ToJSON instance?
As a beginner I wonder: is there a general way to go about finding conversion paths between types?
Greetings,
Bram Neijt
[1] https://github.com/bneijt/httpmtimeline/blob/master/src/Main.hs#L21 _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

EpochTime is an instance of Integral. You can just use toInteger :: Integral a => a -> Integer. On 9/15/2015 6:42 PM, Bram Neijt wrote:
Dear Haskell cafe,
I want to convert form
mtime :: System.Posix.Types.EpochTime
to something aeson can eat without having to define an instance. I choose Integer.
My current solution[1] is just using (read . show). I know, ugly, but the only thing I could get going.
What is the proper way to convert System.Posix.Types.EpochTime to Integer or something else aeson will automatically convert without making a ToJSON instance?
As a beginner I wonder: is there a general way to go about finding conversion paths between types?
Greetings,
Bram Neijt
[1] https://github.com/bneijt/httpmtimeline/blob/master/src/Main.hs#L21 _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

I'm sorry, but as a newby I can't seem to get it to work.
If I use
return (mtime :: Integer)
I get
src/Main.hs:21:13:
Couldn't match type ‘Foreign.C.Types.CTime’ with ‘Integer’
Expected type: Integer
Actual type: System.Posix.Types.EpochTime
In the first argument of ‘return’, namely ‘(mtime :: Integer)’
In a stmt of a 'do' block: return (mtime :: Integer)
If I use
return (toInteger mtime)
I get
src/Main.hs:21:13:
No instance for (Integral System.Posix.Types.EpochTime)
arising from a use of ‘toInteger’
In the first argument of ‘return’, namely ‘(toInteger mtime)’
In a stmt of a 'do' block: return (toInteger mtime)
In the expression:
do { status <- getFileStatus filepath;
let mtime = modificationTime status;
return (toInteger mtime) }
What am I doing wrong here? How would I pattern match to convert the type?
Greetings,
Bram
PS Code is still
https://github.com/bneijt/httpmtimeline/blob/master/src/Main.hs#L21
Versions
cabal-install version 1.22.2.0
using version 1.22.2.0 of the Cabal library
The Glorious Glasgow Haskell Compilation System, version 7.10.1
On Tue, Sep 15, 2015 at 6:48 PM, David Kraeutmann
EpochTime is an instance of Integral. You can just use toInteger :: Integral a => a -> Integer. On 9/15/2015 6:42 PM, Bram Neijt wrote:
Dear Haskell cafe,
I want to convert form
mtime :: System.Posix.Types.EpochTime
to something aeson can eat without having to define an instance. I choose Integer.
My current solution[1] is just using (read . show). I know, ugly, but the only thing I could get going.
What is the proper way to convert System.Posix.Types.EpochTime to Integer or something else aeson will automatically convert without making a ToJSON instance?
As a beginner I wonder: is there a general way to go about finding conversion paths between types?
Greetings,
Bram Neijt
[1] https://github.com/bneijt/httpmtimeline/blob/master/src/Main.hs#L21 _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

Hi Bram,
You can do
case mtime of
CTime t -> fromIntegral t
Here `mtime :: EpochTime`, `t :: Int64` and the result is any `Num`
you want, including `Integer`.
CTime doesn't have an Integral instance itself, probably because
division on times doesn't make sense. That's why you can't use
`fromIntegral` directly.
Regards,
Erik
On 16 September 2015 at 13:19, Bram Neijt
I'm sorry, but as a newby I can't seem to get it to work.
If I use return (mtime :: Integer) I get src/Main.hs:21:13: Couldn't match type ‘Foreign.C.Types.CTime’ with ‘Integer’ Expected type: Integer Actual type: System.Posix.Types.EpochTime In the first argument of ‘return’, namely ‘(mtime :: Integer)’ In a stmt of a 'do' block: return (mtime :: Integer)
If I use return (toInteger mtime) I get src/Main.hs:21:13: No instance for (Integral System.Posix.Types.EpochTime) arising from a use of ‘toInteger’ In the first argument of ‘return’, namely ‘(toInteger mtime)’ In a stmt of a 'do' block: return (toInteger mtime) In the expression: do { status <- getFileStatus filepath; let mtime = modificationTime status; return (toInteger mtime) }
What am I doing wrong here? How would I pattern match to convert the type?
Greetings,
Bram
PS Code is still https://github.com/bneijt/httpmtimeline/blob/master/src/Main.hs#L21 Versions cabal-install version 1.22.2.0 using version 1.22.2.0 of the Cabal library The Glorious Glasgow Haskell Compilation System, version 7.10.1
On Tue, Sep 15, 2015 at 6:48 PM, David Kraeutmann
wrote: EpochTime is an instance of Integral. You can just use toInteger :: Integral a => a -> Integer. On 9/15/2015 6:42 PM, Bram Neijt wrote:
Dear Haskell cafe,
I want to convert form
mtime :: System.Posix.Types.EpochTime
to something aeson can eat without having to define an instance. I choose Integer.
My current solution[1] is just using (read . show). I know, ugly, but the only thing I could get going.
What is the proper way to convert System.Posix.Types.EpochTime to Integer or something else aeson will automatically convert without making a ToJSON instance?
As a beginner I wonder: is there a general way to go about finding conversion paths between types?
Greetings,
Bram Neijt
[1] https://github.com/bneijt/httpmtimeline/blob/master/src/Main.hs#L21 _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

On Wed, Sep 16, 2015 at 7:19 AM, Bram Neijt
If I use return (toInteger mtime) I get src/Main.hs:21:13: No instance for (Integral System.Posix.Types.EpochTime)
I had been wondering about the claim that it had an Integral instance,.. it has Num but not Integral. It *does* have an Enum instance, so fromEnum shoiuld work. -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

On 16/09/15 16:04, Brandon Allbery wrote:
On Wed, Sep 16, 2015 at 7:19 AM, Bram Neijt
mailto:bneijt@gmail.com> wrote: If I use return (toInteger mtime) I get src/Main.hs:21:13: No instance for (Integral System.Posix.Types.EpochTime)
I had been wondering about the claim that it had an Integral instance,.. it has Num but not Integral.
It *does* have an Enum instance, so fromEnum shoiuld work.
Except that's not 2038-safe (fromEnum :: Enum a => a -> Int). Roman

On Wed, Sep 16, 2015 at 9:54 AM, Roman Cheplyaka
Except that's not 2038-safe (fromEnum :: Enum a => a -> Int).
Neither is EpochTime, to the extent that it is defined by Posix in a way that is not Y2038 safe. -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

There's no need to go to Integer, just use this:
epochToJSON :: EpochTime -> Value
epochToJSON (CTime t) = toJSON t
On Wed, Sep 16, 2015 at 12:19 PM, Bram Neijt
I'm sorry, but as a newby I can't seem to get it to work.
If I use return (mtime :: Integer) I get src/Main.hs:21:13: Couldn't match type ‘Foreign.C.Types.CTime’ with ‘Integer’ Expected type: Integer Actual type: System.Posix.Types.EpochTime In the first argument of ‘return’, namely ‘(mtime :: Integer)’ In a stmt of a 'do' block: return (mtime :: Integer)
If I use return (toInteger mtime) I get src/Main.hs:21:13: No instance for (Integral System.Posix.Types.EpochTime) arising from a use of ‘toInteger’ In the first argument of ‘return’, namely ‘(toInteger mtime)’ In a stmt of a 'do' block: return (toInteger mtime) In the expression: do { status <- getFileStatus filepath; let mtime = modificationTime status; return (toInteger mtime) }
What am I doing wrong here? How would I pattern match to convert the type?
Greetings,
Bram
PS Code is still https://github.com/bneijt/httpmtimeline/blob/master/src/Main.hs#L21 Versions cabal-install version 1.22.2.0 using version 1.22.2.0 of the Cabal library The Glorious Glasgow Haskell Compilation System, version 7.10.1
On Tue, Sep 15, 2015 at 6:48 PM, David Kraeutmann
wrote: EpochTime is an instance of Integral. You can just use toInteger :: Integral a => a -> Integer. On 9/15/2015 6:42 PM, Bram Neijt wrote:
Dear Haskell cafe,
I want to convert form
mtime :: System.Posix.Types.EpochTime
to something aeson can eat without having to define an instance. I choose Integer.
My current solution[1] is just using (read . show). I know, ugly, but the only thing I could get going.
What is the proper way to convert System.Posix.Types.EpochTime to Integer or something else aeson will automatically convert without making a ToJSON instance?
As a beginner I wonder: is there a general way to go about finding conversion paths between types?
Greetings,
Bram Neijt
[1] https://github.com/bneijt/httpmtimeline/blob/master/src/Main.hs#L21 _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

To elaborate, EpochTime is just a type synonym for CTime. CTime has a
constructor CTime Int64, so you can pattern match on it to get the Int64
inside. Int64 has a ToJSON instance, so you can run toJSON on it to get a
Value.
On Wed, Sep 16, 2015 at 2:30 PM, Patrick Chilton
There's no need to go to Integer, just use this:
epochToJSON :: EpochTime -> Value epochToJSON (CTime t) = toJSON t
On Wed, Sep 16, 2015 at 12:19 PM, Bram Neijt
wrote: I'm sorry, but as a newby I can't seem to get it to work.
If I use return (mtime :: Integer) I get src/Main.hs:21:13: Couldn't match type ‘Foreign.C.Types.CTime’ with ‘Integer’ Expected type: Integer Actual type: System.Posix.Types.EpochTime In the first argument of ‘return’, namely ‘(mtime :: Integer)’ In a stmt of a 'do' block: return (mtime :: Integer)
If I use return (toInteger mtime) I get src/Main.hs:21:13: No instance for (Integral System.Posix.Types.EpochTime) arising from a use of ‘toInteger’ In the first argument of ‘return’, namely ‘(toInteger mtime)’ In a stmt of a 'do' block: return (toInteger mtime) In the expression: do { status <- getFileStatus filepath; let mtime = modificationTime status; return (toInteger mtime) }
What am I doing wrong here? How would I pattern match to convert the type?
Greetings,
Bram
PS Code is still https://github.com/bneijt/httpmtimeline/blob/master/src/Main.hs#L21 Versions cabal-install version 1.22.2.0 using version 1.22.2.0 of the Cabal library The Glorious Glasgow Haskell Compilation System, version 7.10.1
On Tue, Sep 15, 2015 at 6:48 PM, David Kraeutmann
wrote: EpochTime is an instance of Integral. You can just use toInteger :: Integral a => a -> Integer. On 9/15/2015 6:42 PM, Bram Neijt wrote:
Dear Haskell cafe,
I want to convert form
mtime :: System.Posix.Types.EpochTime
to something aeson can eat without having to define an instance. I choose Integer.
My current solution[1] is just using (read . show). I know, ugly, but the only thing I could get going.
What is the proper way to convert System.Posix.Types.EpochTime to Integer or something else aeson will automatically convert without making a ToJSON instance?
As a beginner I wonder: is there a general way to go about finding conversion paths between types?
Greetings,
Bram Neijt
[1] https://github.com/bneijt/httpmtimeline/blob/master/src/Main.hs#L21 _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

I was looking at the wrong EpochTime, whoops. On 9/15/2015 6:48 PM, David Kraeutmann wrote:
EpochTime is an instance of Integral. You can just use toInteger :: Integral a => a -> Integer. On 9/15/2015 6:42 PM, Bram Neijt wrote:
Dear Haskell cafe,
I want to convert form
mtime :: System.Posix.Types.EpochTime
to something aeson can eat without having to define an instance. I choose Integer.
My current solution[1] is just using (read . show). I know, ugly, but the only thing I could get going.
What is the proper way to convert System.Posix.Types.EpochTime to Integer or something else aeson will automatically convert without making a ToJSON instance?
As a beginner I wonder: is there a general way to go about finding conversion paths between types?
Greetings,
Bram Neijt
[1] https://github.com/bneijt/httpmtimeline/blob/master/src/Main.hs#L21 _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

Thank you all for your responses!
I've decided to use fromEnum (and I'm going to read up on how that works.)
Greetings,
Bram
On Wed, Sep 16, 2015 at 4:53 PM, David Kraeutmann
I was looking at the wrong EpochTime, whoops.
On 9/15/2015 6:48 PM, David Kraeutmann wrote:
EpochTime is an instance of Integral. You can just use toInteger :: Integral a => a -> Integer. On 9/15/2015 6:42 PM, Bram Neijt wrote:
Dear Haskell cafe,
I want to convert form
mtime :: System.Posix.Types.EpochTime
to something aeson can eat without having to define an instance. I choose Integer.
My current solution[1] is just using (read . show). I know, ugly, but the only thing I could get going.
What is the proper way to convert System.Posix.Types.EpochTime to Integer or something else aeson will automatically convert without making a ToJSON instance?
As a beginner I wonder: is there a general way to go about finding conversion paths between types?
Greetings,
Bram Neijt
[1] https://github.com/bneijt/httpmtimeline/blob/master/src/Main.hs#L21 _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
participants (6)
-
Bram Neijt
-
Brandon Allbery
-
David Kraeutmann
-
Erik Hesselink
-
Patrick Chilton
-
Roman Cheplyaka