@Test public void testDateWithShortYear() throws ParseException { String date = "Thu, 01 Jan 15 00:00:00 GMT"; long actual = HttpDate.parse(date.getBytes(ISO_8859_1), 0); LocalDate expected = LocalDate.of(2015, JANUARY, 1); assertEquals(expected.atStartOfDay().toInstant(UTC).getEpochSecond(), actual); }
static long parse(byte[] bytes, int start) throws ParseException { try { int day = trimAndDecodePositiveInt(bytes, start + 5, 2); int month = -1; for (int i = 0; i < MONTHS_IN_YEAR.length; i++) { byte[] entry = MONTHS_IN_YEAR[i]; if (entry[0] == bytes[start + 8] && entry[1] == bytes[start + 9] && entry[2] == bytes[start + 10]) { month = i; } } int yearLength = '0' <= bytes[start + 12 + 2] && bytes[start + 12 + 2] <= '9' ? 4 : 2; int year = (yearLength == 2 ? 2000 : 0) + trimAndDecodePositiveInt(bytes, start + 12, yearLength); int hour = trimAndDecodePositiveInt(bytes, start + 13 + yearLength, 2); int minutes = trimAndDecodePositiveInt(bytes, start + 16 + yearLength, 2); int seconds = trimAndDecodePositiveInt(bytes, start + 19 + yearLength, 2); boolean isLeapYear = isLeap(year); int[] days = isLeapYear ? DAYS_IN_MONTH_LEAP : DAYS_IN_MONTH; year = year - 1970; int yearsLeft = year % 4; long timestamp = 0; int fy = (year - yearsLeft) / 4; for (int i = 0; i < fy; i++) { timestamp += FOUR_YEAR_SECONDS; } timestamp += yearsLeft * YEAR_SECONDS; if (yearsLeft > 2) { timestamp += DAY_SECONDS; } for (int i = 0; i < month; i++) { timestamp += DAY_SECONDS * days[i]; } for (int i = 1; i < day; i++) { timestamp += DAY_SECONDS; } timestamp += ((60 * hour + minutes) * 60) + seconds; return timestamp; } catch (RuntimeException ignored) { throw FAILED_TO_PARSE_DATE; } }
HttpDate { static long parse(byte[] bytes, int start) throws ParseException { try { int day = trimAndDecodePositiveInt(bytes, start + 5, 2); int month = -1; for (int i = 0; i < MONTHS_IN_YEAR.length; i++) { byte[] entry = MONTHS_IN_YEAR[i]; if (entry[0] == bytes[start + 8] && entry[1] == bytes[start + 9] && entry[2] == bytes[start + 10]) { month = i; } } int yearLength = '0' <= bytes[start + 12 + 2] && bytes[start + 12 + 2] <= '9' ? 4 : 2; int year = (yearLength == 2 ? 2000 : 0) + trimAndDecodePositiveInt(bytes, start + 12, yearLength); int hour = trimAndDecodePositiveInt(bytes, start + 13 + yearLength, 2); int minutes = trimAndDecodePositiveInt(bytes, start + 16 + yearLength, 2); int seconds = trimAndDecodePositiveInt(bytes, start + 19 + yearLength, 2); boolean isLeapYear = isLeap(year); int[] days = isLeapYear ? DAYS_IN_MONTH_LEAP : DAYS_IN_MONTH; year = year - 1970; int yearsLeft = year % 4; long timestamp = 0; int fy = (year - yearsLeft) / 4; for (int i = 0; i < fy; i++) { timestamp += FOUR_YEAR_SECONDS; } timestamp += yearsLeft * YEAR_SECONDS; if (yearsLeft > 2) { timestamp += DAY_SECONDS; } for (int i = 0; i < month; i++) { timestamp += DAY_SECONDS * days[i]; } for (int i = 1; i < day; i++) { timestamp += DAY_SECONDS; } timestamp += ((60 * hour + minutes) * 60) + seconds; return timestamp; } catch (RuntimeException ignored) { throw FAILED_TO_PARSE_DATE; } } }
HttpDate { static long parse(byte[] bytes, int start) throws ParseException { try { int day = trimAndDecodePositiveInt(bytes, start + 5, 2); int month = -1; for (int i = 0; i < MONTHS_IN_YEAR.length; i++) { byte[] entry = MONTHS_IN_YEAR[i]; if (entry[0] == bytes[start + 8] && entry[1] == bytes[start + 9] && entry[2] == bytes[start + 10]) { month = i; } } int yearLength = '0' <= bytes[start + 12 + 2] && bytes[start + 12 + 2] <= '9' ? 4 : 2; int year = (yearLength == 2 ? 2000 : 0) + trimAndDecodePositiveInt(bytes, start + 12, yearLength); int hour = trimAndDecodePositiveInt(bytes, start + 13 + yearLength, 2); int minutes = trimAndDecodePositiveInt(bytes, start + 16 + yearLength, 2); int seconds = trimAndDecodePositiveInt(bytes, start + 19 + yearLength, 2); boolean isLeapYear = isLeap(year); int[] days = isLeapYear ? DAYS_IN_MONTH_LEAP : DAYS_IN_MONTH; year = year - 1970; int yearsLeft = year % 4; long timestamp = 0; int fy = (year - yearsLeft) / 4; for (int i = 0; i < fy; i++) { timestamp += FOUR_YEAR_SECONDS; } timestamp += yearsLeft * YEAR_SECONDS; if (yearsLeft > 2) { timestamp += DAY_SECONDS; } for (int i = 0; i < month; i++) { timestamp += DAY_SECONDS * days[i]; } for (int i = 1; i < day; i++) { timestamp += DAY_SECONDS; } timestamp += ((60 * hour + minutes) * 60) + seconds; return timestamp; } catch (RuntimeException ignored) { throw FAILED_TO_PARSE_DATE; } }  }
HttpDate { static long parse(byte[] bytes, int start) throws ParseException { try { int day = trimAndDecodePositiveInt(bytes, start + 5, 2); int month = -1; for (int i = 0; i < MONTHS_IN_YEAR.length; i++) { byte[] entry = MONTHS_IN_YEAR[i]; if (entry[0] == bytes[start + 8] && entry[1] == bytes[start + 9] && entry[2] == bytes[start + 10]) { month = i; } } int yearLength = '0' <= bytes[start + 12 + 2] && bytes[start + 12 + 2] <= '9' ? 4 : 2; int year = (yearLength == 2 ? 2000 : 0) + trimAndDecodePositiveInt(bytes, start + 12, yearLength); int hour = trimAndDecodePositiveInt(bytes, start + 13 + yearLength, 2); int minutes = trimAndDecodePositiveInt(bytes, start + 16 + yearLength, 2); int seconds = trimAndDecodePositiveInt(bytes, start + 19 + yearLength, 2); boolean isLeapYear = isLeap(year); int[] days = isLeapYear ? DAYS_IN_MONTH_LEAP : DAYS_IN_MONTH; year = year - 1970; int yearsLeft = year % 4; long timestamp = 0; int fy = (year - yearsLeft) / 4; for (int i = 0; i < fy; i++) { timestamp += FOUR_YEAR_SECONDS; } timestamp += yearsLeft * YEAR_SECONDS; if (yearsLeft > 2) { timestamp += DAY_SECONDS; } for (int i = 0; i < month; i++) { timestamp += DAY_SECONDS * days[i]; } for (int i = 1; i < day; i++) { timestamp += DAY_SECONDS; } timestamp += ((60 * hour + minutes) * 60) + seconds; return timestamp; } catch (RuntimeException ignored) { throw FAILED_TO_PARSE_DATE; } }   }
HttpDate { static long parse(byte[] bytes, int start) throws ParseException { try { int day = trimAndDecodePositiveInt(bytes, start + 5, 2); int month = -1; for (int i = 0; i < MONTHS_IN_YEAR.length; i++) { byte[] entry = MONTHS_IN_YEAR[i]; if (entry[0] == bytes[start + 8] && entry[1] == bytes[start + 9] && entry[2] == bytes[start + 10]) { month = i; } } int yearLength = '0' <= bytes[start + 12 + 2] && bytes[start + 12 + 2] <= '9' ? 4 : 2; int year = (yearLength == 2 ? 2000 : 0) + trimAndDecodePositiveInt(bytes, start + 12, yearLength); int hour = trimAndDecodePositiveInt(bytes, start + 13 + yearLength, 2); int minutes = trimAndDecodePositiveInt(bytes, start + 16 + yearLength, 2); int seconds = trimAndDecodePositiveInt(bytes, start + 19 + yearLength, 2); boolean isLeapYear = isLeap(year); int[] days = isLeapYear ? DAYS_IN_MONTH_LEAP : DAYS_IN_MONTH; year = year - 1970; int yearsLeft = year % 4; long timestamp = 0; int fy = (year - yearsLeft) / 4; for (int i = 0; i < fy; i++) { timestamp += FOUR_YEAR_SECONDS; } timestamp += yearsLeft * YEAR_SECONDS; if (yearsLeft > 2) { timestamp += DAY_SECONDS; } for (int i = 0; i < month; i++) { timestamp += DAY_SECONDS * days[i]; } for (int i = 1; i < day; i++) { timestamp += DAY_SECONDS; } timestamp += ((60 * hour + minutes) * 60) + seconds; return timestamp; } catch (RuntimeException ignored) { throw FAILED_TO_PARSE_DATE; } }   static final ParseException FAILED_TO_PARSE_DATE; }