From f4117c6cbf5e9df6aa791111f6ac22a3b9f8b5b6 Mon Sep 17 00:00:00 2001 From: RobThree Date: Sat, 24 Feb 2018 13:03:09 +0100 Subject: [PATCH] * Got rid of magic number and moved it to const --- NUlid/Ulid.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/NUlid/Ulid.cs b/NUlid/Ulid.cs index 0346e25..b4ab9e8 100644 --- a/NUlid/Ulid.cs +++ b/NUlid/Ulid.cs @@ -23,6 +23,7 @@ public struct Ulid : IEquatable, IComparable, IComparable, ISerializ // Char to index lookup array for massive speedup since we can find a char's index in O(1). We use 255 as 'sentinel' value for invalid indexes. private static readonly byte[] C2B32 = new byte[] { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255, 255, 255, 255, 255, 255, 255, 10, 11, 12, 13, 14, 15, 16, 17, 255, 18, 19, 255, 20, 21, 255, 22, 23, 24, 25, 26, 255, 27, 28, 29, 30, 31, 255, 255, 255, 255, 255, 255, 10, 11, 12, 13, 14, 15, 16, 17, 255, 18, 19, 255, 20, 21, 255, 22, 23, 24, 25, 26, 255, 27, 28, 29, 30, 31 }; private static readonly int C2B32LEN = C2B32.Length; + private const long UNIXEPOCHMILLISECONDS = 62135596800000; // Internal structure for data private readonly byte[] data; @@ -155,14 +156,14 @@ private Ulid(DateTimeOffset timePart, byte[] randomPart) #region Helper functions public static DateTimeOffset FromUnixTimeMilliseconds(long milliseconds) { - long ticks = milliseconds * TimeSpan.TicksPerMillisecond + 621355968000000000; + long ticks = milliseconds * TimeSpan.TicksPerMillisecond + (UNIXEPOCHMILLISECONDS * 10000); return new DateTimeOffset(ticks, TimeSpan.Zero); } private static long ToUnixTimeMilliseconds(DateTimeOffset value) { long milliseconds = value.Ticks / TimeSpan.TicksPerMillisecond; - return milliseconds - 62135596800000; + return milliseconds - UNIXEPOCHMILLISECONDS; } private static byte[] DateTimeOffsetToByteArray(DateTimeOffset value)