-
-
Save faester/6eef960ae990cc52c311 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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