Skip to content

Commit

Permalink
improve parsing with DateTimeFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Oct 22, 2024
1 parent 8b5a074 commit f2dffed
Show file tree
Hide file tree
Showing 18 changed files with 246 additions and 117 deletions.
8 changes: 4 additions & 4 deletions core/src/main/java/lucee/commons/date/DateTimeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
**/
package lucee.commons.date;

import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import lucee.commons.i18n.FormatUtil;
import lucee.commons.i18n.FormatterWrapper;
import lucee.commons.lang.StringUtil;
import lucee.runtime.PageContext;
import lucee.runtime.engine.ThreadLocalPageContext;
Expand All @@ -34,7 +34,7 @@

public abstract class DateTimeUtil {

private final static DateTimeFormatter HTTP_TIME_STRING_FORMAT = FormatUtil.getDateTimeFormatter(Locale.ENGLISH, "EE, dd-MMM-yyyy HH:mm:ss zz");
private final static FormatterWrapper HTTP_TIME_STRING_FORMAT = FormatUtil.getDateTimeFormatter(Locale.ENGLISH, "EE, dd-MMM-yyyy HH:mm:ss zz");

private static final double DAY_MILLIS = 86400000D;
private static final long CF_UNIX_OFFSET = 2209161600000L;
Expand Down Expand Up @@ -266,9 +266,9 @@ public static String toHTTPTimeString(long time, boolean oldFormat) {
*/
public static String toHTTPTimeString(Date date, boolean oldFormat) {
if (oldFormat) {
return StringUtil.replace(FormatUtil.format(HTTP_TIME_STRING_FORMAT, date, TimeZoneConstants.GMT), "+00:00", "", true);
return StringUtil.replace(FormatUtil.format(HTTP_TIME_STRING_FORMAT.formatter, date, TimeZoneConstants.GMT), "+00:00", "", true);
}
return StringUtil.replace(FormatUtil.format(HTTP_TIME_STRING_FORMAT, date, TimeZoneConstants.UTC), "+00:00", "", true);
return StringUtil.replace(FormatUtil.format(HTTP_TIME_STRING_FORMAT.formatter, date, TimeZoneConstants.UTC), "+00:00", "", true);
}

public static String format(long time, Locale l, TimeZone tz) {
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/lucee/commons/i18n/DateFormatPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public static String format(Locale locale, String pattern, Date date) {

public static String format(Locale locale, TimeZone timeZone, String pattern, Date date) {
if (timeZone == null) timeZone = ThreadLocalPageContext.getTimeZone();
return FormatUtil.format(getSimpleDateFormat(locale, pattern), date, timeZone);
return FormatUtil.format(getSimpleDateFormat(locale, pattern, timeZone), date, timeZone);
}

private static DateTimeFormatter getSimpleDateFormat(Locale locale, String pattern) {
private static DateTimeFormatter getSimpleDateFormat(Locale locale, String pattern, TimeZone timeZone) {
if (locale == null) locale = ThreadLocalPageContext.getLocale();

String key = locale.toString() + '-' + pattern;
Expand All @@ -58,7 +58,7 @@ private static DateTimeFormatter getSimpleDateFormat(Locale locale, String patte
ref = datax.get(key);
sdf = ref == null ? null : ref.get();
if (sdf == null) {
sdf = FormatUtil.getDateTimeFormatter(locale, pattern);
sdf = FormatUtil.getDateTimeFormatter(locale, pattern, timeZone).formatter;
datax.put(key, new SoftReference<>(sdf));
}
}
Expand Down
244 changes: 166 additions & 78 deletions core/src/main/java/lucee/commons/i18n/FormatUtil.java

Large diffs are not rendered by default.

20 changes: 19 additions & 1 deletion core/src/main/java/lucee/commons/i18n/FormatterWrapper.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
package lucee.commons.i18n;

import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public class FormatterWrapper {
public final DateTimeFormatter formatter;
public int successCount;
public final String pattern;
public final boolean custom;
public final short type;
public final ZoneId zone;

FormatterWrapper(DateTimeFormatter formatter) {
FormatterWrapper(DateTimeFormatter formatter, String pattern, short type, ZoneId zone) {
this.formatter = formatter;
this.successCount = 0;
this.pattern = pattern;
this.type = type;
this.zone = zone;
this.custom = false;
}

FormatterWrapper(DateTimeFormatter formatter, String pattern, short type, ZoneId zone, boolean custom) {
this.formatter = formatter;
this.successCount = 0;
this.pattern = pattern;
this.type = type;
this.zone = zone;
this.custom = custom;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public DataDogLayout() {
super(CharsetUtil.UTF8, new byte[0], new byte[0]);
engine = CFMLEngineFactory.getInstance();
caster = engine.getCastUtil();
format = FormatUtil.getDateTimeFormatter(null, "yyyy-MM-dd HH:mm:ss");
format = FormatUtil.getDateTimeFormatter(null, "yyyy-MM-dd HH:mm:ss").formatter;
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/lucee/commons/lang/SystemOut.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

public final class SystemOut {

public static final DateTimeFormatter FORMAT = FormatUtil.getDateTimeFormatter(null, "yyyy-MM-dd HH:mm:ss.S");
public static final DateTimeFormatter FORMAT = FormatUtil.getDateTimeFormatter(null, "yyyy-MM-dd HH:mm:ss.S").formatter;

/**
* logs a value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static String format(Date date, TimeZone tz, String pattern) {
tmp = map.get(id);
format = tmp == null ? null : tmp.get();
if (format == null) {
format = FormatUtil.getDateTimeFormatter(locale, pattern);
format = FormatUtil.getDateTimeFormatter(locale, pattern).formatter;
map.put(id, new SoftReference<DateTimeFormatter>(format));
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/lucee/runtime/dump/DumpUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public static DumpData toDumpData(Object o, PageContext pageContext, int maxleve
// Calendar
if (o instanceof Calendar) {
Calendar c = (Calendar) o;
DateTimeFormatter formatter = FormatUtil.getDateTimeFormatter(Locale.ENGLISH, "EE, dd MMM yyyy HH:mm:ss zz");
DateTimeFormatter formatter = FormatUtil.getDateTimeFormatter(Locale.ENGLISH, "EE, dd MMM yyyy HH:mm:ss zz").formatter;
DumpTable table = new DumpTable("date", "#ff9900", "#ffcc00", "#000000");
table.setTitle("java.util.Calendar");
table.appendRow(1, new SimpleDumpData("Timezone"), new SimpleDumpData(TimeZoneUtil.toString(c.getTimeZone())));
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/lucee/runtime/format/DateFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public String format(long time, String mask, TimeZone tz) {
else if (lcMask.equals("long")) return getAsString(calendar, java.text.DateFormat.LONG, tz);
else if (lcMask.equals("full")) return getAsString(calendar, java.text.DateFormat.FULL, tz);
else if ("iso8601".equals(lcMask) || "iso".equals(lcMask)) {
DateTimeFormatter formatter = FormatUtil.getDateTimeFormatter(null, "yyyy-MM-dd");
DateTimeFormatter formatter = FormatUtil.getDateTimeFormatter(null, "yyyy-MM-dd").formatter;
return FormatUtil.format(formatter, calendar.getTime(), tz);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
**/
package lucee.runtime.functions.displayFormatting;

import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.TimeZone;

import lucee.commons.i18n.FormatUtil;
import lucee.commons.i18n.FormatterWrapper;
import lucee.commons.lang.StringUtil;
import lucee.runtime.PageContext;
import lucee.runtime.engine.ThreadLocalPageContext;
Expand Down Expand Up @@ -93,7 +93,7 @@ else if ("epochms".equalsIgnoreCase(mask)) {
return epoch;
}

DateTimeFormatter formatter;
FormatterWrapper formatter;
if (mask != null && (

mask.equalsIgnoreCase("short") ||
Expand Down Expand Up @@ -123,7 +123,7 @@ else if ("epochms".equalsIgnoreCase(mask)) {
else {
formatter = FormatUtil.getDateTimeFormatter(locale, convertMask(mask));

String result = FormatUtil.format(formatter, datetime, tz);
String result = FormatUtil.format(formatter.formatter, datetime, tz);
if (!StringUtil.isEmpty(result)) {
int start, end = 0;
String content;
Expand All @@ -139,7 +139,7 @@ else if ("epochms".equalsIgnoreCase(mask)) {
}
return result;
}
return FormatUtil.format(formatter, datetime, tz);
return FormatUtil.format(formatter.formatter, datetime, tz);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Locale;
import java.util.TimeZone;

import lucee.print;
import lucee.commons.date.TimeZoneUtil;
import lucee.commons.i18n.FormatUtil;
import lucee.commons.lang.ExceptionUtil;
Expand Down Expand Up @@ -79,17 +80,25 @@ private static lucee.runtime.type.dt.DateTime _call(PageContext pc, Object oDate
if (locale == null) locale = pc.getLocale();
DateFormat df = FormatUtil.getDateTimeFormat(locale, tz, format);
try {

return new DateTimeImpl(df.parse(strDate));
// old.rocks
// return new DateTimeImpl(FormatUtil.parse(FormatUtil.getDateTimeFormatter(locale, format),
// strDate, tz));
return new DateTimeImpl(FormatUtil.parse(FormatUtil.getDateTimeFormatter(locale, format), strDate, tz.toZoneId()));
}
catch (Exception e) {
ExpressionException ee = new ExpressionException("could not parse the date [" + strDate + "] with the format [" + format + "] with the locale [" + locale
+ "] and the timezone [" + (tz == null ? "" : tz.getID()) + "]");
ExceptionUtil.initCauseEL(ee, e);
throw ee;
catch (Exception ex) {
try {

DateTimeImpl res = new DateTimeImpl(df.parse(strDate));
print.e("--------- old rocks --------");
print.e(ex);
return res;
// old.rocks
// return new DateTimeImpl(FormatUtil.parse(FormatUtil.getDateTimeFormatter(locale, format),
// strDate, tz));
}
catch (Exception e) {
ExpressionException ee = new ExpressionException("could not parse the date [" + strDate + "] with the format [" + format + "] with the locale [" + locale
+ "] and the timezone [" + (tz == null ? "" : tz.getID()) + "]");
ExceptionUtil.initCauseEL(ee, e);
throw ee;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/lucee/runtime/net/smtp/SMTPClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public static String getNow(TimeZone tz) {
SoftReference<DateTimeFormatter> tmp = formatters.get(tz);
DateTimeFormatter df = tmp == null ? null : tmp.get();
if (df == null) {
df = FormatUtil.getDateTimeFormatter(Locale.US, "EEE, d MMM yyyy HH:mm:ss Z (z)");
df = FormatUtil.getDateTimeFormatter(Locale.US, "EEE, d MMM yyyy HH:mm:ss Z (z)").formatter;
formatters.put(tz, new SoftReference<DateTimeFormatter>(df));
}
return FormatUtil.format(df, new Date(), tz);
Expand Down
20 changes: 17 additions & 3 deletions core/src/main/java/lucee/runtime/op/date/DateCaster.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

import lucee.print;
import lucee.commons.date.DateTimeUtil;
import lucee.commons.date.JREDateTimeUtil;
import lucee.commons.date.TimeZoneConstants;
Expand All @@ -42,6 +42,7 @@
import lucee.runtime.engine.ThreadLocalPageContext;
import lucee.runtime.exp.ExpressionException;
import lucee.runtime.exp.PageException;
import lucee.runtime.i18n.LocaleConstant;
import lucee.runtime.i18n.LocaleFactory;
import lucee.runtime.op.Castable;
import lucee.runtime.op.Caster;
Expand Down Expand Up @@ -212,7 +213,10 @@ public static DateTime toDateTime(Locale locale, String str, TimeZone tz, boolea
DateTime dt = toDateTimeNew(locale, str, tz, null, useCommomDateParserAsWell);
if (dt == null) {
dt = toDateTimeOld(locale, str, tz, null, false);
// if (dt != null) print.e("old.rocks(" + locale + "):" + str);
if (dt != null) {
print.e("--------- old rockx --------");
print.e(locale + "-" + str + "-" + tz.getID() + ":" + useCommomDateParserAsWell);
}
}
if (dt == null) {
String prefix = locale.getLanguage() + "-" + locale.getCountry() + "-";
Expand All @@ -225,6 +229,10 @@ public static DateTime toDateTime(Locale locale, String str, TimeZone tz, boolea
return dt;
}

public static void main(String[] args) throws PageException {
toDateTime(LocaleConstant.ENGLISH_UNITED_KINDOM, "31/12/2008", TimeZoneConstants.CET, false);
}

/**
* parse a string to a Datetime Object, returns null if can't convert
*
Expand Down Expand Up @@ -311,12 +319,18 @@ public static DateTime toDateTimeNew(Locale locale, String str, TimeZone tz, Dat

try {
for (FormatterWrapper fw: all) {

// if (fw.custom && fw.pattern.length() != str.length()) continue;
try {
DateTimeImpl res = new DateTimeImpl(Date.from(ZonedDateTime.parse(str, fw.formatter).toInstant()).getTime(), false);
DateTimeImpl res = new DateTimeImpl(FormatUtil.parse(fw.formatter, str, fw.type, fw.zone));
fw.successCount++;
return res;
}
catch (Exception e) {
if (fw.custom) {
print.e("---- " + fw.pattern + " ------");
print.e(e);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/lucee/runtime/type/dt/DateImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
*/
public final class DateImpl extends Date implements SimpleValue {

private static DateTimeFormatter luceeFormatter = FormatUtil.getDateTimeFormatter(Locale.US, "yyyy-MM-dd");
private static DateTimeFormatter luceeFormatter = FormatUtil.getDateTimeFormatter(Locale.US, "yyyy-MM-dd").formatter;

// private TimeZone timezone;

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/lucee/runtime/type/dt/TimeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
*/
public final class TimeImpl extends Time implements SimpleValue {

private static DateTimeFormatter luceeFormatter = FormatUtil.getDateTimeFormatter(Locale.US, "HH:mm:ss");
private static DateTimeFormatter luceeFormatter = FormatUtil.getDateTimeFormatter(Locale.US, "HH:mm:ss").formatter;

public TimeImpl() {
super(System.currentTimeMillis());
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/java/org/apache/taglibs/datetime/FormatTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public final int doEndTag() throws PageException {

if (pat == null) {
pat = new SimpleDateFormat().toPattern(); // TODO find a better way for this
formatter = FormatUtil.getDateTimeFormatter(null, pat);
formatter = FormatUtil.getDateTimeFormatter(null, pat).formatter;
}

// Get a DateFormatSymbols
Expand All @@ -129,13 +129,13 @@ public final int doEndTag() throws PageException {
if (locale == null) {
throw new ApplicationException("datetime format tag could not find locale for localeRef \"" + localeRef + "\".");
}
formatter = FormatUtil.getDateTimeFormatter(locale, pat);
formatter = FormatUtil.getDateTimeFormatter(locale, pat).formatter;
}
else if (locale_flag) {
formatter = FormatUtil.getDateTimeFormatter(pageContext.getRequest().getLocale(), pat);
formatter = FormatUtil.getDateTimeFormatter(pageContext.getRequest().getLocale(), pat).formatter;
}
else {
formatter = FormatUtil.getDateTimeFormatter(null, pat);
formatter = FormatUtil.getDateTimeFormatter(null, pat).formatter;
}

// See if there is a timeZone
Expand Down
2 changes: 1 addition & 1 deletion loader/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<project default="core" basedir="." name="Lucee"
xmlns:resolver="antlib:org.apache.maven.resolver.ant">

<property name="version" value="6.2.0.124-SNAPSHOT"/>
<property name="version" value="6.2.0.125-SNAPSHOT"/>

<taskdef uri="antlib:org.apache.maven.resolver.ant" resource="org/apache/maven/resolver/ant/antlib.xml">
<classpath>
Expand Down
2 changes: 1 addition & 1 deletion loader/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>org.lucee</groupId>
<artifactId>lucee</artifactId>
<version>6.2.0.124-SNAPSHOT</version>
<version>6.2.0.125-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Lucee Loader Build</name>
Expand Down

0 comments on commit f2dffed

Please sign in to comment.