Elffuss BraidHub

kikocis/java-driver public

JAVA-1013: TimestampCodec should format dates in human-readable format.
Changesetc3c0ee4ca18bfc4192755876d9ac23ba03bc522e6eabbfbc5f9508a0bcdda6f8
AuthorAlexandre Dutra <alexandre.dutra@datastax.com> · 2015-12-08 15:34 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2015-12-18 11:09 +01:00
Parents256adfcff3e2

Browse the files at this changeset

6 files changed

modified changelog/README.md +1 −0
@@ -3,6 +3,7 @@
33 ### 3.0.0-rc1 (in progress)
44
55 - [bug] JAVA-890: fix mapper for case-sensitive UDT.
6+- [improvement] JAVA-1013: TimestampCodec should format dates in human-readable format.
67
78
89 ### 3.0.0-beta1
modified driver-core/src/main/java/com/datastax/driver/core/ParseUtils.java +14 −0
@@ -324,6 +324,20 @@
324324 throw new ParseException("Unable to parse the date: " + str, -1);
325325 }
326326
327+ /**
328+ * Formats the given date, using the given time zone and the given pattern.
329+ *
330+ * @param date The date to format.
331+ * @param timeZone The time zone to use.
332+ * @param pattern The date pattern to use.
333+ * @return The formatted date.
334+ */
335+ public static String formatDate(Date date, TimeZone timeZone, String pattern) {
336+ SimpleDateFormat formatter = new SimpleDateFormat(pattern);
337+ formatter.setTimeZone(timeZone);
338+ return formatter.format(date);
339+ }
340+
327341 /**
328342 * Parse the given string as a time, using the following time pattern: {@code hh:mm:ss[.fffffffff]}.
329343 * <p/>
modified driver-core/src/main/java/com/datastax/driver/core/TypeCodec.java +5 −1
@@ -1405,6 +1405,10 @@
14051405
14061406 private static final TimestampCodec instance = new TimestampCodec();
14071407
1408+ private static final String PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
1409+
1410+ private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
1411+
14081412 private TimestampCodec() {
14091413 super(DataType.timestamp(), Date.class);
14101414 }
@@ -1436,7 +1440,7 @@
14361440 public String format(Date value) {
14371441 if (value == null)
14381442 return "NULL";
1439- return Long.toString(value.getTime());
1443+ return ParseUtils.quote(ParseUtils.formatDate(value, UTC, PATTERN));
14401444 }
14411445
14421446 @Override
modified driver-core/src/test/java/com/datastax/driver/core/DataTypeTest.java +11 −4
@@ -26,6 +26,8 @@
2626 import java.util.*;
2727
2828 import static com.datastax.driver.core.Assertions.assertThat;
29+import static com.datastax.driver.core.ParseUtils.formatDate;
30+import static com.datastax.driver.core.ParseUtils.quote;
2931 import static com.google.common.collect.Lists.newArrayList;
3032 import static org.testng.Assert.assertEquals;
3133 import static org.testng.Assert.fail;
@@ -39,6 +41,11 @@
3941
4042 ProtocolVersion protocolVersion = TestUtils.getDesiredProtocolVersion();
4143
44+ private static final String PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
45+
46+ private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
47+
48+
4249 static boolean exclude(DataType t) {
4350 return t.getName() == DataType.Name.COUNTER;
4451 }
@@ -85,10 +92,10 @@
8592 case TIMESTAMP:
8693 // input: single quotes are optional for long literals, mandatory for date patterns
8794 return new TestValue[]{
88- new TestValue(new Date(42L), "42", "42"),
89- new TestValue(new Date(91294377723L), "91294377723", "91294377723"),
90- new TestValue(new Date(-133L), "-133", "-133"),
91- new TestValue(new Date(784041330999L), "'1994-11-05T14:15:30.999+0100'", "784041330999"),
95+ new TestValue(new Date(42L), "42", quote(formatDate(new Date(42L), UTC, PATTERN))),
96+ new TestValue(new Date(91294377723L), "91294377723", quote(formatDate(new Date(91294377723L), UTC, PATTERN))),
97+ new TestValue(new Date(-133L), "-133", quote(formatDate(new Date(-133L), UTC, PATTERN))),
98+ new TestValue(new Date(784041330999L), "'1994-11-05T14:15:30.999+0100'", quote(formatDate(new Date(784041330999L), UTC, PATTERN))),
9299 new TestValue(null, null, "NULL"), new TestValue(null, "null", "NULL"), new TestValue(null, "NULL", "NULL")};
93100 case DATE:
94101 // input: single quotes are optional for long literals, mandatory for date patterns
modified driver-core/src/test/java/com/datastax/driver/core/TypeCodecTest.java +19 −0
@@ -26,6 +26,7 @@
2626 import org.testng.annotations.Test;
2727
2828 import java.nio.ByteBuffer;
29+import java.text.ParseException;
2930 import java.util.*;
3031
3132 import static com.datastax.driver.core.Assertions.assertThat;
@@ -252,6 +253,24 @@
252253 .cannotFormat(type4UUID);
253254 }
254255
256+ @Test(groups = "unit")
257+ public void should_format_timestamp_with_iso8601_pattern() throws ParseException {
258+ // given
259+ // 2015 Dec 7th, 17:53:46.999, at UTC-7
260+ GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT-7:00"));
261+ cal.set(Calendar.YEAR, 2015);
262+ cal.set(Calendar.MONTH, 11);
263+ cal.set(Calendar.DAY_OF_MONTH, 7);
264+ cal.set(Calendar.HOUR_OF_DAY, 17);
265+ cal.set(Calendar.MINUTE, 53);
266+ cal.set(Calendar.SECOND, 46);
267+ cal.set(Calendar.MILLISECOND, 999);
268+ Date date = cal.getTime();
269+ // when
270+ String formatted = TypeCodec.timestamp().format(date);
271+ // then
272+ assertThat(formatted).isEqualTo("'2015-12-08T00:53:46.999+0000'");
273+ }
255274
256275 private class ListVarcharToListListInteger extends TypeCodec<List<List<Integer>>> {
257276
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +1 −1
@@ -121,7 +121,7 @@
121121
122122 Date date = new Date();
123123 date.setTime(1234325);
124- query = "SELECT * FROM foo WHERE d=1234325;";
124+ query = "SELECT * FROM foo WHERE d='1970-01-01T00:20:34.325+0000';";
125125 select = select().all().from("foo").where(eq("d", date));
126126 assertEquals(select.toString(), query);
127127