com.datastax.driver.extras.codecs.jdk8.ZonedDateTimeCodec Maven / Gradle / Ivy
Show all versions of cassandra-driver-extras Show documentation
/*
* Copyright (C) 2012-2015 DataStax Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.datastax.driver.extras.codecs.jdk8;
import java.nio.ByteBuffer;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.util.List;
import static java.time.temporal.ChronoField.HOUR_OF_DAY;
import static java.time.temporal.ChronoField.MINUTE_OF_HOUR;
import static java.time.temporal.ChronoField.NANO_OF_SECOND;
import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;
import static com.google.common.base.Preconditions.checkArgument;
import com.datastax.driver.core.*;
import com.datastax.driver.core.exceptions.InvalidTypeException;
import static com.datastax.driver.core.ParseUtils.isLongLiteral;
import static com.datastax.driver.core.ParseUtils.quote;
/**
*
* {@link TypeCodec} that maps
* {@link ZonedDateTime} <-> tuple<timestamp,varchar>
* providing a pattern for maintaining timezone information in
* Cassandra.
*
*
* Since Cassandra's timestamp
type preserves only
* milliseconds since epoch, any timezone information
* would normally be lost. By using a
* tuple<timestamp,varchar>
a timezone ID can be
* persisted in the varchar
field such that when the
* value is deserialized the timezone is
* preserved.
*/
public class ZonedDateTimeCodec extends TypeCodec.AbstractTupleCodec {
/**
* A {@link DateTimeFormatter} that parses (most) of
* the ISO formats accepted in CQL.
*/
private static final DateTimeFormatter FORMATTER = new DateTimeFormatterBuilder()
.parseCaseSensitive()
.parseStrict()
.append(DateTimeFormatter.ISO_LOCAL_DATE)
.optionalStart()
.appendLiteral('T')
.appendValue(HOUR_OF_DAY, 2)
.appendLiteral(':')
.appendValue(MINUTE_OF_HOUR, 2)
.optionalEnd()
.optionalStart()
.appendLiteral(':')
.appendValue(SECOND_OF_MINUTE, 2)
.optionalEnd()
.optionalStart()
.appendFraction(NANO_OF_SECOND, 0, 9, true)
.optionalEnd()
.optionalStart()
.appendZoneOrOffsetId()
.optionalEnd()
.toFormatter()
.withZone(ZoneOffset.UTC);
private static final DateTimeFormatter ZONE_FORMATTER = DateTimeFormatter.ofPattern("xxx");
public ZonedDateTimeCodec(TupleType tupleType) {
super(tupleType, ZonedDateTime.class);
List types = tupleType.getComponentTypes();
checkArgument(
types.size() == 2 && types.get(0).equals(DataType.timestamp()) && types.get(1).equals(DataType.varchar()),
"Expected tuple, got %s",
tupleType);
}
@Override
protected ZonedDateTime newInstance() {
return null;
}
@Override
protected ByteBuffer serializeField(ZonedDateTime source, int index, ProtocolVersion protocolVersion) {
if(index == 0) {
long millis = source.toInstant().toEpochMilli();
return bigint().serializeNoBoxing(millis, protocolVersion);
}
if(index == 1) {
return varchar().serialize(ZONE_FORMATTER.format(source.getOffset()), protocolVersion);
}
throw new IndexOutOfBoundsException("Tuple index out of bounds. "+ index);
}
@Override
protected ZonedDateTime deserializeAndSetField(ByteBuffer input, ZonedDateTime target, int index, ProtocolVersion protocolVersion) {
if(index == 0) {
long millis = bigint().deserializeNoBoxing(input, protocolVersion);
return Instant.ofEpochMilli(millis).atZone(ZoneOffset.UTC);
}
if(index == 1) {
String zoneId = varchar().deserialize(input, protocolVersion);
return target.withZoneSameInstant(ZoneId.of(zoneId));
}
throw new IndexOutOfBoundsException("Tuple index out of bounds. "+ index);
}
@Override
protected String formatField(ZonedDateTime value, int index) {
if(index == 0) {
return quote(FORMATTER.format(value));
}
if(index == 1) {
return quote(ZONE_FORMATTER.format(value.getOffset()));
}
throw new IndexOutOfBoundsException("Tuple index out of bounds. "+ index);
}
@Override
protected ZonedDateTime parseAndSetField(String input, ZonedDateTime target, int index) {
if(index == 0) {
// strip enclosing single quotes, if any
if (ParseUtils.isQuoted(input))
input = ParseUtils.unquote(input);
if (isLongLiteral(input)) {
try {
long millis = Long.parseLong(input);
return ZonedDateTime.ofInstant(Instant.ofEpochMilli(millis), ZoneOffset.UTC);
} catch (NumberFormatException e) {
throw new InvalidTypeException(String.format("Cannot parse timestamp value from \"%s\"", input));
}
}
try {
return ZonedDateTime.from(FORMATTER.parse(input));
} catch (DateTimeParseException e) {
throw new InvalidTypeException(String.format("Cannot parse timestamp value from \"%s\"", target));
}
}
if(index == 1) {
String zoneId = varchar().parse(input);
return target.withZoneSameInstant(ZoneId.of(zoneId));
}
throw new IndexOutOfBoundsException("Tuple index out of bounds. "+ index);
}
}