From b6aeda4a504d80e1c91cd64316441a160d073df2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Dato=20Sim=C3=B3?= <dato@google.com>
Date: Fri, 7 Dec 2012 10:56:35 +0000
Subject: [PATCH] Utils.hs: add a clockTimeToString function
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This function allows to easily convert a ClockTime object to a string
representation of its timestamp (seconds-only).

Signed-off-by: Dato SimΓ³ <dato@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 src/Ganeti/Utils.hs          | 6 ++++++
 test/hs/Test/Ganeti/Utils.hs | 6 ++++++
 2 files changed, 12 insertions(+)

diff --git a/src/Ganeti/Utils.hs b/src/Ganeti/Utils.hs
index 8a73023f2..d9ea3b55c 100644
--- a/src/Ganeti/Utils.hs
+++ b/src/Ganeti/Utils.hs
@@ -45,6 +45,7 @@ module Ganeti.Utils
   , exitUnless
   , rStripSpace
   , newUUID
+  , clockTimeToString
   ) where
 
 import Data.Char (toUpper, isAlphaNum, isDigit, isSpace)
@@ -57,6 +58,7 @@ import Ganeti.BasicTypes
 import qualified Ganeti.Constants as C
 import System.IO
 import System.Exit
+import System.Time
 
 -- * Debug functions
 
@@ -286,3 +288,7 @@ newUUID :: IO String
 newUUID = do
   contents <- readFile C.randomUuidFile
   return $! rStripSpace $ take 128 contents
+
+-- | Convert a ClockTime into a (seconds-only) timestamp.
+clockTimeToString :: ClockTime -> String
+clockTimeToString (TOD t _) = show t
diff --git a/test/hs/Test/Ganeti/Utils.hs b/test/hs/Test/Ganeti/Utils.hs
index a84b30ff5..26d0b91cf 100644
--- a/test/hs/Test/Ganeti/Utils.hs
+++ b/test/hs/Test/Ganeti/Utils.hs
@@ -33,6 +33,7 @@ import Test.HUnit
 
 import Data.Char (isSpace)
 import Data.List
+import System.Time
 import qualified Text.JSON as J
 #ifndef NO_REGEX_PCRE
 import Text.Regex.PCRE
@@ -233,6 +234,10 @@ case_new_uuid = do
   assertBool "newUUID" $ uuid =~ C.uuidRegex
 #endif
 
+prop_clockTimeToString :: Integer -> Integer -> Property
+prop_clockTimeToString ts pico =
+  clockTimeToString (TOD ts pico) ==? show ts
+
 -- | Test list for the Utils module.
 testSuite "Utils"
             [ 'prop_commaJoinSplit
@@ -252,4 +257,5 @@ testSuite "Utils"
 #ifndef NO_REGEX_PCRE
             , 'case_new_uuid
 #endif
+            , 'prop_clockTimeToString
             ]
-- 
GitLab