Skip to content

Commit

Permalink
Merge branch '3.11' of https://github.com/JumpMind/symmetric-ds.git i…
Browse files Browse the repository at this point in the history
…nto 3.11
  • Loading branch information
erilong committed Jan 17, 2020
2 parents cc86a1c + cea5d08 commit fe6f685
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 12 deletions.
Expand Up @@ -33,7 +33,7 @@
import org.apache.commons.lang.ArrayUtils;
import org.jumpmind.db.model.TypeMap;
import org.jumpmind.db.util.BinaryEncoding;
import org.jumpmind.util.AppUtils;
import org.jumpmind.exception.ParseException;
import org.jumpmind.util.FormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -173,8 +173,13 @@ protected String formatDateTimeValue(Object object, int type) {
date = FormatUtils.parseDate(object.toString(),
(String[])ArrayUtils.addAll(FormatUtils.TIMESTAMP_PATTERNS, FormatUtils.TIME_PATTERNS));
} catch (Exception ex) {
log.debug("Failed to parse argument as a date " + object + " " + ex);
return "'" + object + "'";
try {
// Try Timestamp with time zone
date = FormatUtils.parseTimestampWithTimezone(object == null ? "" : object.toString());
} catch(Exception e) {
log.debug("Failed to parse argument as a date " + object + " " + ex);
return "'" + object + "'";
}
}
}

Expand Down
Expand Up @@ -92,17 +92,19 @@ protected boolean isTimestampNewer(Conflict conflict, AbstractDatabaseWriter wri
// If you are in this situation because of an instance where the conflict exists
// because the row doesn't exist, then existing simply needs to be null
if (existingStr != null) {
int split = existingStr.lastIndexOf(" ");
existingTs = FormatUtils.parseDate(existingStr.substring(0, split).trim(),
FormatUtils.TIMESTAMP_PATTERNS,
TimeZone.getTimeZone(existingStr.substring(split).trim()));
// int split = existingStr.lastIndexOf(" ");
// existingTs = FormatUtils.parseDate(existingStr.substring(0, split).trim(),
// FormatUtils.TIMESTAMP_PATTERNS,
// TimeZone.getTimeZone(existingStr.substring(split).trim()));
existingTs = FormatUtils.parseTimestampWithTimezone(existingStr, FormatUtils.TIMESTAMP_WITH_TIMEZONE_PATTERNS);
}
// Get the loadingTs with timezone
if (loadingStr != null) {
int split = loadingStr.lastIndexOf(" ");
loadingTs = FormatUtils.parseDate(loadingStr.substring(0, split).trim(),
FormatUtils.TIMESTAMP_PATTERNS,
TimeZone.getTimeZone(loadingStr.substring(split).trim()));
// int split = loadingStr.lastIndexOf(" ");
// loadingTs = FormatUtils.parseDate(loadingStr.substring(0, split).trim(),
// FormatUtils.TIMESTAMP_PATTERNS,
// TimeZone.getTimeZone(loadingStr.substring(split).trim()));
loadingTs = FormatUtils.parseTimestampWithTimezone(loadingStr, FormatUtils.TIMESTAMP_WITH_TIMEZONE_PATTERNS);
}
} else {
// Get the existingTs
Expand Down
32 changes: 31 additions & 1 deletion symmetric-util/src/main/java/org/jumpmind/util/FormatUtils.java
Expand Up @@ -20,8 +20,12 @@
*/
package org.jumpmind.util;

import java.sql.Timestamp;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
Expand All @@ -33,6 +37,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DurationFormatUtils;
import org.apache.commons.lang.time.FastDateFormat;
Expand All @@ -46,6 +51,10 @@ public final class FormatUtils {

public static final String[] TIME_PATTERNS = { "HH:mm:ss.S", "HH:mm:ss",
"yyyy-MM-dd HH:mm:ss.S", "yyyy-MM-dd HH:mm:ss" };

public static final String[] TIMESTAMP_WITH_TIMEZONE_PATTERNS = {
"yyyy-MM-dd HH:mm:ss.n xxx"
};

public static final FastDateFormat TIMESTAMP_FORMATTER = FastDateFormat
.getInstance("yyyy-MM-dd HH:mm:ss.SSS");
Expand Down Expand Up @@ -85,7 +94,7 @@ public final class FormatUtils {
static {
isInfamousTurkey = Locale.getDefault().getCountry().equalsIgnoreCase("tr");
}

private FormatUtils() {
}

Expand Down Expand Up @@ -419,6 +428,27 @@ public static Date parseDate(String str, String[] parsePatterns, TimeZone timeZo
throw new ParseException("Unable to parse the date: " + str);
}

public static Timestamp parseTimestampWithTimezone(String str) {
return parseTimestampWithTimezone(str, TIMESTAMP_WITH_TIMEZONE_PATTERNS);
}

public static Timestamp parseTimestampWithTimezone(String str, String[] parsePatterns) {
Timestamp ret = null;
for(int i = 0; i < parsePatterns.length; i++) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(parsePatterns[i]);
ret = null;
try {
ret = Timestamp.from(ZonedDateTime.parse(str, formatter).toInstant());
} catch(DateTimeParseException e) {

}
if(ret != null) {
return ret;
}
}
throw new ParseException("Unable to parse the date: " + str);
}

public static String[] splitOnSpacePreserveQuotedStrings(String source) {
List<String> matchList = new ArrayList<String>();
Pattern regex = Pattern.compile("[^\\s\"']+|\"([^\"]*)\"|'([^']*)'");
Expand Down

0 comments on commit fe6f685

Please sign in to comment.