Revision 5ea9f6cb src/Ganeti/JSON.hs

b/src/Ganeti/JSON.hs
51 51
  , GenericContainer(..)
52 52
  , Container
53 53
  , MaybeForJSON(..)
54
  , TimeAsDoubleJSON(..)
54 55
  )
55 56
  where
56 57

  
......
59 60
import Control.Monad.Error.Class
60 61
import Data.Maybe (fromMaybe, catMaybes)
61 62
import qualified Data.Map as Map
63
import System.Time (ClockTime(..))
62 64
import Text.Printf (printf)
63 65

  
64 66
import qualified Text.JSON as J
......
315 317
  readJSON = J.readJSON
316 318
  showJSON (MaybeForJSON (Just x)) = J.showJSON x
317 319
  showJSON (MaybeForJSON Nothing)  = J.JSNull
320

  
321
newtype TimeAsDoubleJSON
322
    = TimeAsDoubleJSON { unTimeAsDoubleJSON :: ClockTime }
323
  deriving (Show, Eq, Ord)
324
instance J.JSON TimeAsDoubleJSON where
325
  readJSON v = do
326
      t <- J.readJSON v :: J.Result Double
327
      return . TimeAsDoubleJSON . uncurry TOD
328
             $ divMod (round $ t * pico) (pico :: Integer)
329
    where
330
      pico :: (Num a) => a
331
      pico = 10^(12 :: Int)
332
  showJSON (TimeAsDoubleJSON (TOD ss ps)) = J.showJSON
333
      (fromIntegral ss + fromIntegral ps / 10^(12 :: Int) :: Double)

Also available in: Unified diff