Reputation: 1001
I need to generate the incremental numbers based on date. numbers created on a previous date (say 06-Jan-2014) should not be greater then numbers created on the coming dates (say 08-Jan-2014). how to add it with date ? I tried the down one but in few cases it is failing
static long num =1;
public long GetUniqueNumberAsPerDate(DateTime date)
{
string dateStr = date.ToString("yyMMdd");
long alwaysIncrementeduniqueNum = num + 1;
return Convert.Int64(dateStr + alwaysIncrementeduniqueNum.ToString());
}
Failing cases
Date Passed : 2014-06-06 Num : 14 Number Created : 14060614
Date Passed : 2013-06-06 Num : 132 Number Created : 130606132 ( which is greater then 14060614)
The function can be called at same time by multiple applications. so the dateTime (even milliseconds) can be same Any solution to this problem
Upvotes: 3
Views: 10926
Reputation: 20754
If you want to have incremental number on a machine you can use this class
public class IncrementalNumberGenerator
{
private readonly string _path;
private readonly EventWaitHandle _waitHandle;
public IncrementalNumberGenerator(string path)
{
_path = path;
_waitHandle = new EventWaitHandle(true, EventResetMode.AutoReset, Guid.NewGuid().ToString("N"));
if (!File.Exists(_path))
File.WriteAllText(_path,"0");
}
public ulong Next()
{
try
{
_waitHandle.WaitOne();
var currentValue = ulong.Parse(File.ReadAllText(_path));
File.WriteAllText(_path, (currentValue + 1).ToString());
return currentValue + 1;
}
finally
{
_waitHandle.Set();
}
}
}
this will use a named wait handle to synchronize over different threads and processes.
use it like
var ing = new IncrementalNumberGenerator(@"c:\data\temp\synch");
and call Next
when you need new number. By using this class you can get incremental number even if you have multiple instances of your application running or even if your application restarted.
Upvotes: 1
Reputation: 5074
I need to generate the incremental numbers based on date. numbers created on a previous date should not be greater then numbers created on the coming dates.
According to this requirement you can simply take
date.Ticks
It provides a number that is strictly increasing with the DateTime
.
If you want to avoid equal numbers on the same tick, you can add
Thread.Sleep(16);
Windows' timer interrupt ticks at 64 Hz. This would however imply that you cannot generate more than 64 values per second.
Upvotes: 1
Reputation: 15076
The problem seems to be that your running number (alwaysincrementeduniquenumber) varies in the range from 1 to 1000. And when you do a string concatenation the final number will have less or more digits.
You could do something like
const long k = 1000000;
long value = long.Parse(dateStr) * k + alwaysIncrementeduniqueNum;
Assuming that you do not produce more than K
records per day, this should guarantee that you don't have this problem.
Upvotes: 2
Reputation: 1132
public long GetUniqueNumberAsPerDate(DateTime date)
{
double dateStr = date.ToOADate();
dateStr *= 1000000;
long alwaysIncrementeduniqueNum = num + 1;
string uniqueNumber = alwaysIncrementeduniqueNum + " " + dateStr;
return long.Parse(uniqueNumber);
}
i think this should work, it first converts the date into a decimal number which is then muliplied to get rid of the decimals, and then the incrementing number is just added at the front as a string.
You can switch this aregment (date number first) so that the date number will always be bigger at a later date
Upvotes: 1