Haskell: Extract Int from POSIXTime / NominalDiffTime

I sign web requests and as part of the query string, I need to include oauth_timestamp=123456789 , where 123456789 is the nominal time from 1970-01-01 00:00 UTC.

I used POSIXTime for my view, which is just an alias of type NominalDiffTime .

I am having problems when I want to convert POSIXTime / NominalDiffTime to Text so that I can add it to the collection of query string elements.

The problem is that the constructor for NominalDiffTime is private, and therefore I cannot extract the actual number.

Even the hacky way to use show will add an "s" generating 123456789s .

Is there an easy way to extract the actual number from NominalDiffTime?

+5
source share
1 answer

This may qualify as an example of class types that make the API more esoteric than they should be: NominalDiffTime implements RealFrac , a class type of arbitrary precision. As a result, the floor function is available to you:

 Ξ»> import Data.Time.Clock.POSIX Ξ»> pt <- getPOSIXTime Ξ»> pt 1458787669.804632s Ξ»> toRational pt 182348458725579 % 125000 Ξ»> floor pt :: Int 1458787669 
+5
source

Source: https://habr.com/ru/post/1245676/


All Articles