-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert
datetime
in Lua within OffsetDateTime
in Java
Since `datetime` in Lua supports working with offsets, it is logical to allow it to be converted to `OffsetDateTime` in Java to expand the possibilities of working with dates
- Loading branch information
1 parent
a86d4e8
commit cb4d1b9
Showing
9 changed files
with
408 additions
and
3 deletions.
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
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
81 changes: 81 additions & 0 deletions
81
...tool/driver/mappers/converters/object/DefaultOffsetDateTimeToExtensionValueConverter.java
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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package io.tarantool.driver.mappers.converters.object; | ||
|
||
import io.tarantool.driver.mappers.converters.ObjectConverter; | ||
import org.msgpack.value.ExtensionValue; | ||
import org.msgpack.value.ValueFactory; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.time.OffsetDateTime; | ||
|
||
import static io.tarantool.driver.mappers.converters.value.defaults.DefaultExtensionValueToInstantConverter.DATETIME_TYPE; | ||
import static io.tarantool.driver.mappers.converters.value.defaults.DefaultExtensionValueToOffsetDateTimeConverter.SECONDS_PER_MINUTE; | ||
import static java.nio.ByteOrder.LITTLE_ENDIAN; | ||
import static java.time.ZoneOffset.UTC; | ||
|
||
/** | ||
* Default {@link ExtensionValue} to {@link java.time.OffsetDateTime} converter. | ||
* | ||
* @author Valeriy Vyrva | ||
*/ | ||
public class DefaultOffsetDateTimeToExtensionValueConverter implements ObjectConverter<OffsetDateTime, ExtensionValue> { | ||
|
||
private static final long serialVersionUID = 20231027114017L; | ||
|
||
/** | ||
* Will contain only requited part: | ||
* <ol> | ||
* <li>{@code 8 bytes}: Seconds since Epoch.</li> | ||
* </ol> | ||
* | ||
* @see <a href="https://github.com/tarantool/tarantool/blob/master/src/lib/core/datetime.h#L85">struct datetime</a> | ||
*/ | ||
private static final int BUFFER_SIZE_COMPACT = Long.BYTES; | ||
/** | ||
* Will contain and required and optional parts: | ||
* <ol> | ||
* <li>{@code 8 bytes}: Seconds since Epoch.</li> | ||
* <li>{@code 4 bytes}: Nanoseconds.</li> | ||
* <li>{@code 2 bytes}: Offset in minutes from UTC.</li> | ||
* <li>{@code 2 bytes}: Olson timezone id.</li> | ||
* </ol> | ||
* The "timezone id" is not used on Java. | ||
* | ||
* @see <a href="https://github.com/tarantool/tarantool/blob/master/src/lib/core/datetime.h#L85">struct datetime</a> | ||
*/ | ||
private static final int BUFFER_SIZE_COMPLETE = Long.BYTES + Integer.BYTES + Short.BYTES + Short.BYTES; | ||
|
||
@Override | ||
public ExtensionValue toValue(OffsetDateTime object) { | ||
return ValueFactory.newExtension(DATETIME_TYPE, toBytes(object)); | ||
} | ||
|
||
/** | ||
* Encode java object into protocol level representation. | ||
* | ||
* @param object Object to encode | ||
* @return Protocol level representation | ||
* @see | ||
* <a href="https://github.com/tarantool/tarantool/blob/master/src/lib/core/mp_datetime.c#L18"> | ||
* serialization schema</a> | ||
* @see | ||
* <a href="https://github.com/tarantool/tarantool/blob/master/src/lib/core/datetime.h#L85">struct datetime</a> | ||
* @see | ||
* <a href="https://github.com/tarantool/tarantool/blob/master/src/lib/core/mp_datetime.c#L107">datetime_pack</a> | ||
* @see | ||
* <a href="https://github.com/tarantool/tarantool/blob/master/src/lib/core/mp_datetime.c#L56">datetime_unpack</a> | ||
*/ | ||
private byte[] toBytes(OffsetDateTime object) { | ||
boolean isCompact = object.getNano() == 0 && object.getOffset().equals(UTC); | ||
ByteBuffer buffer = ByteBuffer.wrap(new byte[isCompact ? BUFFER_SIZE_COMPACT : BUFFER_SIZE_COMPLETE]); | ||
buffer.order(LITTLE_ENDIAN); | ||
//Required part | ||
buffer.putLong(object.toEpochSecond()); | ||
//Optional part | ||
if (!isCompact) { | ||
buffer.putInt(object.getNano()); | ||
buffer.putShort((short) (object.getOffset().getTotalSeconds() / SECONDS_PER_MINUTE)); | ||
} | ||
return buffer.array(); | ||
} | ||
|
||
} |
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
62 changes: 62 additions & 0 deletions
62
...ver/mappers/converters/value/defaults/DefaultExtensionValueToOffsetDateTimeConverter.java
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package io.tarantool.driver.mappers.converters.value.defaults; | ||
|
||
import io.tarantool.driver.mappers.converters.ValueConverter; | ||
import org.msgpack.value.ExtensionValue; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.time.Instant; | ||
import java.time.OffsetDateTime; | ||
import java.time.ZoneOffset; | ||
|
||
import static io.tarantool.driver.mappers.converters.value.defaults.DefaultExtensionValueToInstantConverter.DATETIME_TYPE; | ||
import static java.nio.ByteOrder.LITTLE_ENDIAN; | ||
import static java.time.ZoneOffset.UTC; | ||
|
||
/** | ||
* Default {@link ExtensionValue} to {@link java.time.OffsetDateTime} converter. | ||
* | ||
* @author Valeriy Vyrva | ||
*/ | ||
public class DefaultExtensionValueToOffsetDateTimeConverter implements ValueConverter<ExtensionValue, OffsetDateTime> { | ||
|
||
private static final long serialVersionUID = 20231027114017L; | ||
|
||
public static final int SECONDS_PER_MINUTE = 60; | ||
|
||
@Override | ||
public boolean canConvertValue(ExtensionValue value) { | ||
return value.getType() == DATETIME_TYPE; | ||
} | ||
|
||
@Override | ||
public OffsetDateTime fromValue(ExtensionValue value) { | ||
return fromBytes(value.getData()); | ||
} | ||
|
||
/** | ||
* Decode protocol level representation into java object. | ||
* | ||
* @param value Bytes from protocol level | ||
* @return Decoded value | ||
* @see | ||
* <a href="https://github.com/tarantool/tarantool/blob/master/src/lib/core/mp_datetime.c#L18"> | ||
* serialization schema</a> | ||
* @see | ||
* <a href="https://github.com/tarantool/tarantool/blob/master/src/lib/core/datetime.h#L85">struct datetime</a> | ||
* @see | ||
* <a href="https://github.com/tarantool/tarantool/blob/master/src/lib/core/mp_datetime.c#L107">datetime_pack</a> | ||
* @see | ||
* <a href="https://github.com/tarantool/tarantool/blob/master/src/lib/core/mp_datetime.c#L56">datetime_unpack</a> | ||
*/ | ||
private OffsetDateTime fromBytes(byte[] value) { | ||
ByteBuffer buffer = ByteBuffer.wrap(value); | ||
buffer.order(LITTLE_ENDIAN); | ||
return Instant | ||
//Required part | ||
.ofEpochSecond(buffer.getLong()) | ||
//Optional part | ||
.plusNanos(buffer.hasRemaining() ? buffer.getInt() : 0) | ||
.atOffset(buffer.hasRemaining() ? ZoneOffset.ofTotalSeconds(buffer.getShort() * SECONDS_PER_MINUTE) : UTC); | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.