Skip to content

Commit

Permalink
Fix DateFormat time zone is not restored and add Test. (#2549)
Browse files Browse the repository at this point in the history
* Fix DateFormat time zone is not restored and add Test.

* delete the test and make sure mvn clean test -X can be SUCCESS

* delete the test and make sure mvn clean test -X can be SUCCESS

* add test

* restore the DateFormat time zone in SqlDateTypeAdapter and SqlTimeTypeAdapter

* Adjust the code according to the code review feedback.

* Adjust the code according to the code review feedback.

* Adjust the code according to the code review feedback.

* Adjust the Test

* fix Werror error

* Adjust the DefaultDateTypeAdapterTest according to the code review feedback.

---------

Co-authored-by: Carpe-Wang <wangcarpe@126.com>
  • Loading branch information
Carpe-Wang and Carpe-Wang committed Dec 5, 2023
1 parent 5471932 commit 58d1a9f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,13 @@ private Date deserializeToDate(JsonReader in) throws IOException {
// Needs to be synchronized since JDK DateFormat classes are not thread-safe
synchronized (dateFormats) {
for (DateFormat dateFormat : dateFormats) {
TimeZone originalTimeZone = dateFormat.getTimeZone();
try {
return dateFormat.parse(s);
} catch (ParseException ignored) {
// OK: try the next format
} finally {
dateFormat.setTimeZone(originalTimeZone);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
* Adapter for java.sql.Date. Although this class appears stateless, it is not. DateFormat captures
Expand Down Expand Up @@ -59,15 +60,17 @@ public java.sql.Date read(JsonReader in) throws IOException {
return null;
}
String s = in.nextString();
try {
Date utilDate;
synchronized (this) {
utilDate = format.parse(s);
synchronized (this) {
TimeZone originalTimeZone = format.getTimeZone(); // Save the original time zone
try {
Date utilDate = format.parse(s);
return new java.sql.Date(utilDate.getTime());
} catch (ParseException e) {
throw new JsonSyntaxException(
"Failed parsing '" + s + "' as SQL Date; at path " + in.getPreviousPath(), e);
} finally {
format.setTimeZone(originalTimeZone); // Restore the original time zone after parsing
}
return new java.sql.Date(utilDate.getTime());
} catch (ParseException e) {
throw new JsonSyntaxException(
"Failed parsing '" + s + "' as SQL Date; at path " + in.getPreviousPath(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
* Adapter for java.sql.Time. Although this class appears stateless, it is not. DateFormat captures
Expand Down Expand Up @@ -60,14 +61,17 @@ public Time read(JsonReader in) throws IOException {
return null;
}
String s = in.nextString();
try {
synchronized (this) {
synchronized (this) {
TimeZone originalTimeZone = format.getTimeZone(); // Save the original time zone
try {
Date date = format.parse(s);
return new Time(date.getTime());
} catch (ParseException e) {
throw new JsonSyntaxException(
"Failed parsing '" + s + "' as SQL Time; at path " + in.getPreviousPath(), e);
} finally {
format.setTimeZone(originalTimeZone); // Restore the original time zone
}
} catch (ParseException e) {
throw new JsonSyntaxException(
"Failed parsing '" + s + "' as SQL Time; at path " + in.getPreviousPath(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.junit.Assert.fail;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.internal.bind.DefaultDateTypeAdapter.DateType;
Expand Down Expand Up @@ -223,6 +224,34 @@ public void testUnexpectedToken() throws Exception {
}
}

@Test
public void testGsonDateFormat() {
TimeZone originalTimeZone = TimeZone.getDefault();
// Set the default timezone to UTC
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
try {
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm z").create();
Date originalDate = new Date(0);

// Serialize the date object
String json = gson.toJson(originalDate);
assertThat(json).isEqualTo("\"1970-01-01 00:00 UTC\"");

// Deserialize a date string with the PST timezone
Date deserializedDate = gson.fromJson("\"1970-01-01 00:00 PST\"", Date.class);
// Assert that the deserialized date's time is correct
assertThat(deserializedDate.getTime()).isEqualTo(new Date(28800000).getTime());

// Serialize the deserialized date object again
String jsonAfterDeserialization = gson.toJson(deserializedDate);
// The expectation is that the date, after deserialization, when serialized again should still
// be in the UTC timezone
assertThat(jsonAfterDeserialization).isEqualTo("\"1970-01-01 08:00 UTC\"");
} finally {
TimeZone.setDefault(originalTimeZone);
}
}

private static TypeAdapter<Date> dateAdapter(TypeAdapterFactory adapterFactory) {
TypeAdapter<Date> adapter = adapterFactory.create(new Gson(), TypeToken.get(Date.class));
assertThat(adapter).isNotNull();
Expand Down

0 comments on commit 58d1a9f

Please sign in to comment.