You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Zero/ZeroLevel/Services/IdGenerator.cs

68 lines
2.3 KiB

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

Powered by TurnKey Linux.