Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request: A function to get ID at perticular instance of time in past #24

Open
msonawane opened this issue Dec 28, 2019 · 4 comments
Open

Comments

@msonawane
Copy link

Usecase: snowflake as database primary key. it will be useful to get all records inserted before 1 month.
It will be great to have a function that:

  1. given a time in past generates ID
  2. ignores node id ?
@bwmarrin
Copy link
Owner

bwmarrin commented Feb 1, 2020

I'm sorry for not noticing this issue sooner - but here I am :)

I'm a bit confused on your use case, maybe you can give me a bit more context?

Are you wanting to generate these just for the purpose of doing a sql query? So you can query a database for all records where the IDs created with in a given time frame?

@msonawane
Copy link
Author

yes

@bwmarrin
Copy link
Owner

bwmarrin commented Feb 5, 2020

Hmn, I suppose this is a bit tricky. I'm not entirely sure if this should be in the library - but I'm not opposed to it if I can come up with a good method name for it :)

Here's a example of how to accomplish this

// Generate a Go Datetime, you could use time.X funcs to get a value at a specific date/time
dt := time.Now()

// Convert it into Unix timestamp in milliseconds and offset it by the snowflake Epoch
t = (dt.UnixNano() / 1000000) - (snowflake.Epoch)

// shift it over 22 bits, this should be (nodebits + stepbits)
t = (t) << 22

// Print the result
fmt.Printf("Int64   t: %d\n", t)

@codemedic
Copy link
Contributor

How about adding a function to Node.

func (n *Node) MakeID(t time.Time, seq int64) (ID, error) {
    // ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants