Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DateFormat time zone is not restored and add Test. #2549

Merged
merged 17 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,18 @@ public java.sql.Date read(JsonReader in) throws IOException {
return null;
}
String s = in.nextString();
TimeZone originalTimeZone = format.getTimeZone(); // Save the original time zone
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this and the setTimeZone call inside the synchronized (this) { ... } block. Type adapters must be thread-safe and with the current implementation there could be a race condition.

Or rewrite this so that the synchronized block covers the try; I guess that should be fine as well, e.g.:

    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
      }
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is my mistake; I did not carefully read the code review you provided at the beginning.

try {
Date utilDate;
synchronized (this) {
TimeZone originalTimeZone = format.getTimeZone(); // Save the original time zone
utilDate = format.parse(s);
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);
} finally {
format.setTimeZone(originalTimeZone); // Restore the original time zone after parsing
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,17 @@ public Time read(JsonReader in) throws IOException {
return null;
}
String s = in.nextString();
TimeZone originalTimeZone = format.getTimeZone(); // Save the original time zone
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as #2549 (comment)

try {
synchronized (this) {
TimeZone originalTimeZone = format.getTimeZone(); // Save the original time zone
Date date = format.parse(s);
format.setTimeZone(originalTimeZone); // Restore the original time zone
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
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static org.junit.Assert.assertEquals;
import static junit.framework.TestCase.assertEquals;
import static org.junit.Assert.fail;

import com.google.gson.Gson;
Expand Down Expand Up @@ -227,23 +227,28 @@ public void testUnexpectedToken() throws Exception {

@Test
public void testGsonDateFormat() {
TimeZone originalTimeZone = TimeZone.getDefault();
// Set the default timezone to UTC
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm z").create();
Date originalDate = new Date(0);
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);
assertEquals("\"1970-01-01 00:00 UTC\"", json);
// Serialize the date object
String json = gson.toJson(originalDate);
assertEquals("\"1970-01-01 00:00 UTC\"", json);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use Truth for assertions here:

Suggested change
assertEquals("\"1970-01-01 00:00 UTC\"", json);
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);
// Deserialize a date string with the PST timezone
Date deserializedDate = gson.fromJson("\"1970-01-01 00:00 PST\"", Date.class);

// 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
assertEquals("\"1970-01-01 08:00 UTC\"", jsonAfterDeserialization);
// 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
assertEquals("\"1970-01-01 08:00 UTC\"", jsonAfterDeserialization);
} finally {
TimeZone.setDefault(originalTimeZone);
}
}

private static TypeAdapter<Date> dateAdapter(TypeAdapterFactory adapterFactory) {
Expand Down