1{-# OPTIONS_GHC -fno-cse #-}
4-- Module : Data.UUID.V1
5-- Copyright : (c) 2008 Jason Dusek
9-- Maintainer : aslatter@gmail.com
10-- Stability : experimental
11-- Portability : portable
13-- RFC 4122 Version 1 UUID state machine.
15module Data.UUID.V1(nextUUID)
26import System.IO.Unsafe
31import Data.UUID.Internal
33-- | Returns a new UUID derived from the local hardware MAC
34-- address and the current system time.
35-- Is generated according to the Version 1 UUID sepcified in
38-- Returns nothing if the hardware MAC address could not
40nextUUID :: IO (Maybe UUID)
45 (Just (c, t), Just (MAC a' b' c' d' e' f')) -> do
47 (tL, tM, tH) = word64ToTimePieces t
48 (cL, cH) = word16ToClockSeqPieces c
49 return $ Just $ UUID tL tM tH cH cL $ Node a' b' c' d' e' f'
53-- |The bit layout and version number here used are described in clause 13 of
54-- ITU X.667, from September 2004.
55word64ToTimePieces :: Word64 -> (Word32, Word16, Word16)
56word64ToTimePieces w = (lo, mi, hi)
58 lo = fromIntegral $ (w `shiftL` 32) `shiftR` 32
59 mi = fromIntegral $ (w `shiftL` 16) `shiftR` 48
60 hi = (fromIntegral $ w `shiftR` 48) `setBit` 15
64 State c0 h0 <- readIORef state
65 h1 <- fmap hundredsOfNanosSinceGregorianReform getCurrentTime
68 writeIORef state $ State 0 h1
75 writeIORef state $ State c1 h0
76 return $ Just (c1, h0)
82state = unsafePerformIO $ do
83 h0 <- fmap hundredsOfNanosSinceGregorianReform getCurrentTime
88 {-# UNPACK #-} !Word16
89 {-# UNPACK #-} !Word64
94hundredsOfNanosSinceGregorianReform :: UTCTime -> Word64
95hundredsOfNanosSinceGregorianReform t = floor $ 10000000 * dt
97 gregorianReform = UTCTime (fromGregorian 1582 10 15) 0
98 dt = t `diffUTCTime` gregorianReform
101-- |Per clause 13 of ITU X.667, from September 2004.
102word16ToClockSeqPieces :: Word16 -> (Word8, Word8)
103word16ToClockSeqPieces w = (lo, hi)
105 lo = fromIntegral $ (w `shiftL` 8) `shiftR` 8
106 hi = (fromIntegral $ w `shiftR` 8) `setBit` 7
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list