Skip to content
This repository has been archived by the owner on May 25, 2021. It is now read-only.

Commit

Permalink
added clockSequence back in using the Environment.TickCount to close …
Browse files Browse the repository at this point in the history
…out issue #66
  • Loading branch information
nberardi committed Sep 21, 2012
1 parent 0547280 commit 794d97f
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/GuidGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;

namespace FluentCassandra
{
Expand Down Expand Up @@ -30,16 +31,13 @@ public static partial class GuidGenerator
private static readonly DateTimeOffset GregorianCalendarStart = new DateTimeOffset(1582, 10, 15, 0, 0, 0, TimeSpan.Zero);

// random clock sequence and node
public static byte[] DefaultClockSequence { get; set; }
public static byte[] DefaultNode { get; set; }

static GuidGenerator()
{
DefaultClockSequence = new byte[2];
DefaultNode = new byte[6];

var random = new Random();
random.NextBytes(DefaultClockSequence);
random.NextBytes(DefaultNode);
}

Expand All @@ -49,6 +47,11 @@ public static GuidVersion GetVersion(this Guid guid)
return (GuidVersion)((bytes[VersionByte] & 0xFF) >> VersionByteShift);
}

private static byte[] ClockSequence
{
get { return BitConverter.GetBytes(Convert.ToInt16(Environment.TickCount % Int16.MaxValue)); }
}

public static DateTimeOffset GetDateTimeOffset(Guid guid)
{
byte[] bytes = guid.ToByteArray();
Expand Down Expand Up @@ -83,17 +86,17 @@ public static DateTime GetUtcDateTime(Guid guid)

public static Guid GenerateTimeBasedGuid()
{
return GenerateTimeBasedGuid(DateTimePrecise.UtcNowOffset, DefaultClockSequence, DefaultNode);
return GenerateTimeBasedGuid(DateTimePrecise.UtcNowOffset, ClockSequence, DefaultNode);
}

public static Guid GenerateTimeBasedGuid(DateTime dateTime)
{
return GenerateTimeBasedGuid(dateTime, DefaultClockSequence, DefaultNode);
return GenerateTimeBasedGuid(dateTime, ClockSequence, DefaultNode);
}

public static Guid GenerateTimeBasedGuid(DateTimeOffset dateTime)
{
return GenerateTimeBasedGuid(dateTime, DefaultClockSequence, DefaultNode);
return GenerateTimeBasedGuid(dateTime, ClockSequence, DefaultNode);
}

public static Guid GenerateTimeBasedGuid(DateTime dateTime, byte[] clockSequence, byte[] node)
Expand Down

0 comments on commit 794d97f

Please sign in to comment.