on b7aaa38 help sign in
53 lines 1.6 kB history raw
1{-# LANGUAGE TypeFamilies #-}
2module Data.UUID.Util (
3 UnpackedUUID(..)
4 , unpack, pack
5 , version
6 , extractMac
7 , extractTime
8 , setTime
9 ) where
11import Prelude hiding (null)
12import Data.Word
13import Data.Word.Util
14import Data.Bits
15import Data.UUID.Internal
16import Network.Info
17import Data.Int (Int64)
19version :: UUID -> Int
20version uuid =
21 fromEnum ((time_hi_and_version unpacked `shiftR` 12) .&. 0xF)
22 where
23 unpacked = unpack uuid
25-- Note UUID time is in 10^-7 seconds.
26setTime :: (Integral a, Bits a) => UUID -> a -> Maybe UUID
27setTime 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)
35extractTime :: UUID -> Maybe Int64
36extractTime 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
43timeAndVersionToTime :: Word16 -> Word16
44timeAndVersionToTime tv = tv .&. 0x0FFF
46extractMac :: UUID -> Maybe MAC
47extractMac 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