on master help sign in
Add file to version control (should have been in last couple versions)
commitc31a6972568a000af86bc8a62d66a70e48e503de authorAntoine Latter <aslatter@gmail.com> dateTue 29 Oct 2013, 00:04 (13 years ago) parent7f6db7d
1 file changed+54
Data/UUID/Util.hs+54
Data/UUID/Util.hsnew file+54
@@ -0,0 +1,54 @@
1+{-# LANGUAGE TypeFamilies #-}
2+module Data.UUID.Util (
3+ UnpackedUUID(..)
4+ , unpack, pack
5+ , version
6+ , extractMac
7+ , extractTime
8+ , setTime
9+ ) where
10+
11+import Prelude hiding (null)
12+import Data.Word
13+import Data.Word.Util
14+import Data.Bits
15+import Data.UUID.Internal
16+import Network.Info
17+import Data.Int (Int64)
18+
19+version :: UUID -> Int
20+version uuid =
21+ fromEnum ((time_hi_and_version unpacked `shiftR` 12) .&. 0xF)
22+ where
23+ unpacked = unpack uuid
24+
25+-- Note UUID time is in 10^-7 seconds.
26+setTime :: (Integral a, Bits a) => UUID -> a -> Maybe UUID
27+setTime uuid t =
28+ if version uuid == 1
29+ then Just $ pack $ (unpack uuid){time_low = new_low_bits, time_mid = new_mid_bits, time_hi_and_version = new_hi_and_version_bits}
30+ else Nothing
31+ where new_low_bits = fromIntegral $ t .&. 0xFFFFFFFF
32+ new_mid_bits = fromIntegral $ (t `shiftR` 32) .&. 0xFFFF
33+ new_hi_and_version_bits = fromIntegral $ 0x1000 .|. ((t `shiftR` 48) .&. 0x0FFF)
34+
35+extractTime :: UUID -> Maybe Int64
36+extractTime uuid =
37+ if version uuid == 1
38+ then Just $ fromIntegral $ w32to64 (w16to32 (timeAndVersionToTime $ time_hi_and_version unpacked) $ time_mid unpacked) (time_low unpacked)
39+ else Nothing
40+ where
41+ unpacked = unpack uuid
42+
43+timeAndVersionToTime :: Word16 -> Word16
44+timeAndVersionToTime tv = tv .&. 0x0FFF
45+
46+extractMac :: UUID -> Maybe MAC
47+extractMac uuid =
48+ if version uuid == 1
49+ then Just $
50+ MAC (node_0 unpacked) (node_1 unpacked) (node_2 unpacked) (node_3 unpacked) (node_4 unpacked) (node_5 unpacked)
51+ else Nothing
52+ where
53+ unpacked = unpack uuid
54+