Permalink
Browse files

updated TimeUUID

  • Loading branch information...
1 parent 10617e5 commit 2ba33bc8213fccb4ddf80738110b5d66b7894731 @nberardi nberardi committed Feb 6, 2012
Showing with 24 additions and 14 deletions.
  1. +22 −13 src/GuidGenerator.cs
  2. +2 −1 src/IFluentRecordExpression.cs
View
@@ -23,7 +23,7 @@ public static class GuidGenerator
private const byte NodeByte = 10;
// offset to move from 1/1/0001, which is 0-time for .NET, to gregorian 0-time of 10/15/1582
- private static readonly DateTime GregorianCalendarStart = new DateTime(1582, 10, 15, 0, 0, 0, DateTimeKind.Utc);
+ private static readonly DateTimeOffset GregorianCalendarStart = new DateTimeOffset(1582, 10, 15, 0, 0, 0, TimeSpan.Zero);
// random node that is 16 bytes
private static readonly byte[] RandomNode;
@@ -42,7 +42,7 @@ public static GuidVersion GetVersion(this Guid guid)
return (GuidVersion)((bytes[VersionByte] & 0xFF) >> VersionByteShift);
}
- public static DateTime GetDateTime(Guid guid)
+ public static DateTimeOffset GetDateTimeOffset(Guid guid)
{
byte[] bytes = guid.ToByteArray();
@@ -56,39 +56,48 @@ public static DateTime GetDateTime(Guid guid)
long timestamp = BitConverter.ToInt64(timestampBytes, 0);
long ticks = timestamp + GregorianCalendarStart.Ticks;
- return new DateTime(ticks, DateTimeKind.Utc);
+ return new DateTimeOffset(ticks, TimeSpan.Zero);
}
- public static DateTimeOffset GetDateTimeOffset(Guid guid)
+ public static DateTime GetDateTime(Guid guid)
{
- return new DateTimeOffset(GetDateTime(guid), TimeSpan.Zero);
+ return GetDateTimeOffset(guid).DateTime;
}
- public static Guid GenerateTimeBasedGuid()
+ public static DateTime GetLocalDateTime(Guid guid)
{
- return GenerateTimeBasedGuid(DateTime.UtcNow, RandomNode);
+ return GetDateTimeOffset(guid).LocalDateTime;
}
- public static Guid GenerateTimeBasedGuid(DateTimeOffset dateTime)
+ public static DateTime GetUtcDateTime(Guid guid)
{
- return GenerateTimeBasedGuid(dateTime, RandomNode);
+ return GetDateTimeOffset(guid).UtcDateTime;
+ }
+
+ public static Guid GenerateTimeBasedGuid()
+ {
+ return GenerateTimeBasedGuid(DateTimeOffset.UtcNow, RandomNode);
}
public static Guid GenerateTimeBasedGuid(DateTime dateTime)
{
return GenerateTimeBasedGuid(dateTime, RandomNode);
}
- public static Guid GenerateTimeBasedGuid(DateTimeOffset dateTime, byte[] node)
+ public static Guid GenerateTimeBasedGuid(DateTimeOffset dateTime)
{
- return GenerateTimeBasedGuid(dateTime.UtcDateTime, node);
+ return GenerateTimeBasedGuid(dateTime, RandomNode);
}
public static Guid GenerateTimeBasedGuid(DateTime dateTime, byte[] node)
{
- dateTime = dateTime.ToUniversalTime();
- long ticks = (dateTime - GregorianCalendarStart).Ticks;
+ var utc = dateTime.Kind == DateTimeKind.Utc;
+ return GenerateTimeBasedGuid(new DateTimeOffset(dateTime, utc ? TimeSpan.Zero : (DateTimeOffset.Now.Offset)), node);
+ }
+ public static Guid GenerateTimeBasedGuid(DateTimeOffset dateTime, byte[] node)
+ {
+ long ticks = (dateTime - GregorianCalendarStart).Ticks;
byte[] guid = new byte[ByteArraySize];
byte[] clockSequenceBytes = BitConverter.GetBytes(Convert.ToInt16(Environment.TickCount % Int16.MaxValue));
byte[] timestamp = BitConverter.GetBytes(ticks);
@@ -1,9 +1,10 @@
using System;
+using System.Dynamic;
using FluentCassandra.Types;
namespace FluentCassandra
{
- public interface IFluentRecordExpression
+ public interface IFluentRecordExpression : IDynamicMetaObjectProvider
{
CassandraType this[CassandraType name] { get; }
}

0 comments on commit 2ba33bc

Please sign in to comment.