Comments (10)
Forget about the epoch. What you want is an ITimeSource
that you can 'force' to a specific date. So you implement an ITimeSource
:
public class MyTimeSource : ITimeSource
{
private static readonly TimeSpan _tickduration = TimeSpan.FromMilliseconds(1);
public DateTimeOffset Epoch { get; private set; }
public TimeSpan TickDuration => _tickduration;
private DateTimeOffset _current;
public MyTimeSource(DateTimeOffset? epoch = null)
{
Epoch = epoch ?? new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Utc);
_current = Epoch;
}
public void SetDateTime(DateTimeOffset dateTime) => _current = dateTime;
public void Adjust(TimeSpan timeSpan) => _current = _current.Add(timeSpan);
public long GetTicks() => (long)(_current - Epoch).TotalMilliseconds;
}
The above timesource has two methods: SetDateTime()
which allows you to specify an exact date/time and Adjust()
which allows you to "adjust" the current time of the timesource with a specified timespan to a later date*
Next, you tell IdGen to use your timesource and then you'll be able to 'set the clock' to whatever date/time you desire:
var mytimesource = new MyTimeSource();
var idgen = new IdGenerator(0, new IdGeneratorOptions(IdStructure.Default, mytimesource));
// Generate an ID
var id1 = idgen.CreateId(); // 0
// Set timesource to now
mytimesource.SetDateTime(DateTime.UtcNow);
var id2 = idgen.CreateId(); // 166125122225176576
// Set timesource to specific date
mytimesource.SetDateTime(new DateTime(2025, 1, 1, 0, 0, 0, DateTimeKind.Utc));
var id3 = idgen.CreateId(); // 662082630451200000
// Adjust timesource (+1 hour)
mytimesource.Adjust(TimeSpan.FromHours(1));
var id4 = idgen.CreateId(); // 662097729945600000
* Note that a specific IdGen instance will prevent you from setting the time 'backwards' to an earlier date; if you need to do that you'll need to create a new instance of the IdGen.
from idgen.
Thank you for your help, I've just bought you a coffee on p@ypal !
from idgen.
See the FromID()
method which returns an ID
structure.
from idgen.
thank you @RobThree , that anwsered my first question !
is there a way we can give generator any DateTime value and it will generate a ID for us, for example, if I give it timestamp 2030-01-01 00:00:00 then it will give me ID for the timestamp
from idgen.
Pass an ITimeSource
in the constructor via the IdGeneratorOptions
; you can use the DefaultTimeSource
if you don't want to implement your own and pass it a date to it's constructor.
from idgen.
@RobThree but ITimeSource define an Epoch , as I understand is the point start of IDs range
I have a fixed epoch, for example my epoch is 2020/01/01
I want to know what exactly ID value for a point in future, for example 2025/05/20 13:05:12
Everytime I give ID generator value 2025/05/20 13:05:12
I need it gives me back a fixed unique ID
ITimeSource is just help me define an epoch, not a point in future
from idgen.
I'm not talking about an epoch (although, when you would use the DefaultTimeSource
you would need to 'misuse' the epoch for it - I only suggested it if this was a one-time thing or something).
Simply implement an ITimeSource
on which you can set the date to whatever you want. Give it a SetTime()
method or something similar and you'll be able to set the time on your timesource to whatever you want.
from idgen.
But epoch is fixed, we don't change it. Sorry I am still confusing. I dont need to change it, if I change epoch then my Id will be changed based on the epoch then it is not correct anymore. ITimeSource just require epoch, it does not have another parameter for a Datetime that is for any day in future. I need two param for 2 datetime, one for epoch and one for a day in future
For example, I have epoch at 2020, at 2025 my ID = 5, at 2027 my Id = 10... (I dont want to change my epoch 2020, the epoch is fixed)
If I give the generator date 2025, I expect it will give me exactly ID = 5
from idgen.
Many thanks @RobThree , the sample helps a lot, I understood
from idgen.
That really wasn't necessary but thank you very much! Much appreciated! 👊
from idgen.
Related Issues (20)
- Drop support for netstandard 1.1 HOT 5
- Non-generic IIdGenerator HOT 5
- Avoid spending a sign bit by using ulongs HOT 13
- Tag 3.0.3 is missing HOT 1
- Documentation about generatorId collisions when id structure changes HOT 9
- IdGen is generating 17bit Id instead of 16bit? What should we change in the method so that it generate 16 bit Id instead of 17 bit now and in the future it continuous to generate 16 bit Id? HOT 11
- Is there a way to shorten the number of digits generated for an id? HOT 5
- Handling uniqueness ID across multiple instance of same service HOT 6
- IdGenerator does not contain a definition for 'Take' HOT 1
- Consider adding IdGenerator#CreateFromDateTime HOT 5
- Public Method Comments HOT 1
- SpinWait drastically slows continous ID generation rate for small tick length HOT 10
- Could we log how often we overflow sequence IDs and have to spinwait? HOT 1
- Concurrency issues with instances scale up HOT 2
- Use dependabot to keep references up to date HOT 1
- Sequence input for CreateId() HOT 3
- IIdGenerator is registered as a singleton, but duplicate IDs are still generated HOT 4
- Debugging InvalidSystemClockExceptions HOT 15
- Is there a way to generate id from the past HOT 1
- How to use this in distributed computing ? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from idgen.