-
Notifications
You must be signed in to change notification settings - Fork 32
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
Correct the coefficient value of timestamp nanoseconds when writing to Ion binary #177
Changes from all commits
4c4b12e
968633c
23f7953
2e6dcd9
0600361
3c5587e
5acde3b
02a9c36
44e0b20
b3422b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -652,30 +652,12 @@ func (b *bitstream) ReadTimestamp() (Timestamp, error) { | |
|
||
// Check the fractional seconds part of the timestamp. | ||
if length > 0 { | ||
// First byte indicates number of precision units in fractional seconds. | ||
fracSecsBytes, err := b.in.Peek(1) | ||
nsecs, overflow, fractionPrecision, err = b.readNsecs(length) | ||
if err != nil { | ||
return Timestamp{}, err | ||
} | ||
|
||
nsecs, overflow, err = b.readNsecs(length) | ||
if err != nil { | ||
return Timestamp{}, err | ||
} | ||
|
||
if nsecs > 0 { | ||
fractionPrecision = 9 | ||
|
||
// Adjust fractionPrecision for each trailing zero. | ||
// ie. .123456000 should have 6 fractionPrecision instead of 9 | ||
ns := nsecs | ||
for ns > 0 && (ns%10) == 0 { | ||
ns /= 10 | ||
fractionPrecision-- | ||
} | ||
precision = TimestampPrecisionNanosecond | ||
} else if len(fracSecsBytes) > 0 && fracSecsBytes[0] > 0xC0 && (fracSecsBytes[0]^0xC0) > 0 { | ||
fractionPrecision = fracSecsBytes[0] ^ 0xC0 | ||
if fractionPrecision > 0 { | ||
precision = TimestampPrecisionNanosecond | ||
} | ||
} | ||
|
@@ -692,32 +674,34 @@ func (b *bitstream) ReadTimestamp() (Timestamp, error) { | |
} | ||
|
||
// ReadNsecs reads the fraction part of a timestamp and rounds to nanoseconds. | ||
// This function returns the nanoseconds as an int, overflow as a bool, and an error | ||
// This function returns the nanoseconds as an int, overflow as a bool, exponent as an uint8, and an error | ||
// if there was a problem executing this function. | ||
func (b *bitstream) readNsecs(length uint64) (int, bool, error) { | ||
func (b *bitstream) readNsecs(length uint64) (int, bool, uint8, error) { | ||
d, err := b.readDecimal(length) | ||
if err != nil { | ||
return 0, false, err | ||
return 0, false, 0, err | ||
} | ||
|
||
nsec, err := d.ShiftL(9).trunc() | ||
if err != nil || nsec < 0 || nsec > 999999999 { | ||
msg := fmt.Sprintf("invalid timestamp fraction: %v", d) | ||
return 0, false, &SyntaxError{msg, b.pos} | ||
return 0, false, 0, &SyntaxError{msg, b.pos} | ||
} | ||
|
||
nsec, err = d.ShiftL(9).round() | ||
if err != nil { | ||
msg := fmt.Sprintf("invalid timestamp fraction: %v", d) | ||
return 0, false, &SyntaxError{msg, b.pos} | ||
return 0, false, 0, &SyntaxError{msg, b.pos} | ||
} | ||
|
||
exponent := uint8(d.scale) | ||
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. Let's add a comment explaining that this cast should be safe because the max precision of 9 can fit inside uint8. 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. That is not true because the Ion timestamp spec mentions:
Other Ion implementations can support arbitrary precision so Ion binary generated from Ion Java can have precision reduced. Currently Ion Go support timestamps up to 9 precision units. I created a ticket to update timestamps to support unlimited precision. |
||
|
||
// Overflow to second. | ||
if nsec == 1000000000 { | ||
return 0, true, nil | ||
return 0, true, exponent, nil | ||
} | ||
|
||
return int(nsec), false, nil | ||
return int(nsec), false, exponent, nil | ||
} | ||
|
||
// ReadDecimal reads a decimal value of the given length: an exponent encoded as a | ||
|
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.
Why this change?
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.
Because the test binary data doesn't have nanoseconds. It was failing because calling NewTimestamp with
TimestampPrecisionNanosecond
will set fractional precision: 9.