/** * Formats an RFC 1123 format date. */ public static String formatRfc1123Date(Date date) { return formatRfc1123Date(date.getTime()); }
/** * @return the expiration time from the Expires header or -1 if not set */ private long getExpiresTime() { String expires = getHeader("Expires"); if (expires != null) { Date expiresDate = DateUtil.parseRfc1123Date(expires); if (expiresDate != null) { return expiresDate.getTime(); } else { // Per RFC2616, 14.21 (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.21): // "HTTP/1.1 clients and caches MUST treat other invalid date formats, // especially including the value "0", as in the past (i.e., "already // expired")." return 0; } } return -1; }
/** * Formats an ISO 8601 format date. */ public static String formatIso8601Date(Date date) { return formatIso8601Date(date.getTime()); }
/** * Tries to find a valid date from the input headers. * * @return The value of the date header, in milliseconds, or -1 if no Date could be determined. */ private static long getAndUpdateDate(Multimap<String, String> headers) { // Validate the Date header. Must conform to the HTTP date format. long timestamp = -1; long currentTime = getTimeSource().currentTimeMillis(); Collection<String> dates = headers.get(HttpHeaders.DATE); if (!dates.isEmpty()) { Date d = DateUtil.parseRfc1123Date(dates.iterator().next()); if (d != null) { timestamp = d.getTime(); } } if (timestamp == -1) { timestamp = currentTime; headers.replaceValues(HttpHeaders.DATE, ImmutableList.of(DateUtil.formatRfc1123Date(timestamp))); } return timestamp; }
@Test public void parseMalformedIso8691() { assertNull(DateUtil.parseIso8601DateTime("invalid date format")); }
/** * Tries to find a valid date from the input headers. * * @return The value of the date header, in milliseconds, or -1 if no Date could be determined. */ private static long getAndUpdateDate(Multimap<String, String> headers) { // Validate the Date header. Must conform to the HTTP date format. long timestamp = -1; long currentTime = getTimeSource().currentTimeMillis(); Collection<String> dates = headers.get("Date"); if (!dates.isEmpty()) { Date d = DateUtil.parseRfc1123Date(dates.iterator().next()); if (d != null) { timestamp = d.getTime(); if (Math.abs(currentTime - timestamp) > responseDateDriftLimit) { // Do not trust the date from response if it is too old (server time out of sync) timestamp = -1; } } } if (timestamp == -1) { timestamp = currentTime; headers.replaceValues("Date", ImmutableList.of(DateUtil.formatRfc1123Date(timestamp))); } return timestamp; }
@Test public void parseMalformedIso8691() { assertNull(DateUtil.parseIso8601DateTime("invalid date format")); }
/** * Formats an RFC 1123 format date. */ public static String formatRfc1123Date(Date date) { return formatRfc1123Date(date.getTime()); }
/** * @return the expiration time from the Expires header or -1 if not set */ private long getExpiresTime() { String expires = getHeader("Expires"); if (expires != null) { Date expiresDate = DateUtil.parseRfc1123Date(expires); if (expiresDate != null) { return expiresDate.getTime(); } else { // Per RFC2616, 14.21 (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.21): // "HTTP/1.1 clients and caches MUST treat other invalid date formats, // especially including the value "0", as in the past (i.e., "already // expired")." return 0; } } return -1; }
/** * Formats an ISO 8601 format date. */ public static String formatIso8601Date(Date date) { return formatIso8601Date(date.getTime()); }
@Test public void parseMalformedIso8691() { assertNull(DateUtil.parseIso8601DateTime("invalid date format")); }
/** * Formats an RFC 1123 format date. */ public static String formatRfc1123Date(Date date) { return formatRfc1123Date(date.getTime()); }
/** * @return the expiration time from the Expires header or -1 if not set */ public long getExpiresTime() { String expires = getHeader(HttpHeaders.EXPIRES); if (expires != null) { Date expiresDate = DateUtil.parseRfc1123Date(expires); if (expiresDate != null) { return expiresDate.getTime(); } else { // Per RFC2616, 14.21 (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.21): // "HTTP/1.1 clients and caches MUST treat other invalid date formats, // especially including the value "0", as in the past (i.e., "already // expired")." return 0; } } return -1; }
/** * Formats an ISO 8601 format date. */ public static String formatIso8601Date(Date date) { return formatIso8601Date(date.getTime()); }
@Test public void parseMalformedIso8691() { assertNull(DateUtil.parseIso8601DateTime("invalid date format")); }
/** * Formats an RFC 1123 format date. */ public static String formatRfc1123Date(Date date) { return formatRfc1123Date(date.getTime()); }
@Test public void parse() { for (int i = 0, j = rfc1123text.length; i < j; ++i) { assertEquals(timeStamps[i].getTime(), DateUtil.parseRfc1123Date(rfc1123text[i]).getTime()); } }
/** * Formats an ISO 8601 format date. */ public static String formatIso8601Date(Date date) { return formatIso8601Date(date.getTime()); }
@Test public void parseMalformedIso8691() { assertNull(DateUtil.parseIso8601DateTime("invalid date format")); }
/** * Formats an RFC 1123 format date. */ public static String formatRfc1123Date(Date date) { return formatRfc1123Date(date.getTime()); }