[Haskell-cafe] Converting EpochTime to Integer

Erik Hesselink hesselink at gmail.com
Wed Sep 16 11:42:54 UTC 2015


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 <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


More information about the Haskell-Cafe mailing list