-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Fix DateFormat time zone is not restored and add Test. #2549
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
46942b6
Fix DateFormat time zone is not restored and add Test.
6ad06f9
delete the test and make sure mvn clean test -X can be SUCCESS
fb8c463
delete the test and make sure mvn clean test -X can be SUCCESS
fb850a5
add test
7928fe4
restore the DateFormat time zone in SqlDateTypeAdapter and SqlTimeTyp…
3da89da
Merge branch 'main' into main
Carpe-Wang db4ff5e
Adjust the code according to the code review feedback.
7b8e91a
Adjust the code according to the code review feedback.
1687fb1
Adjust the code according to the code review feedback.
23bbbca
Adjust the Test
3b7c7d0
fix Werror error
a8a13e7
Adjust the DefaultDateTypeAdapterTest according to the code review fe…
49f0caf
Merge branch 'main' into main
Carpe-Wang edc10e1
Merge branch 'main' into main
Carpe-Wang 72cc078
Merge branch 'main' into main
Carpe-Wang d3767a8
Merge branch 'main' into main
Carpe-Wang 972900a
Merge branch 'main' into main
Carpe-Wang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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; | ||||||
|
@@ -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); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use Truth for assertions here:
Suggested change
|
||||||
|
||||||
// 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) { | ||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 thesynchronized (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 thetry
; I guess that should be fine as well, e.g.:There was a problem hiding this comment.
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.