1{-# OPTIONS_GHC -fno-cse #-}
2{-# LANGUAGE TypeFamilies #-}
5-- Module : Data.UUID.V1
6-- Copyright : (c) 2008 Jason Dusek
7-- (c) 2009 Mark Lentczner
8-- (c) 2009-2010 Antoine Latter
12-- Maintainer : aslatter@gmail.com
13-- Stability : experimental
14-- Portability : portable
16-- RFC 4122 Version 1 UUID state machine.
18module Data.UUID.V1(nextUUID)
26import Control.Concurrent.MVar
28import System.IO.Unsafe
30import qualified System.Info.MAC as SysMAC
33import Data.UUID.Builder
34import Data.UUID.Internal
36-- | Returns a new UUID derived from the local hardware MAC
37-- address and the current system time.
38-- Is generated according to the Version 1 UUID sepcified in
41-- Returns nothing if the hardware MAC address could not
43nextUUID :: IO (Maybe UUID)
48 (Just (c, t), Just m) -> return $ Just $ makeUUID t c m
52makeUUID :: Word64 -> Word16 -> MAC -> UUID
53makeUUID time clock mac =
54 buildFromBytes 1 /-/ tLow /-/ tMid /-/ tHigh /-/ clock /-/ (MACSource mac)
55 where tLow = (fromIntegral time) :: Word32
56 tMid = (fromIntegral (time `shiftR` 32)) :: Word16
57 tHigh = (fromIntegral (time `shiftR` 48)) :: Word16
59newtype MACSource = MACSource MAC
60instance ByteSource MACSource where
61 z /-/ (MACSource (MAC a b c d e f)) = z a b c d e f
62type instance ByteSink MACSource g = Takes3Bytes (Takes3Bytes g)
65-- |Approximates the clock algorithm in RFC 4122, section 4.2
66-- Isn't system wide or thread safe, nor does it properly randomize
67-- the clock value on initialization.
68stepTime :: IO (Maybe (Word16, Word64))
70 h1 <- fmap hundredsOfNanosSinceGregorianReform getCurrentTime
71 modifyMVar state $ \s@(State c0 h0) ->
74 return (State c0 h1, Just (c0, h1))
78 in if c1 <= 0x3fff -- when clock is initially randomized,
79 -- then this test will need to change
81 return (State c1 h1, Just (c1, h1))
88state = unsafePerformIO $ do
89 h0 <- fmap hundredsOfNanosSinceGregorianReform getCurrentTime
90 newMVar $ State 0 h0 -- the 0 should be a random number
94 {-# UNPACK #-} !Word16
95 {-# UNPACK #-} !Word64
100hundredsOfNanosSinceGregorianReform :: UTCTime -> Word64
101hundredsOfNanosSinceGregorianReform t = floor $ 10000000 * dt
103 gregorianReform = UTCTime (fromGregorian 1582 10 15) 0
104 dt = t `diffUTCTime` gregorianReform
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