forked from I2P_Developers/i2p.i2p
Util: Consolidate and standardize date/time formatting (ticket #2016)
This commit is contained in:
@@ -26,6 +26,7 @@ import java.io.PrintWriter;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.math.BigInteger;
|
||||
import java.security.MessageDigest;
|
||||
import java.text.DateFormat;
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
@@ -107,6 +108,17 @@ public class DataHelper {
|
||||
private static final Pattern ILLEGAL_KEY = Pattern.compile("[#=\r\n;]");
|
||||
private static final Pattern ILLEGAL_VALUE = Pattern.compile("[#\r\n]");
|
||||
|
||||
/**
|
||||
* The default formatting for date/time, current locale, local time zone
|
||||
* @since 0.9.43
|
||||
*/
|
||||
private static final DateFormat DATE_FORMAT = DateFormat.getDateInstance(DateFormat.MEDIUM);
|
||||
private static final DateFormat TIME_FORMAT = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT);
|
||||
static {
|
||||
DATE_FORMAT.setTimeZone(SystemVersion.getSystemTimeZone());
|
||||
TIME_FORMAT.setTimeZone(SystemVersion.getSystemTimeZone());
|
||||
}
|
||||
|
||||
/** Read a mapping from the stream, as defined by the I2P data structure spec,
|
||||
* and store it into a Properties object.
|
||||
*
|
||||
@@ -1608,6 +1620,34 @@ public class DataHelper {
|
||||
default: return bytes + space;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The default formatting for date, current locale, local time zone.
|
||||
* Warning - NOT UTC!
|
||||
* Examples:
|
||||
* en: Aug 30, 2019
|
||||
* de: 30.08.2019
|
||||
* @since 0.9.43
|
||||
*/
|
||||
public static String formatDate(long now) {
|
||||
synchronized(DATE_FORMAT) {
|
||||
return DATE_FORMAT.format(new Date(now));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The default formatting for date/time, current locale, local time zone.
|
||||
* Warning - NOT UTC!
|
||||
* Examples:
|
||||
* en: Aug 30, 2019 12:38 PM
|
||||
* de: 30.08.2019 12:38
|
||||
* @since 0.9.43
|
||||
*/
|
||||
public static String formatTime(long now) {
|
||||
synchronized(TIME_FORMAT) {
|
||||
return TIME_FORMAT.format(new Date(now));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Strip out any HTML (simply removing any less than / greater than symbols)
|
||||
|
||||
@@ -5,13 +5,12 @@ import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
import net.i2p.I2PAppContext;
|
||||
import net.i2p.data.DataHelper;
|
||||
import net.i2p.util.I2PThread;
|
||||
import net.i2p.util.Log;
|
||||
|
||||
@@ -125,7 +124,6 @@ public class BufferedStatLog implements StatLog {
|
||||
}
|
||||
|
||||
private class StatLogWriter implements Runnable {
|
||||
private final SimpleDateFormat _fmt = new SimpleDateFormat("yyyyMMdd HH:mm:ss.SSS");
|
||||
public void run() {
|
||||
int writeStart = -1;
|
||||
int writeEnd = -1;
|
||||
@@ -162,10 +160,7 @@ public class BufferedStatLog implements StatLog {
|
||||
int cur = start;
|
||||
while (cur != end) {
|
||||
//if (shouldLog(_events[cur].getStat())) {
|
||||
String when = null;
|
||||
synchronized (_fmt) {
|
||||
when = _fmt.format(new Date(_events[cur].getTime()));
|
||||
}
|
||||
String when = DataHelper.formatTime(_events[cur].getTime());
|
||||
_out.write(when);
|
||||
_out.write(" ");
|
||||
if (_events[cur].getScope() == null)
|
||||
|
||||
@@ -503,7 +503,7 @@ public class LogManager implements Flushable {
|
||||
return true;
|
||||
|
||||
try {
|
||||
SimpleDateFormat fmt = (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM);
|
||||
SimpleDateFormat fmt = (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
|
||||
if (!format.equals(""))
|
||||
fmt.applyPattern(format);
|
||||
// the router sets the JVM time zone to UTC but saves the original here so we can get it
|
||||
@@ -722,9 +722,13 @@ public class LogManager implements Flushable {
|
||||
_format = fmt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Any usage of returned formatter must be synchronized!
|
||||
*/
|
||||
public SimpleDateFormat getDateFormat() {
|
||||
return _dateFormat;
|
||||
}
|
||||
|
||||
public String getDateFormatPattern() {
|
||||
return _dateFormatPattern;
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ package net.i2p.util;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import net.i2p.I2PAppContext;
|
||||
@@ -86,7 +87,11 @@ class LogRecordFormatter {
|
||||
}
|
||||
|
||||
public static String getWhen(LogManager manager, LogRecord logRecord) {
|
||||
return manager.getDateFormat().format(new Date(logRecord.getDate()));
|
||||
SimpleDateFormat fmt = manager.getDateFormat();
|
||||
Date d = new Date(logRecord.getDate());
|
||||
synchronized(fmt) {
|
||||
return fmt.format(d);
|
||||
}
|
||||
}
|
||||
|
||||
/** don't translate */
|
||||
|
||||
@@ -2,7 +2,6 @@ package net.i2p.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
@@ -75,19 +74,16 @@ public class SocketTimeout extends SimpleTimer2.TimedEvent {
|
||||
|
||||
public void setTimeoutCommand(Runnable job) { _command = job; }
|
||||
|
||||
private static final SimpleDateFormat _fmt = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss.SSS");
|
||||
private static String ts(long when) { synchronized (_fmt) { return _fmt.format(new Date(when)); } }
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("SocketTimeout started on ");
|
||||
buf.append(ts(_startTime));
|
||||
buf.append(new Date(_startTime));
|
||||
buf.append(" idle for ");
|
||||
buf.append(System.currentTimeMillis() - _lastActivity);
|
||||
buf.append("ms ");
|
||||
if (_totalTimeoutTime > 0)
|
||||
buf.append("total timeout at ").append(ts(_totalTimeoutTime));
|
||||
buf.append("total timeout at ").append(new Date(_totalTimeoutTime));
|
||||
buf.append("cancelled? ").append(_cancelled);
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user