-
Notifications
You must be signed in to change notification settings - Fork 767
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
Closes #158 by providing .Time() and .Count() #202
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ import ( | |
"errors" | ||
"fmt" | ||
"io" | ||
"math" | ||
"os" | ||
"reflect" | ||
"runtime" | ||
|
@@ -201,7 +202,6 @@ func readRandomUint32() uint32 { | |
return uint32((uint32(b[0]) << 0) | (uint32(b[1]) << 8) | (uint32(b[2]) << 16) | (uint32(b[3]) << 24)) | ||
} | ||
|
||
|
||
// machineId stores machine id generated once and used in subsequent calls | ||
// to NewObjectId function. | ||
var machineId = readMachineId() | ||
|
@@ -352,6 +352,50 @@ func Now() time.Time { | |
// strange reason has its own datatype defined in BSON. | ||
type MongoTimestamp int64 | ||
|
||
// Time returns the time part of ts which is stored with second precision. | ||
func (ts MongoTimestamp) Time() time.Time { | ||
b := make([]byte, 8, 8) | ||
binary.BigEndian.PutUint64(b, uint64(ts)) | ||
return time.Unix(int64(binary.BigEndian.Uint32(b[:4])), 0) | ||
} | ||
|
||
// Counter returns the counter part of ts. | ||
func (ts MongoTimestamp) Counter() uint32 { | ||
b := make([]byte, 8, 8) | ||
binary.BigEndian.PutUint64(b, uint64(ts)) | ||
return binary.BigEndian.Uint32(b[4:]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't that the same as uint32(ts)? |
||
} | ||
|
||
// NewMongoTimestamp creates a timestamp using the given date t with second precision | ||
// and counter c. | ||
// | ||
// Returns -1 and en error if time t is earlier than 1970-01-01T00:00:00Z | ||
// or later than 2106-02-07T06:28:15Z. | ||
// | ||
// Note that two timestamps are not allowed to have the same combination of time and counter, | ||
// so you have to make sure to increase the counter when creating multiple MongoTimestamps | ||
// within one second. | ||
func NewMongoTimestamp(t time.Time, c uint32) (MongoTimestamp, error) { | ||
var tv uint32 | ||
|
||
u := t.Unix() | ||
|
||
if u < 0 || u > math.MaxUint32 { | ||
return -1, errors.New("invalid value for time") | ||
} | ||
|
||
tv = uint32(u) | ||
|
||
buf := bytes.Buffer{} | ||
|
||
binary.Write(&buf, binary.BigEndian, tv) | ||
binary.Write(&buf, binary.BigEndian, c) | ||
|
||
i := int64(binary.BigEndian.Uint64(buf.Bytes())) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @niemeyer Also, should this style be converted to the terse bitshifting syntax? |
||
|
||
return MongoTimestamp(i), nil | ||
} | ||
|
||
type orderKey int64 | ||
|
||
// MaxKey is a special value that compares higher than all other possible BSON | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't that the same as int64(uint64(ts) >> 32)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of course. Doh!