Skip to content

Instantly share code, notes, and snippets.

@faester
Created June 12, 2015 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save faester/6eef960ae990cc52c311 to your computer and use it in GitHub Desktop.
Save faester/6eef960ae990cc52c311 to your computer and use it in GitHub Desktop.
public class UniqueNumber
{
private const long MaxNumbersPerDay = 10000000;
private readonly object _mutex = new object();
private long _runningNumber = 0;
private DateTime _lastDate = DateTime.MinValue;
public long GetNext()
{
lock (_mutex)
{
var now = DateTime.UtcNow.Date;
if (now != _lastDate)
{
_runningNumber = 0;
_lastDate = now;
}
_runningNumber++;
var datePart = now.Year*10000 + now.Month*100 + now.Day;
return datePart*MaxNumbersPerDay + _runningNumber;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment