using System; using System.Globalization; using System.Threading; namespace ZeroLevel.Services { /// /// Provides various options for generating identifiers /// public static class IdGenerator { /// /// Returns a function to get consecutive int64 values. /// public static Func IncreasingSequenceIdGenerator() { long id = 0; return new Func(() => Interlocked.Increment(ref id)); } /// /// Creates a base64 hash from the specified datetime /// public static string HashFromDateTime(DateTime date) { var bytes = BitConverter.GetBytes(date.Ticks); return Convert.ToBase64String(bytes) .Replace('+', '_') .Replace('/', '-') .TrimEnd('='); } /// /// Creates a base64 hash from the current datetime /// public static string HashFromCurrentDateTime() { return HashFromDateTime(DateTime.Now); } /// /// Returns a hash as a string from the 32-bit hash value of the specified datetime /// public static string ShortHashFromDateTime(DateTime date) { return date.ToString(CultureInfo.InvariantCulture).GetHashCode().ToString("x"); } /// /// Returns a hash as a string from the 32-bit hash value of the current datetime /// public static string ShortHashFromCurrentDateTime() { return DateTime.Now.ToString(CultureInfo.InvariantCulture).GetHashCode().ToString("x"); } /// /// Creates a timestamp from current datetime /// public static string CreateTimestamp() { return DateTime.Now.ToString("yyyyMMddHHmmssFFF"); } /// /// Creates a timestamp from a specified datetime /// public static string CreateTimestamp(DateTime date) { return date.ToString("yyyyMMddHHmmssFFF"); } } }