Skip to content

Commit bb525d9

Browse files
committed
Fix negative timezone offset parsing.
We now correctly decode the localMinutesOffset by parsing the number as small int instead of unsigned short. Also, encode the offset correctly as smallint. [resolves #208] Signed-off-by: Mark Paluch <mpaluch@vmware.com>
1 parent 303a519 commit bb525d9

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

src/main/java/io/r2dbc/mssql/codec/OffsetDateTimeCodec.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ OffsetDateTime doDecode(ByteBuf buffer, Length length, TypeInformation type, Cla
9898
LocalTime localTime = LocalTimeCodec.INSTANCE.doDecode(buffer, length, type, LocalTime.class);
9999
LocalDate localDate = LocalDateCodec.INSTANCE.doDecode(buffer, length, type, LocalDate.class);
100100

101-
int localMinutesOffset = Decode.uShort(buffer);
101+
int localMinutesOffset = Decode.smallInt(buffer);
102102
ZoneOffset offset = ZoneOffset.ofTotalSeconds(localMinutesOffset * 60);
103103

104104
return OffsetDateTime.of(localTime.atDate(localDate), offset).plusMinutes(localMinutesOffset);
@@ -111,7 +111,7 @@ static void doEncode(ByteBuf buffer, OffsetDateTime value) {
111111

112112
int localMinutesOffset = value.getOffset().getTotalSeconds() / 60;
113113

114-
Encode.uShort(buffer, localMinutesOffset);
114+
Encode.smallInt(buffer, localMinutesOffset);
115115
}
116116

117117
}

src/main/java/io/r2dbc/mssql/message/tds/Encode.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,16 @@ public static void smallInt(ByteBuf buffer, short value) {
151151
buffer.writeShortLE(value);
152152
}
153153

154+
/**
155+
* Encode short number. SQL server type {@code SMALLINT}.
156+
*
157+
* @param buffer the data buffer.
158+
* @param value the value to encode.
159+
*/
160+
public static void smallInt(ByteBuf buffer, int value) {
161+
buffer.writeShortLE(value);
162+
}
163+
154164
/**
155165
* Encode integer number. SQL server type {@code INT}.
156166
*

src/test/java/io/r2dbc/mssql/CodecIntegrationTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ void shouldEncodeDateTime2() {
143143
@Test
144144
void shouldEncodeZonedDateTimeAsDatetimeoffset() {
145145
testType(connection, "DATETIMEOFFSET", ZonedDateTime.parse("2018-08-27T17:41:14.890+00:45"), ZonedDateTime.class, OffsetDateTime.parse("2018-08-27T17:41:14.890+00:45"));
146+
testType(connection, "DATETIMEOFFSET", ZonedDateTime.parse("2018-08-27T17:41:14.890-01:45"), ZonedDateTime.class, OffsetDateTime.parse("2018-08-27T17:41:14.890-01:45"));
146147
}
147148

148149
@Test

src/test/java/io/r2dbc/mssql/codec/OffsetDateTimeCodecUnitTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ void shouldEncodeDatetimeoffset() {
4949
assertThat(encoded.getFormalType()).isEqualTo("datetimeoffset");
5050
}
5151

52+
@Test
53+
void shouldEncodeDatetimeOffsetNegativeTz() {
54+
55+
OffsetDateTime value = OffsetDateTime.parse("2018-08-27T17:41:14.890-00:45");
56+
57+
Encoded encoded = OffsetDateTimeCodec.INSTANCE.encode(TestByteBufAllocator.TEST, RpcParameterContext.out(), value);
58+
59+
EncodedAssert.assertThat(encoded).isEqualToHex("07 0a a0 74 84 8a 9a a4 3e 0b d3 ff");
60+
assertThat(encoded.getFormalType()).isEqualTo("datetimeoffset");
61+
}
62+
5263
@Test
5364
void shouldEncodeNull() {
5465

@@ -67,4 +78,15 @@ void shouldDecodeDateTimeOffset() {
6778

6879
assertThat(decoded).isEqualTo("2018-08-27T17:41:14.890+00:45");
6980
}
81+
82+
@Test
83+
void shouldDecodeDateTimeOffsetNegativeTz() {
84+
85+
ByteBuf buffer = HexUtils.decodeToByteBuf("0a a0 74 84 8a 9a a4 3e 0b d3 ff");
86+
87+
OffsetDateTime decoded = OffsetDateTimeCodec.INSTANCE.decode(buffer, ColumnUtil.createColumn(DATETIMEOFFSET), OffsetDateTime.class);
88+
89+
assertThat(decoded).isEqualTo("2018-08-27T17:41:14.890-00:45");
90+
}
91+
7092
}

0 commit comments

Comments
 (0)