Skip to content

Commit 4e25c1e

Browse files
committed
GuidCombGenerator using DateTime.UtcNow
GuidCombGenerator using DateTime.UtcNow instead of DateTime.Now as .Now is affected by daylight saving time (DST) changes. This results in once a year the keys to not be sequential to previous keys. * DateTime.Now is also much slower then DateTime.UtcNow as .Now needs to apply timezone offsets to the time. * Also a small optimalisation as the base value does not need to be generated each time a guid is generate.
1 parent 9c058af commit 4e25c1e

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/NHibernate/Id/GuidCombGenerator.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ namespace NHibernate.Id
2424
/// </remarks>
2525
public class GuidCombGenerator : IIdentifierGenerator
2626
{
27+
private static readonly long BaseDateTicks = new DateTime(1900, 1, 1).Ticks;
28+
2729
#region IIdentifierGenerator Members
2830

2931
/// <summary>
@@ -44,11 +46,10 @@ private Guid GenerateComb()
4446
{
4547
byte[] guidArray = Guid.NewGuid().ToByteArray();
4648

47-
DateTime baseDate = new DateTime(1900, 1, 1);
48-
DateTime now = DateTime.Now;
49+
DateTime now = DateTime.UtcNow;
4950

5051
// Get the days and milliseconds which will be used to build the byte string
51-
TimeSpan days = new TimeSpan(now.Ticks - baseDate.Ticks);
52+
TimeSpan days = new TimeSpan(now.Ticks - BaseDateTicks);
5253
TimeSpan msecs = now.TimeOfDay;
5354

5455
// Convert to a byte array

0 commit comments

Comments
 (0)