Skip to content

Commit

Permalink
bug fixed for issue 331. alibaba#331
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Apr 13, 2016
1 parent 9f3d140 commit 8d74f9e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 25 deletions.
58 changes: 40 additions & 18 deletions src/main/java/com/alibaba/fastjson/parser/JSONScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,20 @@ public boolean scanISO8601DateIfMatch(boolean strict) {

token = JSONToken.LITERAL_ISO8601_DATE;
return true;
} else if (t == '+' || t == '-') {
if (len == 16) {
if (charAt(bp + 13) != ':' //
|| charAt(bp + 14) != '0' //
|| charAt(bp + 15) != '0') {
return false;
}

setTime('0', '0', '0', '0', '0', '0');
calendar.set(Calendar.MILLISECOND, 0);
setTimeZone(t, charAt(bp + 11), charAt(bp + 12));
return true;
}
return false;
} else {
return false;
}
Expand All @@ -333,12 +347,7 @@ public boolean scanISO8601DateIfMatch(boolean strict) {
return false;
}

int hour = digits[h0] * 10 + digits[h1];
int minute = digits[m0] * 10 + digits[m1];
int seconds = digits[s0] * 10 + digits[s1];
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, seconds);
setTime(h0, h1, m0, m1, s0, s1);

char dot = charAt(bp + 19);
if (dot == '.') {
Expand Down Expand Up @@ -426,18 +435,7 @@ public boolean scanISO8601DateIfMatch(boolean strict) {
timzeZoneLength = 3;
}

int timeZoneOffset = (digits[t0] * 10 + digits[t1]) * 3600 * 1000;
if (timeZoneFlag == '-') {
timeZoneOffset = -timeZoneOffset;
}

if (calendar.getTimeZone().getRawOffset() != timeZoneOffset) {
String[] timeZoneIDs = TimeZone.getAvailableIDs(timeZoneOffset);
if (timeZoneIDs.length > 0) {
TimeZone timeZone = TimeZone.getTimeZone(timeZoneIDs[0]);
calendar.setTimeZone(timeZone);
}
}
setTimeZone(timeZoneFlag, t0, t1);

} else if (timeZoneFlag == 'Z') {// UTC
timzeZoneLength = 1;
Expand All @@ -460,6 +458,30 @@ public boolean scanISO8601DateIfMatch(boolean strict) {
return true;
}

protected void setTime(char h0, char h1, char m0, char m1, char s0, char s1) {
int hour = digits[h0] * 10 + digits[h1];
int minute = digits[m0] * 10 + digits[m1];
int seconds = digits[s0] * 10 + digits[s1];
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, seconds);
}

protected void setTimeZone(char timeZoneFlag, char t0, char t1) {
int timeZoneOffset = (digits[t0] * 10 + digits[t1]) * 3600 * 1000;
if (timeZoneFlag == '-') {
timeZoneOffset = -timeZoneOffset;
}

if (calendar.getTimeZone().getRawOffset() != timeZoneOffset) {
String[] timeZoneIDs = TimeZone.getAvailableIDs(timeZoneOffset);
if (timeZoneIDs.length > 0) {
TimeZone timeZone = TimeZone.getTimeZone(timeZoneIDs[0]);
calendar.setTimeZone(timeZone);
}
}
}

private boolean checkTime(char h0, char h1, char m0, char m1, char s0, char s1) {
if (h0 == '0') {
if (h1 < '0' || h1 > '9') {
Expand Down
22 changes: 15 additions & 7 deletions src/main/java/com/alibaba/fastjson/serializer/DateCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,22 @@ protected <T> T cast(DefaultJSONParser parser, Type clazz, Object fieldName, Obj
} finally {
dateLexer.close();
}

DateFormat dateFormat = parser.getDateFormat();
try {
return (T) dateFormat.parse(strVal);
} catch (ParseException e) {
// skip

if (strVal.length() == parser.getDateFomartPattern().length()) {
DateFormat dateFormat = parser.getDateFormat();
try {
return (T) dateFormat.parse(strVal);
} catch (ParseException e) {
// skip
}
}


// JSONScanner iso8601Lexer = new JSONScanner(strVal);
// if (iso8601Lexer.scanISO8601DateIfMatch()) {
// val = iso8601Lexer.getCalendar().getTime();
// }
// iso8601Lexer.close();
//
long longVal = Long.parseLong(strVal);
return (T) new java.util.Date(longVal);
}
Expand Down

0 comments on commit 8d74f9e

Please sign in to comment.