Skip to content

Commit

Permalink
LDEV-5115 - switch to DateTimeFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Oct 21, 2024
1 parent 8e60a95 commit 94e0d4a
Show file tree
Hide file tree
Showing 18 changed files with 110 additions and 100 deletions.
5 changes: 3 additions & 2 deletions core/src/main/java/lucee/aprint.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;

import lucee.commons.i18n.FormatUtil;
import lucee.commons.io.IOUtil;
import lucee.commons.io.SystemUtil;
import lucee.commons.io.res.Resource;
Expand All @@ -56,11 +57,11 @@
*/
public class aprint {
public static void edate(String value) {
e(SystemOut.FORMAT.format(new Date(System.currentTimeMillis())) + " " + value);
e(FormatUtil.format(SystemOut.FORMAT, new Date(System.currentTimeMillis()), null) + " " + value);
}

public static void date(String value) {
o(SystemOut.FORMAT.format(new Date(System.currentTimeMillis())) + " " + value);
o(FormatUtil.format(SystemOut.FORMAT, new Date(System.currentTimeMillis()), null) + " " + value);
}

public static void ds(boolean useOutStream) {
Expand Down
28 changes: 8 additions & 20 deletions core/src/main/java/lucee/commons/date/DateTimeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
**/
package lucee.commons.date;

import java.text.SimpleDateFormat;
import java.text.DateFormat;
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.lang.StringUtil;
import lucee.runtime.PageContext;
import lucee.runtime.engine.ThreadLocalPageContext;
Expand All @@ -33,20 +35,10 @@

public abstract class DateTimeUtil {

private final static SimpleDateFormat HTTP_TIME_STRING_FORMAT_OLD;
private final static SimpleDateFormat HTTP_TIME_STRING_FORMAT;
private final static DateFormat HTTP_TIME_STRING_FORMAT_OLD = FormatUtil.getDateTimeFormat(Locale.ENGLISH, TimeZoneConstants.GMT, "EE, dd MMM yyyy HH:mm:ss zz");
private final static DateFormat HTTP_TIME_STRING_FORMAT_NEW = FormatUtil.getDateTimeFormat(Locale.ENGLISH, TimeZoneConstants.UTC, "EE, dd-MMM-yyyy HH:mm:ss zz");

// public final static SimpleDateFormat DATETIME_FORMAT_LOCAL;

static {
// DATETIME_FORMAT_LOCAL = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");

HTTP_TIME_STRING_FORMAT_OLD = new SimpleDateFormat("EE, dd MMM yyyy HH:mm:ss zz", Locale.ENGLISH);
HTTP_TIME_STRING_FORMAT_OLD.setTimeZone(TimeZone.getTimeZone("GMT"));

HTTP_TIME_STRING_FORMAT = new SimpleDateFormat("EE, dd-MMM-yyyy HH:mm:ss zz", Locale.ENGLISH);
HTTP_TIME_STRING_FORMAT.setTimeZone(TimeZoneConstants.UTC);
}
private final static DateTimeFormatter 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 @@ -278,13 +270,9 @@ public static String toHTTPTimeString(long time, boolean oldFormat) {
*/
public static String toHTTPTimeString(Date date, boolean oldFormat) {
if (oldFormat) {
synchronized (HTTP_TIME_STRING_FORMAT_OLD) {
return StringUtil.replace(HTTP_TIME_STRING_FORMAT_OLD.format(date), "+00:00", "", true);
}
}
synchronized (HTTP_TIME_STRING_FORMAT) {
return StringUtil.replace(HTTP_TIME_STRING_FORMAT.format(date), "+00:00", "", true);
return StringUtil.replace(FormatUtil.format(HTTP_TIME_STRING_FORMAT, date, TimeZoneConstants.GMT), "+00:00", "", true);
}
return StringUtil.replace(FormatUtil.format(HTTP_TIME_STRING_FORMAT, date, TimeZoneConstants.UTC), "+00:00", "", true);
}

public static String format(long time, Locale l, TimeZone tz) {
Expand Down
13 changes: 6 additions & 7 deletions core/src/main/java/lucee/commons/i18n/DateFormatPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package lucee.commons.i18n;

import java.lang.ref.SoftReference;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
Expand All @@ -31,7 +31,7 @@

public final class DateFormatPool {

private final static Map<String, SoftReference<SimpleDateFormat>> datax = new ConcurrentHashMap<>();
private final static Map<String, SoftReference<DateFormat>> datax = new ConcurrentHashMap<>();

public static String format(Locale locale, TimeZone timeZone, String pattern, Date date) {
return getSimpleDateFormat(locale, timeZone, pattern).format(date);
Expand All @@ -45,21 +45,20 @@ public static String format(String pattern, Date date) {
return getSimpleDateFormat(null, null, pattern).format(date);
}

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

String key = locale.toString() + '-' + timeZone.getID() + '-' + pattern;
SoftReference<SimpleDateFormat> ref = datax.get(key);
SimpleDateFormat sdf = ref == null ? null : ref.get();
SoftReference<DateFormat> ref = datax.get(key);
DateFormat sdf = ref == null ? null : ref.get();

if (sdf == null) {
synchronized (SystemUtil.createToken("DateFormatPool", key)) {
ref = datax.get(key);
sdf = ref == null ? null : ref.get();
if (sdf == null) {
sdf = new SimpleDateFormat(pattern, locale);
sdf.setTimeZone(timeZone);
sdf = FormatUtil.getDateTimeFormat(locale, timeZone, pattern);
datax.put(key, new SoftReference<>(sdf));
}
}
Expand Down
50 changes: 35 additions & 15 deletions core/src/main/java/lucee/commons/i18n/FormatUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.FormatStyle;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -336,16 +337,16 @@ private static void add24AndRemoveComma(List<DateFormat> list, Locale locale, bo
DateFormat[] df = list.toArray(new DateFormat[list.size()]);
for (int i = 0; i < df.length; i++) {
if (df[i] instanceof SimpleDateFormat) {
add24AndRemoveComma(list, (SimpleDateFormat) df[i], locale, isDate, isTime);
add24AndRemoveComma(list, df[i], locale, isDate, isTime);
}
}
}

private static void add24AndRemoveComma(List<DateFormat> list, SimpleDateFormat sdf, Locale locale, boolean isDate, boolean isTime) {
private static void add24AndRemoveComma(List<DateFormat> list, DateFormat sdf, Locale locale, boolean isDate, boolean isTime) {
String p;

List<SimpleDateFormat> results = new ArrayList<SimpleDateFormat>();
p = sdf.toPattern() + "";
List<DateFormat> results = new ArrayList<>();
p = ((SimpleDateFormat) sdf).toPattern() + "";
// print.e("----- "+p);
if (isDate && isTime) {
if ((check(results, p, locale, " 'um' ", " "))) {
Expand Down Expand Up @@ -385,8 +386,8 @@ else if ((check(results, p, locale, "h:mma", "H:mm"))) {
}
}
if (results.size() > 0) {
Iterator<SimpleDateFormat> it = results.iterator();
SimpleDateFormat _sdf;
Iterator<DateFormat> it = results.iterator();
DateFormat _sdf;
while (it.hasNext()) {
_sdf = it.next();
if (!list.contains(_sdf)) {
Expand All @@ -398,11 +399,12 @@ else if ((check(results, p, locale, "h:mma", "H:mm"))) {

}

private static boolean check(List<SimpleDateFormat> results, String orgPattern, Locale locale, String from, String to) {
private static boolean check(List<DateFormat> results, String orgPattern, Locale locale, String from, String to) {
int index = orgPattern.indexOf(from);
if (index != -1) {
String p = StringUtil.replace(orgPattern, from, to, true);
SimpleDateFormat sdf = new SimpleDateFormat(p, locale);

DateFormat sdf = FormatUtil.getDateTimeFormat(locale, null, p);
results.add(sdf);
return true;
}
Expand All @@ -424,11 +426,11 @@ private static void addCustom(List<DateFormat> list, Locale locale, short format
String content = IOUtil.toString(file, (Charset) null);
String[] arr = lucee.runtime.type.util.ListUtil.listToStringArray(content, '\n');
String line;
SimpleDateFormat sdf;
DateFormat sdf;
for (int i = 0; i < arr.length; i++) {
line = arr[i].trim();
if (StringUtil.isEmpty(line)) continue;
sdf = new SimpleDateFormat(line, locale);
sdf = FormatUtil.getDateTimeFormat(locale, null, line);
if (!list.contains(sdf)) list.add(sdf);
}

Expand Down Expand Up @@ -481,7 +483,7 @@ public static DateFormat getDateFormat(Locale locale, TimeZone tz, String mask)
else if (mask.equalsIgnoreCase("long")) df = DateFormat.getDateInstance(DateFormat.LONG, locale);
else if (mask.equalsIgnoreCase("full")) df = DateFormat.getDateInstance(DateFormat.FULL, locale);
else {
df = new SimpleDateFormat(mask, locale);
df = FormatUtil.getDateTimeFormat(locale, null, mask);
}
df.setTimeZone(tz);
return df;
Expand Down Expand Up @@ -518,9 +520,9 @@ public static DateFormat getTimeFormat(Locale locale, TimeZone tz, String mask)
else if (mask.equalsIgnoreCase("long")) df = DateFormat.getTimeInstance(DateFormat.LONG, locale);
else if (mask.equalsIgnoreCase("full")) df = DateFormat.getTimeInstance(DateFormat.FULL, locale);
else {
df = new SimpleDateFormat(mask, locale);
df = locale == null ? new SimpleDateFormat(mask) : new SimpleDateFormat(mask, locale);
}
df.setTimeZone(tz);
if (tz != null) df.setTimeZone(tz);
return df;
}

Expand All @@ -533,10 +535,28 @@ public static DateFormat getDateTimeFormat(Locale locale, TimeZone tz, String ma
else if (mask.equalsIgnoreCase("full")) df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locale);
else if (mask.equalsIgnoreCase("iso8601")) df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
else {
df = new SimpleDateFormat(mask, locale);
df = locale == null ? new SimpleDateFormat(mask) : new SimpleDateFormat(mask, locale);
}
df.setTimeZone(tz);
if (tz != null) df.setTimeZone(tz);
return df;
}

public static DateTimeFormatter getDateTimeFormatter(Locale locale, String mask) {
DateTimeFormatter formatter;
if (mask.equalsIgnoreCase("short")) formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
else if (mask.equalsIgnoreCase("medium")) formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
else if (mask.equalsIgnoreCase("long")) formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
else if (mask.equalsIgnoreCase("full")) formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL);
else if (mask.equalsIgnoreCase("iso8601")) formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ");
else formatter = DateTimeFormatter.ofPattern(mask);

if (locale != null) formatter.withLocale(locale);
// if (tz != null) formatter.setTimeZone(tz);
return formatter;
}

public static String format(DateTimeFormatter formatter, Date date, TimeZone timeZone) {
return date.toInstant().atZone(timeZone != null ? timeZone.toZoneId() : ZoneId.systemDefault()).format(formatter);
}

}
3 changes: 2 additions & 1 deletion core/src/main/java/lucee/commons/io/log/LogUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Date;

import lucee.aprint;
import lucee.commons.i18n.FormatUtil;
import lucee.commons.io.CharsetUtil;
import lucee.commons.io.IOUtil;
import lucee.commons.io.res.Resource;
Expand Down Expand Up @@ -210,7 +211,7 @@ public static void logGlobal(Config config, int level, String type, String msg)
log.getParentResource().mkdirs();
log.createNewFile();
}
IOUtil.write(log, SystemOut.FORMAT.format(new Date(System.currentTimeMillis())) + " " + type + " " + msg + "\n", CharsetUtil.UTF8, true);
IOUtil.write(log, FormatUtil.format(SystemOut.FORMAT, new Date(System.currentTimeMillis()), null) + " " + type + " " + msg + "\n", CharsetUtil.UTF8, true);
}
catch (Exception e) {
ERR = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.layout.AbstractStringLayout;

import lucee.commons.i18n.FormatUtil;
import lucee.commons.io.CharsetUtil;
import lucee.commons.io.log.LogUtil;
import lucee.loader.engine.CFMLEngine;
Expand Down Expand Up @@ -44,7 +44,7 @@ public DataDogLayout() {
super(CharsetUtil.UTF8, new byte[0], new byte[0]);
engine = CFMLEngineFactory.getInstance();
caster = engine.getCastUtil();
format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
format = FormatUtil.getDateTimeFormat(null, null, "yyyy-MM-dd HH:mm:ss");
}

@Override
Expand Down
9 changes: 5 additions & 4 deletions core/src/main/java/lucee/commons/lang/SystemOut.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@

import java.io.PrintStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;

import lucee.commons.i18n.FormatUtil;
import lucee.commons.io.DevNullOutputStream;
import lucee.commons.io.SystemUtil;
import lucee.runtime.PageContext;
Expand All @@ -34,7 +35,7 @@

public final class SystemOut {

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

/**
* logs a value
Expand All @@ -43,13 +44,13 @@ public final class SystemOut {
*/
public static void printDate(PrintWriter pw, String value) {
long millis = System.currentTimeMillis();
pw.write(FORMAT.format(new Date(millis)) + " " + value + "\n");
pw.write(FormatUtil.format(FORMAT, new Date(millis), null) + " " + value + "\n");
pw.flush();
}

public static void printDate(PrintWriter pw, Throwable t) {
long millis = System.currentTimeMillis();
pw.write(FORMAT.format(new Date(millis)) + "\n");
pw.write(FormatUtil.format(FORMAT, new Date(millis), null) + "\n");
t.printStackTrace(pw);
pw.write("\n");
pw.flush();
Expand Down
17 changes: 8 additions & 9 deletions core/src/main/java/lucee/runtime/converter/JSONDateFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
package lucee.runtime.converter;

import java.lang.ref.SoftReference;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;

import lucee.commons.i18n.FormatUtil;
import lucee.commons.io.SystemUtil;
import lucee.commons.lang.SerializableObject;
import lucee.runtime.engine.ThreadLocalPageContext;
Expand All @@ -36,7 +36,7 @@ public class JSONDateFormat {
public static final String PATTERN_CF = "MMMM, dd yyyy HH:mm:ss Z";
public static final String PATTERN_ISO8601 = "yyyy-MM-dd'T'HH:mm:ssZ"; // preferred pattern for json

private static Map<String, SoftReference<DateFormat>> map = new ConcurrentHashMap<String, SoftReference<DateFormat>>();
private static Map<String, SoftReference<DateTimeFormatter>> map = new ConcurrentHashMap<>();
// private static DateFormat format=null;
private static Locale locale = Locale.ENGLISH;
private final static Object sync = new SerializableObject();
Expand All @@ -45,19 +45,18 @@ public static String format(Date date, TimeZone tz, String pattern) {
tz = ThreadLocalPageContext.getTimeZone(tz);

String id = SystemUtil.createToken(locale.hashCode() + "", tz.getID());
SoftReference<DateFormat> tmp = map.get(id);
DateFormat format = tmp == null ? null : tmp.get();
SoftReference<DateTimeFormatter> tmp = map.get(id);
DateTimeFormatter format = tmp == null ? null : tmp.get();
if (format == null) {
synchronized (id) {
tmp = map.get(id);
format = tmp == null ? null : tmp.get();
if (format == null) {
format = new SimpleDateFormat(pattern, locale);
format.setTimeZone(tz);
map.put(id, new SoftReference<DateFormat>(format));
format = FormatUtil.getDateTimeFormatter(locale, pattern);
map.put(id, new SoftReference<DateTimeFormatter>(format));
}
}
}
return format.format(date);
return FormatUtil.format(format, date, tz);
}
}
6 changes: 3 additions & 3 deletions core/src/main/java/lucee/runtime/dump/DumpUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
Expand All @@ -46,6 +46,7 @@
import org.w3c.dom.NodeList;

import lucee.commons.date.TimeZoneUtil;
import lucee.commons.i18n.FormatUtil;
import lucee.commons.io.res.Resource;
import lucee.commons.io.res.util.ResourceUtil;
import lucee.commons.lang.CharSet;
Expand Down Expand Up @@ -116,8 +117,7 @@ public static DumpData toDumpData(Object o, PageContext pageContext, int maxleve
if (o instanceof Calendar) {
Calendar c = (Calendar) o;

SimpleDateFormat df = new SimpleDateFormat("EE, dd MMM yyyy HH:mm:ss zz", Locale.ENGLISH);
df.setTimeZone(c.getTimeZone());
DateFormat df = FormatUtil.getDateTimeFormat(Locale.ENGLISH, c.getTimeZone(), "EE, dd MMM yyyy HH:mm:ss zz");

DumpTable table = new DumpTable("date", "#ff9900", "#ffcc00", "#000000");
table.setTitle("java.util.Calendar");
Expand Down
Loading

0 comments on commit 94e0d4a

Please sign in to comment.