Skip to content
This repository was archived by the owner on Oct 20, 2022. It is now read-only.

Commit dd9e139

Browse files
Fix bug: SQL data source dates are shifted (Issue 11).
1 parent 844f760 commit dd9e139

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/main/java/com/google/visualization/datasource/util/SqlDataSourceHelper.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ private static TableCell buildTableCell(ResultSet rs, ValueType valueType,
706706
// Use the 'set' method with those parameters, and not the 'setTime'
707707
// method with the date parameter, since the Date object contains the
708708
// current time zone and it's impossible to change it to 'GMT'.
709-
gc.setTime(date);
709+
gc.set(date.getYear() + 1900, date.getMonth(), date.getDate());
710710
value = new DateValue(gc);
711711
}
712712
break;
@@ -716,7 +716,14 @@ private static TableCell buildTableCell(ResultSet rs, ValueType valueType,
716716
if (timestamp != null) {
717717
GregorianCalendar gc =
718718
new GregorianCalendar(TimeZone.getTimeZone("GMT"));
719-
gc.setTime(timestamp);
719+
// Set the year, month, date, hours, minutes and seconds in the
720+
// gregorian calendar. Use the 'set' method with those parameters,
721+
// and not the 'setTime' method with the timestamp parameter, since
722+
// the Timestamp object contains the current time zone and it's
723+
// impossible to change it to 'GMT'.
724+
gc.set(timestamp.getYear() + 1900, timestamp.getMonth(),
725+
timestamp.getDate(), timestamp.getHours(), timestamp.getMinutes(),
726+
timestamp.getSeconds());
720727
// Set the milliseconds explicitly, as they are not saved in the
721728
// underlying date.
722729
gc.set(Calendar.MILLISECOND, timestamp.getNanos() / 1000000);
@@ -729,7 +736,18 @@ private static TableCell buildTableCell(ResultSet rs, ValueType valueType,
729736
if (time != null) {
730737
GregorianCalendar gc =
731738
new GregorianCalendar(TimeZone.getTimeZone("GMT"));
732-
gc.setTime(time);
739+
// Set the hours, minutes and seconds of the time in the gregorian
740+
// calendar. Set the year, month and date to be January 1 1970 like
741+
// in the Time object.
742+
// Use the 'set' method with those parameters,
743+
// and not the 'setTime' method with the time parameter, since
744+
// the Time object contains the current time zone and it's
745+
// impossible to change it to 'GMT'.
746+
gc.set(1970, Calendar.JANUARY, 1, time.getHours(), time.getMinutes(),
747+
time.getSeconds());
748+
// Set the milliseconds explicitly, otherwise the milliseconds from
749+
// the time the gc was initialized are used.
750+
gc.set(GregorianCalendar.MILLISECOND, 0);
733751
value = new TimeOfDayValue(gc);
734752
}
735753
break;

0 commit comments

Comments
 (0)