Skip to content

Update parse date #19

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 81 additions & 23 deletions src/main/java/org/mcsoxford/rss/Dates.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package org.mcsoxford.rss;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.TimeZone;

/**
* Internal helper class for date conversions.
Expand All @@ -26,27 +26,85 @@
*/
final class Dates {

/**
* @see <a href="http://www.ietf.org/rfc/rfc0822.txt">RFC 822</a>
*/
private static final SimpleDateFormat RFC822 = new SimpleDateFormat(
"EEE, dd MMM yyyy HH:mm:ss Z", java.util.Locale.ENGLISH);

/* Hide constructor */
private Dates() {}

/**
* Parses string as an RFC 822 date/time.
*
* @throws RSSFault if the string is not a valid RFC 822 date/time
*/
static java.util.Date parseRfc822(String date) {
try {
return RFC822.parse(date);
} catch (ParseException e) {
throw new RSSFault(e);
}
}
static private boolean isGMT = false;
static final String[] standardFormats = { "EEEE', 'dd-MMM-yy HH:mm:ss z", // RFC
// 850
// (obsoleted
// by
// 1036)
"EEEE', 'dd-MMM-yy HH:mm:ss", // ditto but no tz. Happens too often
"EEE', 'dd-MMM-yyyy HH:mm:ss z", // RFC 822/1123
"EEE', 'dd MMM yyyy HH:mm:ss z", // REMIND what rfc? Apache/1.1
"EEEE', 'dd MMM yyyy HH:mm:ss z", // REMIND what rfc? Apache/1.1
"EEE', 'dd MMM yyyy hh:mm:ss z", // REMIND what rfc? Apache/1.1
"EEEE', 'dd MMM yyyy hh:mm:ss z", // REMIND what rfc? Apache/1.1
"EEE MMM dd HH:mm:ss z yyyy", // Date's string output format
"EEE MMM dd HH:mm:ss yyyy", // ANSI C asctime format()
"EEE', 'dd-MMM-yy HH:mm:ss", // No time zone 2 digit year RFC 1123
"EEE', 'dd-MMM-yyyy HH:mm:ss" // No time zone RFC 822/1123
};

/*
* because there are problems with JDK1.1.6/SimpleDateFormat with
* recognizing GMT, we have to create this workaround with the following
* hardcoded strings
*/
static final String[] gmtStandardFormats = { "EEEE',' dd-MMM-yy HH:mm:ss 'GMT'", // RFC
// 850
// (obsoleted
// by
// 1036)
"EEE',' dd-MMM-yyyy HH:mm:ss 'GMT'", // RFC 822/1123
"EEE',' dd MMM yyyy HH:mm:ss 'GMT'", // REMIND what rfc? Apache/1.1
"EEEE',' dd MMM yyyy HH:mm:ss 'GMT'", // REMIND what rfc? Apache/1.1
"EEE',' dd MMM yyyy hh:mm:ss 'GMT'", // REMIND what rfc? Apache/1.1
"EEEE',' dd MMM yyyy hh:mm:ss 'GMT'", // REMIND what rfc? Apache/1.1
"EEE MMM dd HH:mm:ss 'GMT' yyyy" // Date's string output format
};

static String dateString;

static java.util.Date parse(String date) {
dateString = date.trim();
if (dateString.indexOf("GMT") != -1) {
isGMT = true;
}
return getDate();
}

static private java.util.Date getDate() {

int arrayLen = isGMT ? gmtStandardFormats.length : standardFormats.length;
for (int i = 0; i < arrayLen; i++) {
java.util.Date d = null;

if (isGMT) {
d = tryParsing(gmtStandardFormats[i]);
} else {
d = tryParsing(standardFormats[i]);
}
if (d != null) {
return d;
}

}

return null;
}

static private java.util.Date tryParsing(String format) {

java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(format, Locale.US);
if (isGMT) {
df.setTimeZone(TimeZone.getTimeZone("GMT"));
}
try {
return df.parse(dateString);
} catch (Exception e) {
return null;
}
}
}



4 changes: 2 additions & 2 deletions src/main/java/org/mcsoxford/rss/RSSHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public void set(String link) {
private final Setter SET_PUBDATE = new ContentSetter() {
@Override
public void set(String pubDate) {
final java.util.Date date = Dates.parseRfc822(pubDate);
final java.util.Date date = Dates.parse(pubDate);
if (item == null) {
feed.setPubDate(date);
} else {
Expand All @@ -175,7 +175,7 @@ public void set(String pubDate) {
private final Setter SET_LAST_BUILE_DATE = new ContentSetter() {
@Override
public void set(String pubDate) {
final java.util.Date date = Dates.parseRfc822(pubDate);
final java.util.Date date = Dates.parse(pubDate);
if (item == null) {
feed.setLastBuildDate(date);
} else {
Expand Down