@@ -184,20 +184,7 @@ public static DateTime ToDateTime(Uuid uuid)
184184 if ( ! IsUUIDv6 ( uuid ) )
185185 throw new ArgumentException ( String . Format ( "{0} is not a Version 6 UUID." , uuid ) , nameof ( uuid ) ) ;
186186
187- Byte [ ] time = new Byte [ 8 ] ;
188- Byte [ ] hex = uuid . ToByteArray ( ) ;
189-
190- time [ 0 ] = hex [ 0 ] ;
191- time [ 1 ] = hex [ 1 ] ;
192- time [ 2 ] = hex [ 2 ] ;
193- time [ 3 ] = hex [ 3 ] ;
194- time [ 4 ] = hex [ 4 ] ;
195- time [ 5 ] = hex [ 5 ] ;
196- time [ 6 ] = ( Byte ) ( ( Byte ) ( ( hex [ 6 ] & 0x0F ) << 4 ) + ( hex [ 7 ] >> 4 ) ) ;
197- time [ 7 ] = ( Byte ) ( hex [ 7 ] << 4 ) ;
198-
199- Int64 timeInterval = System . Net . IPAddress . NetworkToHostOrder ( BitConverter . ToInt64 ( time , 0 ) ) ;
200- timeInterval >>= 4 ;
187+ long timeInterval = GetTimeInterval ( uuid ) ;
201188 TimeSpan timeSpan = TimeSpan . FromTicks ( timeInterval ) ;
202189
203190 return s_epoch . ToUniversalTime ( ) + timeSpan ;
@@ -214,21 +201,10 @@ public static Uuid ToUUIDv1(Uuid uuid)
214201 if ( ! IsUUIDv6 ( uuid ) )
215202 throw new ArgumentException ( String . Format ( "{0} is not a Version 6 UUID." , uuid ) , nameof ( uuid ) ) ;
216203
217- Byte [ ] time = new Byte [ 8 ] ;
218- Byte [ ] hex = uuid . ToByteArray ( ) ;
219-
220- time [ 0 ] = hex [ 0 ] ;
221- time [ 1 ] = hex [ 1 ] ;
222- time [ 2 ] = hex [ 2 ] ;
223- time [ 3 ] = hex [ 3 ] ;
224- time [ 4 ] = hex [ 4 ] ;
225- time [ 5 ] = hex [ 5 ] ;
226- time [ 6 ] = ( Byte ) ( ( Byte ) ( ( hex [ 6 ] & 0x0F ) << 4 ) + ( hex [ 7 ] >> 4 ) ) ;
227- time [ 7 ] = ( Byte ) ( hex [ 7 ] << 4 ) ;
204+ Int64 timeInterval = GetTimeInterval ( uuid ) ;
228205
229- Int64 timeInterval = System . Net . IPAddress . NetworkToHostOrder ( BitConverter . ToInt64 ( time , 0 ) ) ;
230- timeInterval >>= 4 ;
231- time = BitConverter . GetBytes ( System . Net . IPAddress . HostToNetworkOrder ( timeInterval ) ) ;
206+ Byte [ ] hex = uuid . ToByteArray ( ) ;
207+ Byte [ ] time = BitConverter . GetBytes ( System . Net . IPAddress . HostToNetworkOrder ( timeInterval ) ) ;
232208
233209 hex [ 0 ] = time [ 4 ] ;
234210 hex [ 1 ] = time [ 5 ] ;
@@ -243,5 +219,24 @@ public static Uuid ToUUIDv1(Uuid uuid)
243219
244220 return new Uuid ( hex ) ;
245221 }
222+
223+ private static Int64 GetTimeInterval ( Uuid uuid )
224+ {
225+ Byte [ ] hex = uuid . ToByteArray ( ) ;
226+ Byte [ ] time = new Byte [ 8 ] ;
227+
228+ time [ 0 ] = hex [ 0 ] ;
229+ time [ 1 ] = hex [ 1 ] ;
230+ time [ 2 ] = hex [ 2 ] ;
231+ time [ 3 ] = hex [ 3 ] ;
232+ time [ 4 ] = hex [ 4 ] ;
233+ time [ 5 ] = hex [ 5 ] ;
234+ time [ 6 ] = ( Byte ) ( ( Byte ) ( ( hex [ 6 ] & 0x0F ) << 4 ) + ( hex [ 7 ] >> 4 ) ) ;
235+ time [ 7 ] = ( Byte ) ( hex [ 7 ] << 4 ) ;
236+
237+ Int64 timeInterval = System . Net . IPAddress . NetworkToHostOrder ( BitConverter . ToInt64 ( time , 0 ) ) ;
238+ timeInterval >>= 4 ;
239+ return timeInterval ;
240+ }
246241 }
247242}
0 commit comments