Coder Social home page Coder Social logo

Id to datetime about idgen HOT 10 CLOSED

robthree avatar robthree commented on August 18, 2024
Id to datetime

from idgen.

Comments (10)

RobThree avatar RobThree commented on August 18, 2024 3

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.

minhhungit avatar minhhungit commented on August 18, 2024 2

Thank you for your help, I've just bought you a coffee on p@ypal !

from idgen.

RobThree avatar RobThree commented on August 18, 2024 1

See the FromID() method which returns an ID structure.

from idgen.

minhhungit avatar minhhungit commented on August 18, 2024

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.

RobThree avatar RobThree commented on August 18, 2024

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.

minhhungit avatar minhhungit commented on August 18, 2024

@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.

RobThree avatar RobThree commented on August 18, 2024

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.

minhhungit avatar minhhungit commented on August 18, 2024

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.

minhhungit avatar minhhungit commented on August 18, 2024

Many thanks @RobThree , the sample helps a lot, I understood

from idgen.

RobThree avatar RobThree commented on August 18, 2024

That really wasn't necessary but thank you very much! Much appreciated! 👊

from idgen.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.