Skip to content

Commit

Permalink
code cleanup of DateTimeConverter
Browse files Browse the repository at this point in the history
- remove wrapper constructor
- remove wrapper methods
  • Loading branch information
ashitsalesforce committed Feb 14, 2024
1 parent 04005d5 commit 3db12af
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ private long getTimeInMillis(TimeZone tz, Object paramValue) {
return ((Date)paramValue).getTime();
}
else if(paramValue instanceof String) {
Calendar cal = (Calendar)new DateTimeConverter(tz).convert(java.util.Calendar.class, paramValue);
Calendar cal = (Calendar)new DateTimeConverter(tz, false).convert(java.util.Calendar.class, paramValue);
return cal.getTimeInMillis();
} else {
throw new IllegalArgumentException(Messages.getFormattedString("DatabaseDAO.errorParamMappingType", paramValue.getClass().getName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,15 @@

public class DateOnlyConverter extends DateTimeConverter implements Converter {

public DateOnlyConverter(TimeZone tz) {
super(tz);
}

public DateOnlyConverter(TimeZone tz, boolean useEuroDateFormat) {
super(tz, useEuroDateFormat);
}

Calendar getCalendar(TimeZone tz) {
protected Calendar getCalendar(TimeZone tz) {
return DateOnlyCalendar.getInstance(tz);
}

Calendar getNAValueCalendar() {
protected Calendar getNAValueCalendar() {
return NADateOnlyCalendarValue.getInstance();
}
}
}
48 changes: 10 additions & 38 deletions src/main/java/com/salesforce/dataloader/dyna/DateTimeConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,11 @@ public class DateTimeConverter implements Converter {
final boolean useEuroDates;
final TimeZone timeZone;

public DateTimeConverter(TimeZone tz) {
this(tz, false);

}

public DateTimeConverter(TimeZone tz, boolean useEuroDateFormat) {
this.timeZone = tz;
this.useEuroDates = useEuroDateFormat;
}

Calendar parseDate(TimeZone tz, String dateString, String pattern) {
final DateFormat df = new SimpleDateFormat(pattern);
df.setTimeZone(tz);
return parseDate(dateString, df);
}

private Calendar parseDate(String dateString, DateFormat fmt) {
final ParsePosition pos = new ParsePosition(0);
fmt.setLenient(false);
Expand All @@ -78,25 +67,6 @@ private Calendar parseDate(String dateString, DateFormat fmt) {
return null;
}

/**
* Attempts to parse a date string using the given formatting patterns
*
* @param dateString
* The date string to parse
* @param patterns
* Patterns to try. These will be used in the constructor for SimpleDateFormat
* @return A Calendar object representing the given date string
*/
private Calendar tryParse(TimeZone tz, String dateString, String... patterns) {
if (patterns == null) return null;
for (String pattern : patterns) {
Calendar cal = parseDate(tz, dateString, pattern);
if (cal != null) return cal;
}
return null;
}


@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public Object convert(Class type, Object value) {
Expand All @@ -107,16 +77,15 @@ public Object convert(Class type, Object value) {
if(value instanceof NATextValue) {
return getNAValueCalendar();
}

if (value instanceof Calendar) { return value; }

Calendar cal = getCalendar(this.timeZone);

if (value instanceof Date) {
cal.setTimeInMillis(((Date)value).getTime());
return cal;
}

if (value instanceof Calendar) { return value; }

String dateString = value.toString().trim();
int len = dateString.length();

Expand All @@ -128,8 +97,10 @@ public Object convert(Class type, Object value) {
timeZoneForValue = GMT_TZ;
}

for (String basePattern : useEuroDates ? supportedEuropeanPatterns : supportedRegularPatterns) {
cal = tryParse(timeZoneForValue, dateString, basePattern);
for (String pattern : useEuroDates ? supportedEuropeanPatterns : supportedRegularPatterns) {
final DateFormat df = new SimpleDateFormat(pattern);
df.setTimeZone(timeZoneForValue);
cal = parseDate(dateString, df);
if (cal != null) return cal;
}

Expand All @@ -146,11 +117,12 @@ public Object convert(Class type, Object value) {
throw new ConversionException("Failed to parse date: " + value);
}

Calendar getCalendar(TimeZone timezone) {
// NOTE: Always use this method to get Calendar instance
protected Calendar getCalendar(TimeZone timezone) {
return Calendar.getInstance(timezone);
}

Calendar getNAValueCalendar() {
protected Calendar getNAValueCalendar() {
return NACalendarValue.getInstance();
}

Expand All @@ -159,7 +131,7 @@ Calendar getNAValueCalendar() {
* These patterns are a subset of patterns supported by Java text.SimpleDateFormat
* https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html
*/
static List<String> getSupportedPatterns(boolean europeanDates) {
private static List<String> getSupportedPatterns(boolean europeanDates) {

List<String> basePatterns = new ArrayList<String>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public void testFullBasicFormat() {
public void testDateConverterWithNull() {

Calendar calDate;
DateTimeConverter converter = new DateTimeConverter(TZ);
DateTimeConverter converter = new DateTimeConverter(TZ, false);

// test null and empty string
calDate = (Calendar) converter.convert(null, null);
Expand All @@ -213,7 +213,7 @@ public void testDateConverterWithNull() {
public void testDateClosureUnderCalendarConversion() {

Calendar calDate;
DateTimeConverter converter = new DateTimeConverter(TZ);
DateTimeConverter converter = new DateTimeConverter(TZ, false);

// if we pass in a calendar, should get the same Calendar back
Calendar testCalDate = Calendar.getInstance();
Expand Down Expand Up @@ -650,9 +650,9 @@ public void testDateConverterNegative() {
// https://developer.salesforce.com/docs/atlas.en-us.dataLoader.meta/dataLoader/supported_data_types.htm
@Test
public void testUserSpecifiedTimeZoneIsUsed() throws Exception {
DateTimeConverter AsianTZDateConverter = new DateTimeConverter(TimeZone.getTimeZone("Asia/Tokyo"));
DateTimeConverter USTZDateConverter = new DateTimeConverter(TimeZone.getTimeZone("America/Los_Angeles"));
DateTimeConverter GMTTZDateConverter = new DateTimeConverter(TimeZone.getTimeZone("GMT"));
DateTimeConverter AsianTZDateConverter = new DateTimeConverter(TimeZone.getTimeZone("Asia/Tokyo"), false);
DateTimeConverter USTZDateConverter = new DateTimeConverter(TimeZone.getTimeZone("America/Los_Angeles"), false);
DateTimeConverter GMTTZDateConverter = new DateTimeConverter(TimeZone.getTimeZone("GMT"), false);
DateOnlyConverter AsianTZDateOnlyConverter = new DateOnlyConverter(TimeZone.getTimeZone("Asia/Tokyo"), false);
DateOnlyConverter USTZDateOnlyConverter = new DateOnlyConverter(TimeZone.getTimeZone("America/Los_Angeles"), false);
DateOnlyConverter GMTTZDateOnlyConverter = new DateOnlyConverter(TimeZone.getTimeZone("GMT"), false);
Expand Down

0 comments on commit 3db12af

Please sign in to comment.