com.datastax.driver.extras.codecs.joda.DateTimeCodec Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cassandra-driver-extras Show documentation
Show all versions of cassandra-driver-extras Show documentation
Extended functionality for the Java driver.
/*
* 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.joda;
import com.datastax.driver.core.*;
import com.datastax.driver.core.exceptions.InvalidTypeException;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder;
import org.joda.time.format.ISODateTimeFormat;
import java.nio.ByteBuffer;
import java.util.List;
import static com.datastax.driver.core.ParseUtils.isLongLiteral;
import static com.datastax.driver.core.ParseUtils.quote;
import static com.google.common.base.Preconditions.checkArgument;
import static org.joda.time.DateTimeZone.UTC;
/**
* {@link TypeCodec} that maps
* {@link DateTime} to CQL {@code tuple},
* 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.
*
* IMPORTANT: this codec's {@link #format(Object) format} method formats
* timestamps using an ISO-8601 format that includes nanoseconds.
* This format is incompatible with Cassandra versions < 2.0.9.
*
* @see 'Working with timestamps' section of CQL specification
*/
public class DateTimeCodec extends TypeCodec.AbstractTupleCodec {
/**
* A {@link DateTimeFormatter} that parses (most) of
* the ISO formats accepted in CQL.
*/
private static final DateTimeFormatter FORMATTER = new DateTimeFormatterBuilder()
.append(ISODateTimeFormat.dateOptionalTimeParser().getParser())
.appendOptional(
new DateTimeFormatterBuilder()
.appendTimeZoneOffset("Z", true, 2, 4)
.toParser())
.toFormatter()
.withZoneUTC();
private static final DateTimeFormatter ZONE_FORMATTER = DateTimeFormat.forPattern("ZZ");
public DateTimeCodec(TupleType tupleType) {
super(tupleType, DateTime.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 DateTime newInstance() {
return null;
}
@Override
protected ByteBuffer serializeField(DateTime source, int index, ProtocolVersion protocolVersion) {
if (index == 0) {
long millis = source.getMillis();
return bigint().serializeNoBoxing(millis, protocolVersion);
}
if (index == 1) {
return varchar().serialize(ZONE_FORMATTER.print(source), protocolVersion);
}
throw new IndexOutOfBoundsException("Tuple index out of bounds. " + index);
}
@Override
protected DateTime deserializeAndSetField(ByteBuffer input, DateTime target, int index, ProtocolVersion protocolVersion) {
if (index == 0) {
long millis = bigint().deserializeNoBoxing(input, protocolVersion);
return new DateTime(millis);
}
if (index == 1) {
String zoneId = varchar().deserialize(input, protocolVersion);
return target.withZone(DateTimeZone.forID(zoneId));
}
throw new IndexOutOfBoundsException("Tuple index out of bounds. " + index);
}
@Override
protected String formatField(DateTime value, int index) {
if (index == 0) {
return quote(value.withZone(UTC).toString());
}
if (index == 1) {
return quote(ZONE_FORMATTER.print(value));
}
throw new IndexOutOfBoundsException("Tuple index out of bounds. " + index);
}
@Override
protected DateTime parseAndSetField(String input, DateTime 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 new DateTime(millis);
} catch (NumberFormatException e) {
throw new InvalidTypeException(String.format("Cannot parse timestamp value from \"%s\"", input));
}
}
try {
return FORMATTER.parseDateTime(input);
} catch (RuntimeException e) {
throw new InvalidTypeException(String.format("Cannot parse timestamp value from \"%s\"", target));
}
}
if (index == 1) {
String zoneId = varchar().parse(input);
return target.withZone(DateTimeZone.forID(zoneId));
}
throw new IndexOutOfBoundsException("Tuple index out of bounds. " + index);
}
}