JSON datetime formatting needs timezone (backported from trunk) patch by Alex Petrov; reviewed by Stefania Alborghetti for CASSANDRA-11137
Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/88f22b96 Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/88f22b96 Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/88f22b96 Branch: refs/heads/trunk Commit: 88f22b9692c6fdddf837556f13140d949afe0d28 Parents: e5c4027 Author: Alex Petrov <oleksandr.pet...@gmail.com> Authored: Thu Apr 28 08:59:24 2016 +0800 Committer: Stefania Alborghetti <stefania.alborghe...@datastax.com> Committed: Thu Apr 28 09:10:18 2016 +0800 ---------------------------------------------------------------------- CHANGES.txt | 1 + NEWS.txt | 8 ++++++++ .../cassandra/serializers/TimestampSerializer.java | 11 +++++++---- .../cassandra/cql3/validation/entities/JsonTest.java | 6 ++++-- 4 files changed, 20 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cassandra/blob/88f22b96/CHANGES.txt ---------------------------------------------------------------------- diff --git a/CHANGES.txt b/CHANGES.txt index 3641816..91179b3 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,4 +1,5 @@ 2.2.7 + * JSON datetime formatting needs timezone (CASSANDRA-11137) * Fix is_dense recalculation for Thrift-updated tables (CASSANDRA-11502) * Remove unnescessary file existence check during anticompaction (CASSANDRA-11660) * Add missing files to debian packages (CASSANDRA-11642) http://git-wip-us.apache.org/repos/asf/cassandra/blob/88f22b96/NEWS.txt ---------------------------------------------------------------------- diff --git a/NEWS.txt b/NEWS.txt index e8f4e66..a3ba0dd 100644 --- a/NEWS.txt +++ b/NEWS.txt @@ -13,6 +13,14 @@ restore snapshots created with the previous major version using the 'sstableloader' tool. You can upgrade the file format of your snapshots using the provided 'sstableupgrade' tool. +2.2.7 +===== + +New features +------------ + - JSON timestamps are now in UTC and contain the timezone information, see + CASSANDRA-11137 for more details. + 2.2.6 ===== http://git-wip-us.apache.org/repos/asf/cassandra/blob/88f22b96/src/java/org/apache/cassandra/serializers/TimestampSerializer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/serializers/TimestampSerializer.java b/src/java/org/apache/cassandra/serializers/TimestampSerializer.java index 78ee7e7..77a5df9 100644 --- a/src/java/org/apache/cassandra/serializers/TimestampSerializer.java +++ b/src/java/org/apache/cassandra/serializers/TimestampSerializer.java @@ -22,7 +22,7 @@ import org.apache.cassandra.utils.ByteBufferUtil; import java.nio.ByteBuffer; import java.text.SimpleDateFormat; import java.text.ParseException; -import java.util.Date; +import java.util.*; import java.util.regex.Pattern; import org.apache.commons.lang3.time.DateUtils; @@ -48,11 +48,11 @@ public class TimestampSerializer implements TypeSerializer<Date> "yyyy-MM-dd HH:mm:ssX", "yyyy-MM-dd HH:mm:ssXX", "yyyy-MM-dd HH:mm:ssXXX", - "yyyy-MM-dd HH:mm:ss.SSS", // TO_JSON_FORMAT + "yyyy-MM-dd HH:mm:ss.SSS", "yyyy-MM-dd HH:mm:ss.SSS z", "yyyy-MM-dd HH:mm:ss.SSS zz", "yyyy-MM-dd HH:mm:ss.SSS zzz", - "yyyy-MM-dd HH:mm:ss.SSSX", + "yyyy-MM-dd HH:mm:ss.SSSX", // TO_JSON_FORMAT "yyyy-MM-dd HH:mm:ss.SSSXX", "yyyy-MM-dd HH:mm:ss.SSSXXX", "yyyy-MM-dd'T'HH:mm", @@ -96,11 +96,14 @@ public class TimestampSerializer implements TypeSerializer<Date> } }; + private static final String TO_JSON_FORMAT = dateStringPatterns[19]; private static final ThreadLocal<SimpleDateFormat> FORMATTER_TO_JSON = new ThreadLocal<SimpleDateFormat>() { protected SimpleDateFormat initialValue() { - return new SimpleDateFormat(dateStringPatterns[15]); + SimpleDateFormat sdf = new SimpleDateFormat(TO_JSON_FORMAT); + sdf.setTimeZone(TimeZone.getTimeZone("UTC")); + return sdf; } }; http://git-wip-us.apache.org/repos/asf/cassandra/blob/88f22b96/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java ---------------------------------------------------------------------- diff --git a/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java b/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java index 2c471b0..824d436 100644 --- a/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java +++ b/test/unit/org/apache/cassandra/cql3/validation/entities/JsonTest.java @@ -618,8 +618,10 @@ public class JsonTest extends CQLTester assertRows(execute("SELECT k, toJson(timeval) FROM %s WHERE k = ?", 0), row(0, "\"00:00:00.000000123\"")); // ================ timestamp ================ - execute("INSERT INTO %s (k, timestampval) VALUES (?, ?)", 0, new SimpleDateFormat("y-M-d").parse("2014-01-01")); - assertRows(execute("SELECT k, toJson(timestampval) FROM %s WHERE k = ?", 0), row(0, "\"2014-01-01 00:00:00.000\"")); + SimpleDateFormat sdf = new SimpleDateFormat("y-M-d"); + sdf.setTimeZone(TimeZone.getTimeZone("UDT")); + execute("INSERT INTO %s (k, timestampval) VALUES (?, ?)", 0, sdf.parse("2014-01-01")); + assertRows(execute("SELECT k, toJson(timestampval) FROM %s WHERE k = ?", 0), row(0, "\"2014-01-01 00:00:00.000Z\"")); // ================ timeuuid ================ execute("INSERT INTO %s (k, timeuuidval) VALUES (?, ?)", 0, UUID.fromString("6bddc89a-5644-11e4-97fc-56847afe9799"));