[Haskell-cafe] Converting EpochTime to Integer

Patrick Chilton chpatrick at gmail.com
Wed Sep 16 13:34:00 UTC 2015


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 <chpatrick at gmail.com>
wrote:

> 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 <bneijt at gmail.com> 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 <kane at kane.cx> 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 at haskell.org
>> >> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>> >>
>> >
>> >
>> > _______________________________________________
>> > Haskell-Cafe mailing list
>> > Haskell-Cafe at haskell.org
>> > http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>> >
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20150916/45e4720a/attachment.html>


More information about the Haskell-Cafe mailing list