From a6e054a8b9210d32d8bfd68554b72503d7716a6d Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Thu, 17 Jan 2013 11:38:45 +0100
Subject: [PATCH] Add a function to get the current time in microseconds

In some cases we need higher resolution that seconds; I've settled on
microseconds as that is what 'threadDelay' wants, for exactly, so it's
easier if we keep the same units.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Michele Tartara <mtartara@google.com>
---
 src/Ganeti/Utils.hs | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/src/Ganeti/Utils.hs b/src/Ganeti/Utils.hs
index 74ad060c7..cfaaaab2f 100644
--- a/src/Ganeti/Utils.hs
+++ b/src/Ganeti/Utils.hs
@@ -46,6 +46,7 @@ module Ganeti.Utils
   , rStripSpace
   , newUUID
   , getCurrentTime
+  , getCurrentTimeUSec
   , clockTimeToString
   , chompPrefix
   , wrap
@@ -293,13 +294,22 @@ newUUID = do
   contents <- readFile C.randomUuidFile
   return $! rStripSpace $ take 128 contents
 
--- | Returns the current time as an Integer representing the number of
--- seconds from the Unix epoch.
+-- | Returns the current time as an 'Integer' representing the number
+-- of seconds from the Unix epoch.
 getCurrentTime :: IO Integer
 getCurrentTime = do
   TOD ctime _ <- getClockTime
   return ctime
 
+-- | Returns the current time as an 'Integer' representing the number
+-- of microseconds from the Unix epoch (hence the need for 'Integer').
+getCurrentTimeUSec :: IO Integer
+getCurrentTimeUSec = do
+  TOD ctime pico <- getClockTime
+  -- pico: 10^-12, micro: 10^-6, so we have to shift seconds left and
+  -- picoseconds right
+  return $ ctime * 1000000 + pico `div` 1000000
+
 -- | Convert a ClockTime into a (seconds-only) timestamp.
 clockTimeToString :: ClockTime -> String
 clockTimeToString (TOD t _) = show t
-- 
GitLab