下面列出了java.time.LocalTime#getNano ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected final ByteBuf toBinary(LocalDateTime dateTime) {
ByteBuf buf = LocalDateCodecTest.encode(dateTime.toLocalDate());
LocalTime time = dateTime.toLocalTime();
if (LocalTime.MIDNIGHT.equals(time)) {
return buf;
}
buf.writeByte(time.getHour())
.writeByte(time.getMinute())
.writeByte(time.getSecond());
if (time.getNano() != 0) {
buf.writeIntLE((int) TimeUnit.NANOSECONDS.toMicros(time.getNano()));
}
return buf;
}
protected final ByteBuf toBinary(LocalTime time) {
if (LocalTime.MIDNIGHT.equals(time)) {
return Unpooled.buffer(0, 0);
}
ByteBuf buf = Unpooled.buffer().writeBoolean(false)
.writeIntLE(0)
.writeByte(time.getHour())
.writeByte(time.getMinute())
.writeByte(time.getSecond());
if (time.getNano() != 0) {
buf.writeIntLE((int) TimeUnit.NANOSECONDS.toMicros(time.getNano()));
}
return buf;
}
protected final ByteBuf toBinary(LocalDateTime dateTime) {
ByteBuf buf = LocalDateCodecTest.encode(dateTime.toLocalDate());
LocalTime time = dateTime.toLocalTime();
if (LocalTime.MIDNIGHT.equals(time)) {
return buf;
}
buf.writeByte(time.getHour())
.writeByte(time.getMinute())
.writeByte(time.getSecond());
if (time.getNano() != 0) {
buf.writeIntLE((int) TimeUnit.NANOSECONDS.toMicros(time.getNano()));
}
return buf;
}
protected final ByteBuf toBinary(LocalTime time) {
if (LocalTime.MIDNIGHT.equals(time)) {
return Unpooled.buffer(0, 0);
}
ByteBuf buf = Unpooled.buffer().writeBoolean(false)
.writeIntLE(0)
.writeByte(time.getHour())
.writeByte(time.getMinute())
.writeByte(time.getSecond());
if (time.getNano() != 0) {
buf.writeIntLE((int) TimeUnit.NANOSECONDS.toMicros(time.getNano()));
}
return buf;
}
private void encodeTimeNParameter(ByteBuf payload, LocalTime time, byte scale) {
payload.writeByte(0x00);
payload.writeByte(0x00);
payload.writeByte(MSSQLDataTypeId.TIMENTYPE_ID);
payload.writeByte(scale); //FIXME scale?
if (time == null) {
payload.writeByte(0);
} else {
int length;
if (scale <= 2) {
length = 3;
} else if (scale <= 4) {
length = 4;
} else {
length = 5;
}
payload.writeByte(length);
long nanos = time.getNano();
int seconds = time.toSecondOfDay();
long value = (long) ((long) seconds * Math.pow(10, scale) + nanos);
encodeInt40(payload, value);
}
}
private final void _serializeAsArrayContents(LocalTime value, JsonGenerator g,
SerializerProvider provider) throws IOException
{
g.writeNumber(value.getHour());
g.writeNumber(value.getMinute());
int secs = value.getSecond();
int nanos = value.getNano();
if ((secs > 0) || (nanos > 0))
{
g.writeNumber(secs);
if (nanos > 0) {
if (useNanoseconds(provider)) {
g.writeNumber(nanos);
} else {
g.writeNumber(value.get(ChronoField.MILLI_OF_SECOND));
}
}
}
}
private LocalTime convertToLocalTime(ObjectMapper mapper, JsonNode jsonNode) {
// according to RFC 3339 every full-time must have a timezone;
// until we have full timezone support, we only support UTC;
// users can parse their time as string as a workaround
TemporalAccessor parsedTime = RFC3339_TIME_FORMAT.parse(jsonNode.asText());
ZoneOffset zoneOffset = parsedTime.query(TemporalQueries.offset());
LocalTime localTime = parsedTime.query(TemporalQueries.localTime());
if (zoneOffset != null && zoneOffset.getTotalSeconds() != 0 || localTime.getNano() != 0) {
throw new IllegalStateException(
"Invalid time format. Only a time in UTC timezone without milliseconds is supported yet.");
}
return localTime;
}
static ByteBuf encodeBinary(ByteBufAllocator alloc, LocalDateTime value) {
LocalTime time = value.toLocalTime();
if (LocalTime.MIDNIGHT.equals(time)) {
return LocalDateCodec.encodeDate(alloc, value.toLocalDate());
}
int nano = time.getNano();
int bytes = nano > 0 ? DateTimes.MICRO_DATETIME_SIZE : DateTimes.DATETIME_SIZE;
ByteBuf buf = alloc.buffer(Byte.BYTES + bytes);
try {
buf.writeByte(bytes)
.writeShortLE(value.getYear())
.writeByte(value.getMonthValue())
.writeByte(value.getDayOfMonth())
.writeByte(time.getHour())
.writeByte(time.getMinute())
.writeByte(time.getSecond());
if (nano > 0) {
return buf.writeIntLE(nano / DateTimes.NANOS_OF_MICRO);
}
return buf;
} catch (Throwable e) {
buf.release();
throw e;
}
}
static ByteBuf encodeBinary(ByteBufAllocator alloc, LocalTime time) {
if (LocalTime.MIDNIGHT.equals(time)) {
// It is zero of var int, not terminal.
return alloc.buffer(Byte.BYTES).writeByte(0);
}
int nanos = time.getNano();
int size = nanos > 0 ? MICRO_TIME_SIZE : TIME_SIZE;
ByteBuf buf = alloc.buffer(Byte.BYTES + size);
try {
buf.writeByte(size)
.writeBoolean(false)
.writeIntLE(0)
.writeByte(time.getHour())
.writeByte(time.getMinute())
.writeByte(time.getSecond());
if (nanos > 0) {
return buf.writeIntLE(nanos / NANOS_OF_MICRO);
}
return buf;
} catch (Throwable e) {
buf.release();
throw e;
}
}
public static void putNormalizedKeyLocalTime(LocalTime record, MemorySegment target, int offset, int numBytes) {
int hour = record.getHour();
if (numBytes > 0) {
target.put(offset, (byte) (hour & 0xff - Byte.MIN_VALUE));
numBytes -= 1;
offset += 1;
}
int minute = record.getMinute();
if (numBytes > 0) {
target.put(offset, (byte) (minute & 0xff - Byte.MIN_VALUE));
numBytes -= 1;
offset += 1;
}
int second = record.getSecond();
if (numBytes > 0) {
target.put(offset, (byte) (second & 0xff - Byte.MIN_VALUE));
numBytes -= 1;
offset += 1;
}
int nano = record.getNano();
int unsignedNano = nano - Integer.MIN_VALUE;
if (numBytes >= 4) {
target.putIntBigEndian(offset, unsignedNano);
numBytes -= 4;
offset += 4;
} else if (numBytes > 0) {
for (int i = 0; numBytes > 0; numBytes--, i++) {
target.put(offset + i, (byte) (unsignedNano >>> ((3 - i) << 3)));
}
return;
}
for (int i = 0; i < numBytes; i++) {
target.put(offset + i, (byte) 0);
}
}
public static void putNormalizedKeyLocalTime(LocalTime record, MemorySegment target, int offset, int numBytes) {
int hour = record.getHour();
if (numBytes > 0) {
target.put(offset, (byte) (hour & 0xff - Byte.MIN_VALUE));
numBytes -= 1;
offset += 1;
}
int minute = record.getMinute();
if (numBytes > 0) {
target.put(offset, (byte) (minute & 0xff - Byte.MIN_VALUE));
numBytes -= 1;
offset += 1;
}
int second = record.getSecond();
if (numBytes > 0) {
target.put(offset, (byte) (second & 0xff - Byte.MIN_VALUE));
numBytes -= 1;
offset += 1;
}
int nano = record.getNano();
int unsignedNano = nano - Integer.MIN_VALUE;
if (numBytes >= 4) {
target.putIntBigEndian(offset, unsignedNano);
numBytes -= 4;
offset += 4;
} else if (numBytes > 0) {
for (int i = 0; numBytes > 0; numBytes--, i++) {
target.put(offset + i, (byte) (unsignedNano >>> ((3 - i) << 3)));
}
return;
}
for (int i = 0; i < numBytes; i++) {
target.put(offset + i, (byte) 0);
}
}
@Override
public void wrap(final LocalTime localTime) {
this.hour = localTime.getHour();
this.minute = localTime.getMinute();
this.second = localTime.getSecond();
this.nano = localTime.getNano();
}
static ByteBuf encodeBinary(ByteBufAllocator alloc, LocalDateTime value) {
LocalTime time = value.toLocalTime();
if (LocalTime.MIDNIGHT.equals(time)) {
return LocalDateCodec.encodeDate(alloc, value.toLocalDate());
}
int nano = time.getNano();
int bytes = nano > 0 ? DateTimes.MICRO_DATETIME_SIZE : DateTimes.DATETIME_SIZE;
ByteBuf buf = alloc.buffer(Byte.BYTES + bytes);
try {
buf.writeByte(bytes)
.writeShortLE(value.getYear())
.writeByte(value.getMonthValue())
.writeByte(value.getDayOfMonth())
.writeByte(time.getHour())
.writeByte(time.getMinute())
.writeByte(time.getSecond());
if (nano > 0) {
return buf.writeIntLE(nano / DateTimes.NANOS_OF_MICRO);
}
return buf;
} catch (Throwable e) {
buf.release();
throw e;
}
}
static ByteBuf encodeBinary(ByteBufAllocator alloc, LocalTime time) {
if (LocalTime.MIDNIGHT.equals(time)) {
// It is zero of var int, not terminal.
return alloc.buffer(Byte.BYTES).writeByte(0);
}
int nanos = time.getNano();
int size = nanos > 0 ? MICRO_TIME_SIZE : TIME_SIZE;
ByteBuf buf = alloc.buffer(Byte.BYTES + size);
try {
buf.writeByte(size)
.writeBoolean(false)
.writeIntLE(0)
.writeByte(time.getHour())
.writeByte(time.getMinute())
.writeByte(time.getSecond());
if (nanos > 0) {
return buf.writeIntLE(nanos / NANOS_OF_MICRO);
}
return buf;
} catch (Throwable e) {
buf.release();
throw e;
}
}
private static void binaryEncodeTime(LocalTime value, ByteBuf buffer) {
int hour = value.getHour();
int minute = value.getMinute();
int second = value.getSecond();
int nano = value.getNano();
if (nano == 0) {
if (hour == 0 && minute == 0 && second == 0) {
buffer.writeByte(0);
} else {
buffer.writeByte(8);
buffer.writeByte(0);
buffer.writeIntLE(0);
buffer.writeByte(hour);
buffer.writeByte(minute);
buffer.writeByte(second);
}
} else {
int microSecond = nano / 1000;
buffer.writeByte(12);
buffer.writeByte(0);
buffer.writeIntLE(0);
buffer.writeByte(hour);
buffer.writeByte(minute);
buffer.writeByte(second);
buffer.writeIntLE(microSecond);
}
}
/**
* Obtains an instance defining the yearly rule to create transitions between two offsets.
* <p>
* Applications should normally obtain an instance from {@link ZoneRules}.
* This factory is only intended for use when creating {@link ZoneRules}.
*
* @param month the month of the month-day of the first day of the cutover week, not null
* @param dayOfMonthIndicator the day of the month-day of the cutover week, positive if the week is that
* day or later, negative if the week is that day or earlier, counting from the last day of the month,
* from -28 to 31 excluding 0
* @param dayOfWeek the required day-of-week, null if the month-day should not be changed
* @param time the cutover time in the 'before' offset, not null
* @param timeEndOfDay whether the time is midnight at the end of day
* @param timeDefnition how to interpret the cutover
* @param standardOffset the standard offset in force at the cutover, not null
* @param offsetBefore the offset before the cutover, not null
* @param offsetAfter the offset after the cutover, not null
* @return the rule, not null
* @throws IllegalArgumentException if the day of month indicator is invalid
* @throws IllegalArgumentException if the end of day flag is true when the time is not midnight
* @throws IllegalArgumentException if {@code time.getNano()} returns non-zero value
*/
public static ZoneOffsetTransitionRule of(
Month month,
int dayOfMonthIndicator,
DayOfWeek dayOfWeek,
LocalTime time,
boolean timeEndOfDay,
TimeDefinition timeDefnition,
ZoneOffset standardOffset,
ZoneOffset offsetBefore,
ZoneOffset offsetAfter) {
Objects.requireNonNull(month, "month");
Objects.requireNonNull(time, "time");
Objects.requireNonNull(timeDefnition, "timeDefnition");
Objects.requireNonNull(standardOffset, "standardOffset");
Objects.requireNonNull(offsetBefore, "offsetBefore");
Objects.requireNonNull(offsetAfter, "offsetAfter");
if (dayOfMonthIndicator < -28 || dayOfMonthIndicator > 31 || dayOfMonthIndicator == 0) {
throw new IllegalArgumentException("Day of month indicator must be between -28 and 31 inclusive excluding zero");
}
if (timeEndOfDay && time.equals(LocalTime.MIDNIGHT) == false) {
throw new IllegalArgumentException("Time must be midnight when end of day flag is true");
}
if (time.getNano() != 0) {
throw new IllegalArgumentException("Time's nano-of-second must be zero");
}
return new ZoneOffsetTransitionRule(month, dayOfMonthIndicator, dayOfWeek, time, timeEndOfDay, timeDefnition, standardOffset, offsetBefore, offsetAfter);
}
/**
* Obtains an instance defining the yearly rule to create transitions between two offsets.
* <p>
* Applications should normally obtain an instance from {@link ZoneRules}.
* This factory is only intended for use when creating {@link ZoneRules}.
*
* @param month the month of the month-day of the first day of the cutover week, not null
* @param dayOfMonthIndicator the day of the month-day of the cutover week, positive if the week is that
* day or later, negative if the week is that day or earlier, counting from the last day of the month,
* from -28 to 31 excluding 0
* @param dayOfWeek the required day-of-week, null if the month-day should not be changed
* @param time the cutover time in the 'before' offset, not null
* @param timeEndOfDay whether the time is midnight at the end of day
* @param timeDefnition how to interpret the cutover
* @param standardOffset the standard offset in force at the cutover, not null
* @param offsetBefore the offset before the cutover, not null
* @param offsetAfter the offset after the cutover, not null
* @return the rule, not null
* @throws IllegalArgumentException if the day of month indicator is invalid
* @throws IllegalArgumentException if the end of day flag is true when the time is not midnight
* @throws IllegalArgumentException if {@code time.getNano()} returns non-zero value
*/
public static ZoneOffsetTransitionRule of(
Month month,
int dayOfMonthIndicator,
DayOfWeek dayOfWeek,
LocalTime time,
boolean timeEndOfDay,
TimeDefinition timeDefnition,
ZoneOffset standardOffset,
ZoneOffset offsetBefore,
ZoneOffset offsetAfter) {
Objects.requireNonNull(month, "month");
Objects.requireNonNull(time, "time");
Objects.requireNonNull(timeDefnition, "timeDefnition");
Objects.requireNonNull(standardOffset, "standardOffset");
Objects.requireNonNull(offsetBefore, "offsetBefore");
Objects.requireNonNull(offsetAfter, "offsetAfter");
if (dayOfMonthIndicator < -28 || dayOfMonthIndicator > 31 || dayOfMonthIndicator == 0) {
throw new IllegalArgumentException("Day of month indicator must be between -28 and 31 inclusive excluding zero");
}
if (timeEndOfDay && time.equals(LocalTime.MIDNIGHT) == false) {
throw new IllegalArgumentException("Time must be midnight when end of day flag is true");
}
if (time.getNano() != 0) {
throw new IllegalArgumentException("Time's nano-of-second must be zero");
}
return new ZoneOffsetTransitionRule(month, dayOfMonthIndicator, dayOfWeek, time, timeEndOfDay, timeDefnition, standardOffset, offsetBefore, offsetAfter);
}
static void encodeTime(ParameterWriter writer, LocalTime time) {
int micros = time.getNano() / NANOS_OF_MICRO;
DurationCodec.encodeTime(writer, false, time.getHour(), time.getMinute(), time.getSecond(), micros);
}
public LocalTimeHandle(LocalTime o) {
this.hour = o.getHour();
this.minute = o.getMinute();
this.second = o.getSecond();
this.nano = o.getNano();
}
static void encodeTime(ParameterWriter writer, LocalTime time) {
int micros = time.getNano() / NANOS_OF_MICRO;
DurationCodec.encodeTime(writer, false, time.getHour(), time.getMinute(), time.getSecond(), micros);
}