Skip to content

Commit

Permalink
Adds suggested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
popematt committed Nov 2, 2023
1 parent 54753cc commit ea7cad8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
25 changes: 17 additions & 8 deletions src/com/amazon/ion/impl/bin/IonEncoder_1_1.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ public static int writeTimestampValue(WriteBuffer buffer, Timestamp value) {
}

// Condition 2: The fractional seconds are a common precision.
if (value.getFractionalSecond() != null) {
int secondsScale = value.getFractionalSecond().scale();
if (value.getZFractionalSecond() != null) {
int secondsScale = value.getZFractionalSecond().scale();
if (secondsScale != 0 && secondsScale != 3 && secondsScale != 6 && secondsScale != 9) {
return writeLongFormTimestampValue(buffer, value);
}
Expand Down Expand Up @@ -234,9 +234,12 @@ private static int writeShortFormTimestampValue(WriteBuffer buffer, Timestamp va

bits |= ((long) value.getSecond()) << S_U_TIMESTAMP_SECOND_BIT_OFFSET;

int secondsScale = value.getDecimalSecond().scale();
int secondsScale = 0;
if (value.getZFractionalSecond() != null) {
secondsScale = value.getZFractionalSecond().scale();
}
if (secondsScale != 0) {
long fractionalSeconds = value.getDecimalSecond().remainder(BigDecimal.ONE).movePointRight(secondsScale).longValue();
long fractionalSeconds = value.getZFractionalSecond().unscaledValue().longValue();
bits |= fractionalSeconds << S_U_TIMESTAMP_FRACTION_BIT_OFFSET;
}
switch (secondsScale) {
Expand Down Expand Up @@ -275,9 +278,12 @@ private static int writeShortFormTimestampValue(WriteBuffer buffer, Timestamp va
// if there are nanoseconds (which is too much for one long) and the boundary between the seconds
// and fractional seconds subfields conveniently aligns with a byte boundary.
long fractionBits = 0;
int secondsScale = value.getDecimalSecond().scale();
int secondsScale = 0;
if (value.getZFractionalSecond() != null) {
secondsScale = value.getZFractionalSecond().scale();
}
if (secondsScale != 0) {
fractionBits = value.getDecimalSecond().remainder(BigDecimal.ONE).movePointRight(secondsScale).longValue();
fractionBits = value.getZFractionalSecond().unscaledValue().longValue();
}
switch (secondsScale) {
case 0:
Expand Down Expand Up @@ -350,14 +356,17 @@ static int writeLongFormTimestampValue(WriteBuffer buffer, Timestamp value) {


bits |= ((long) value.getSecond()) << L_TIMESTAMP_SECOND_BIT_OFFSET;
int secondsScale = value.getDecimalSecond().scale();
int secondsScale = 0;
if (value.getZFractionalSecond() != null) {
secondsScale = value.getZFractionalSecond().scale();
}
if (secondsScale == 0) {
buffer.writeFlexUInt(7);
buffer.writeFixedIntOrUInt(bits, 7);
return 9; // OpCode + FlexUInt + 7 bytes data
}

BigDecimal fractionalSeconds = value.getDecimalSecond().remainder(BigDecimal.ONE);
BigDecimal fractionalSeconds = value.getZFractionalSecond();
BigInteger coefficient = fractionalSeconds.unscaledValue();
long exponent = fractionalSeconds.scale();
int numCoefficientBytes = WriteBuffer.flexUIntLength(coefficient);
Expand Down
2 changes: 1 addition & 1 deletion src/com/amazon/ion/impl/bin/Ion_1_1_Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private Ion_1_1_Constants() {}
static final int L_TIMESTAMP_HOUR_BIT_OFFSET = 23;
static final int L_TIMESTAMP_MINUTE_BIT_OFFSET = 28;
static final int L_TIMESTAMP_OFFSET_BIT_OFFSET = 34;
static final int L_TIMESTAMP_SECOND_BIT_OFFSET = 44;
static final int L_TIMESTAMP_SECOND_BIT_OFFSET = 46;
static final int L_TIMESTAMP_UNKNOWN_OFFSET_VALUE = 0b111111111111;

//////// Bit masks ////////
Expand Down
2 changes: 1 addition & 1 deletion test/com/amazon/ion/impl/bin/IonEncoder_1_1Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public void testWriteTimestampValueWithKnownOffsetShortForm(@ConvertWith(StringT
"1947-12-23T00:00Z, 11110111 00001101 10011011 00000111 01011111 00000000 10000000 00010110",
"1947-12-23T23:59Z, 11110111 00001101 10011011 00000111 11011111 10111011 10000011 00010110",
"1947-12-23T23:59:00Z, 11110111 00001111 10011011 00000111 11011111 10111011 10000011 00010110 00000000",
"1947-12-23T23:59:59Z, 11110111 00001111 10011011 00000111 11011111 10111011 10000011 10110110 00000011",
"1947-12-23T23:59:59Z, 11110111 00001111 10011011 00000111 11011111 10111011 10000011 11010110 00001110",
"1947-12-23T23:59:00.0Z, 11110111 00010011 10011011 00000111 11011111 10111011 10000011 00010110 00000000 00000001 00000001",
"1947-12-23T23:59:00.00Z, 11110111 00010011 10011011 00000111 11011111 10111011 10000011 00010110 00000000 00000001 00000010",
"1947-12-23T23:59:00.000Z, 11110111 00010011 10011011 00000111 11011111 10111011 10000011 00010110 00000000 00000001 00000011",
Expand Down

0 comments on commit ea7cad8

Please sign in to comment.