Skip to content

Commit

Permalink
improved iso8601 dateformat support.
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Aug 16, 2017
1 parent 3e89092 commit f8e77b1
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 19 deletions.
72 changes: 57 additions & 15 deletions src/main/java/com/alibaba/fastjson/parser/JSONScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,39 @@ public boolean scanISO8601DateIfMatch(boolean strict) {
return false;
}

char y0 = charAt(bp);
char y1 = charAt(bp + 1);
char y2 = charAt(bp + 2);
char y3 = charAt(bp + 3);
char M0 = charAt(bp + 4);
char M1 = charAt(bp + 5);
char d0 = charAt(bp + 6);
char d1 = charAt(bp + 7);
char y0, y1, y2, y3, M0, M1, d0, d1;

char c0 = charAt(bp);
char c1 = charAt(bp + 1);
char c2 = charAt(bp + 2);
char c3 = charAt(bp + 3);
char c4 = charAt(bp + 4);
char c5 = charAt(bp + 5);
char c6 = charAt(bp + 6);
char c7 = charAt(bp + 7);
char c8 = charAt(bp + 8);

final boolean sperate17 = c4 == '-' && c7 == '-' && rest == 17;
if (sperate17) {
y0 = c0;
y1 = c1;
y2 = c2;
y3 = c3;
M0 = c5;
M1 = c6;
d0 = c8;
d1 = charAt(bp + 9);
} else {
y0 = c0;
y1 = c1;
y2 = c2;
y3 = c3;
M0 = c4;
M1 = c5;
d0 = c6;
d1 = c7;
}


if (!checkDate(y0, y1, y2, y3, M0, M1, d0, d1)) {
return false;
Expand All @@ -263,18 +288,35 @@ public boolean scanISO8601DateIfMatch(boolean strict) {

int hour, minute, seconds, millis;
if (rest != 8) {
char h0 = charAt(bp + 8);
char h1 = charAt(bp + 9);
char m0 = charAt(bp + 10);
char m1 = charAt(bp + 11);
char s0 = charAt(bp + 12);
char s1 = charAt(bp + 13);
char c9 = charAt(bp + 9);
char c10 = charAt(bp + 10);
char c11 = charAt(bp + 11);
char c12 = charAt(bp + 12);
char c13 = charAt(bp + 13);

char h0, h1, m0, m1, s0, s1;

if (sperate17 && c10 == 'T' && c13 == ':' && charAt(bp + 16) == 'Z') {
h0 = c11;
h1 = c12;
m0 = charAt(bp + 14);
m1 = charAt(bp + 15);
s0 = '0';
s1 = '0';
} else {
h0 = c8;
h1 = c9;
m0 = c10;
m1 = c11;
s0 = c12;
s1 = c13;
}

if (!checkTime(h0, h1, m0, m1, s0, s1)) {
return false;
}

if (rest == 17) {
if (rest == 17 && !sperate17) {
char S0 = charAt(bp + 14);
char S1 = charAt(bp + 15);
char S2 = charAt(bp + 16);
Expand Down
14 changes: 10 additions & 4 deletions src/test/java/com/alibaba/json/bvt/date/DateFieldTest10.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,25 @@ public class DateFieldTest10 extends TestCase {
public void test_for_zero() throws Exception {
String text = "{\"date\":\"0000-00-00\"}";

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-DD");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Object object = format.parse("0000-00-00");
JSON.parseObject(text, Model.class);
}

public void test_1() throws Exception {
String text = "{\"date\":\"2017-08-14 19:05:30.000|America/Los_Angeles\"}";

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-DD");
Object object = format.parse("0000-00-00");
JSON.parseObject(text, Model.class);
}

public void test_2() throws Exception {
String text = "{\"date\":\"2017-08-16T04:29Z\"}";
Model model = JSON.parseObject(text, Model.class);

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Object object = format.parse("2017-08-16 04:29");
assertEquals(object, model.date);
}

public static class Model {
public Date date;
}
Expand Down

0 comments on commit f8e77b1

Please sign in to comment.