Skip to content
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

Revert "remove deprecated datetimeformatparserlegacy" #592

Merged
merged 1 commit into from
Sep 20, 2023
Merged
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
86 changes: 86 additions & 0 deletions src/main/java/emissary/util/DateTimeFormatParserLegacy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package emissary.util;

import emissary.config.Configurator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class DateTimeFormatParserLegacy {

protected static final Logger logger = LoggerFactory.getLogger(DateTimeFormatParserLegacy.class);

/** Date formats we can use to parse event date strings */
@Deprecated
protected static final List<SimpleDateFormat> dateFormats = new ArrayList<>();

static {
configure();
}

protected static void configure() {
try {
Configurator configG = emissary.config.ConfigUtil.getConfigInfo(DateTimeFormatParserLegacy.class);
for (final String dfentry : configG.findEntries("DATE_FORMAT")) {
try {
final SimpleDateFormat sdf = new SimpleDateFormat(dfentry);
sdf.setLenient(true);
dateFormats.add(sdf);
} catch (Exception ex) {
logger.debug("DATE_FORMAT entry '{}' cannot be parsed", dfentry, ex);
}
}
logger.debug("Loaded {} DATE_FORMAT entries", dateFormats.size());
} catch (IOException e) {
logger.error("Cannot open default config file", e);
}
}


/**
* Parse an RFC-822 Date or one of the thousands of variants make a quick attempt to normalize the timezone information
* and get the timestamp in GMT. Should change to pass in a default from the U124 header
*
* @param dateString the string date from the RFC 822 Date header
* @param supplyDefaultOnBad when true use current date if sentDate is unparseable
* @return the GMT time of the event or NOW if unparseable, or null if supplyDefaultOnBad is false
*/
@Deprecated
public static Date parseEventDate(final String dateString, final boolean supplyDefaultOnBad) {
Date date = null;
if (dateString != null && dateString.length() > 0) {
// Take it apart and stick it back together to get
// get rid of multiple contiguous spaces
String instr = dateString.replaceAll("\t+", " "); // tabs
instr = instr.replaceAll("[ ]+", " "); // multiple spaces
instr = instr.replaceAll("=0D$", ""); // common qp'ified ending

// try each date format in turn until one works
synchronized (dateFormats) {
for (final SimpleDateFormat sdf : dateFormats) {
try {
date = sdf.parse(instr);
break;
} catch (Exception e) {
// Ignore.
logger.debug("Error parsing date", e);
}
}
}
}

// Use the default if required
if (date == null && supplyDefaultOnBad) {
date = new Date();
}

// Let them have it.
return date;
}

}
42 changes: 42 additions & 0 deletions src/main/resources/emissary/util/DateTimeFormatParserLegacy.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

# see SimpleDateFormat javadocs to understand these
DATE_FORMAT = "E, d MMM yyyy HH:mm:ss Z"
DATE_FORMAT = "E, d MMM yyyy HH:mm:ss z"
DATE_FORMAT = "E, d MMM yyyy HH:mm:ss"
DATE_FORMAT = "d MMM yyyy HH:mm:ss Z"
DATE_FORMAT = "E, d MMM yy HH:mm:ss Z"
DATE_FORMAT = "E, d MMM yyyy H:mm:ss Z"
DATE_FORMAT = "E, d MMM yyyy HH:mm Z"
DATE_FORMAT = "E, d MMM yyyy KK:mm:ss a Z"
DATE_FORMAT = "E d MMM. yyyy HH:mm:ss Z"
DATE_FORMAT = "E, d MMM. yyyy HH:mm:ss Z"
DATE_FORMAT = "E, d MMM yyyy HH:mm:ss Z (z)"
DATE_FORMAT = "E, MMM d, yyyy HH:mm:ss z"
DATE_FORMAT = "E, MMM d, yyyy HH:mm:ss"
DATE_FORMAT = "E, MMM d, yyyy KK:mm a"
DATE_FORMAT = "dd-MMM-yyyy HH:mm:ss Z"
DATE_FORMAT = "E dd-MMM-yyyy HH:mm:ss"
DATE_FORMAT = "E dd-MMM-yyyy HH:mm:ss Z"
DATE_FORMAT = "MM/dd/yy KK:mm a"
DATE_FORMAT = "MM/dd/yy KK:mm:ss a"
DATE_FORMAT = "MM/dd/yy KK:mm"
DATE_FORMAT = "MM/dd/yy KK:mm:ss"
DATE_FORMAT = "MM/dd/yy HH:mm"
DATE_FORMAT = "MM/dd/yy HH:mm:ss"
DATE_FORMAT = "MM/dd/yy kk:mm"
DATE_FORMAT = "MM/dd/yy kk:mm:ss"
DATE_FORMAT = "MMM d yyyy HH:mm:ss"
DATE_FORMAT = "E MMM d HH:mm:ss yyyy"
DATE_FORMAT = "E MMM d yyyy HH:mm:ss"
DATE_FORMAT = "d MMM yyyy HH:mm z"
DATE_FORMAT = "d MMM yyyy HH:mm"
DATE_FORMAT = "E MMM d yyyy HH:mm:ss z"
DATE_FORMAT = "E,d MMM yyyy KK:mm:ss a"
DATE_FORMAT = "E, MMM d, yyyy, HH:mm:ss"
DATE_FORMAT = "E, MMM d, yyyy, HH:mm:ss a"
DATE_FORMAT = "E, MMM d, yyyy, HH:mm:ss a z"
DATE_FORMAT = "E, MMM d, yyyy, HH:mm:ss a Z"
DATE_FORMAT = "E, d MMM, yyyy HH:mm:ss a"
DATE_FORMAT = "dd-MM-yyyy"
DATE_FORMAT = "dd.MM.yyyy"
DATE_FORMAT = "dd/MM/yyyy"