aboutsummaryrefslogtreecommitdiff
path: root/src/org
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2018-11-08 13:37:38 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2018-11-08 13:37:38 +1300
commit9c9370f72c51d7466d3b25a59264ee98f4319b3a (patch)
tree1fca14e8a624e79f86b913dc60f94a93513a8eb0 /src/org
parent81d2b86ac7cca874b32c94fffa28b902c6fb0eca (diff)
downloadtraccar-server-9c9370f72c51d7466d3b25a59264ee98f4319b3a.tar.gz
traccar-server-9c9370f72c51d7466d3b25a59264ee98f4319b3a.tar.bz2
traccar-server-9c9370f72c51d7466d3b25a59264ee98f4319b3a.zip
Fix time formatting issues
Diffstat (limited to 'src/org')
-rw-r--r--src/org/traccar/Context.java6
-rw-r--r--src/org/traccar/MainEventHandler.java5
-rw-r--r--src/org/traccar/database/DataManager.java5
-rw-r--r--src/org/traccar/database/StatisticsManager.java3
-rw-r--r--src/org/traccar/helper/DateUtil.java19
-rw-r--r--src/org/traccar/protocol/OsmAndProtocolDecoder.java6
-rw-r--r--src/org/traccar/web/CsvBuilder.java3
-rw-r--r--src/org/traccar/web/GpxBuilder.java4
8 files changed, 28 insertions, 23 deletions
diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java
index 3c3dac874..6e255b6d3 100644
--- a/src/org/traccar/Context.java
+++ b/src/org/traccar/Context.java
@@ -20,8 +20,6 @@ import com.fasterxml.jackson.databind.SerializationFeature;
import java.net.InetAddress;
import java.net.UnknownHostException;
-import java.time.ZoneId;
-import java.time.format.DateTimeFormatter;
import java.util.Properties;
import com.fasterxml.jackson.datatype.jsr353.JSR353Module;
@@ -96,10 +94,6 @@ public final class Context {
private static final Logger LOGGER = LoggerFactory.getLogger(Context.class);
- public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
- public static final DateTimeFormatter DATE_FORMATTER =
- DateTimeFormatter.ISO_OFFSET_DATE_TIME.withZone(ZoneId.systemDefault());
-
private Context() {
}
diff --git a/src/org/traccar/MainEventHandler.java b/src/org/traccar/MainEventHandler.java
index 311a41f34..3032646c1 100644
--- a/src/org/traccar/MainEventHandler.java
+++ b/src/org/traccar/MainEventHandler.java
@@ -23,10 +23,10 @@ import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.timeout.IdleStateEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.traccar.helper.DateUtil;
import org.traccar.model.Position;
import java.sql.SQLException;
-import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
@@ -61,8 +61,7 @@ public class MainEventHandler extends ChannelInboundHandlerAdapter {
StringBuilder s = new StringBuilder();
s.append(formatChannel(ctx.channel())).append(" ");
s.append("id: ").append(uniqueId);
- s.append(", time: ").append(
- new SimpleDateFormat(Context.DATE_FORMAT).format(position.getFixTime()));
+ s.append(", time: ").append(DateUtil.formatDate(position.getFixTime(), false));
s.append(", lat: ").append(String.format("%.5f", position.getLatitude()));
s.append(", lon: ").append(String.format("%.5f", position.getLongitude()));
if (position.getSpeed() > 0) {
diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java
index 65f67ce44..8da6f2cca 100644
--- a/src/org/traccar/database/DataManager.java
+++ b/src/org/traccar/database/DataManager.java
@@ -20,7 +20,6 @@ import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.sql.SQLException;
-import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
@@ -42,6 +41,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.traccar.Config;
import org.traccar.Context;
+import org.traccar.helper.DateUtil;
import org.traccar.model.Attribute;
import org.traccar.model.Device;
import org.traccar.model.Driver;
@@ -363,8 +363,7 @@ public class DataManager {
long historyDays = config.getInteger("database.historyDays");
if (historyDays != 0) {
Date timeLimit = new Date(System.currentTimeMillis() - historyDays * 24 * 3600 * 1000);
- LOGGER.info(
- "Clearing history earlier than " + new SimpleDateFormat(Context.DATE_FORMAT).format(timeLimit));
+ LOGGER.info("Clearing history earlier than " + DateUtil.formatDate(timeLimit, false));
QueryBuilder.create(dataSource, getQuery("database.deletePositions"))
.setDate("serverTime", timeLimit)
.executeUpdate();
diff --git a/src/org/traccar/database/StatisticsManager.java b/src/org/traccar/database/StatisticsManager.java
index 735c1f1a7..4e656bfd8 100644
--- a/src/org/traccar/database/StatisticsManager.java
+++ b/src/org/traccar/database/StatisticsManager.java
@@ -18,6 +18,7 @@ package org.traccar.database;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.traccar.Context;
+import org.traccar.helper.DateUtil;
import org.traccar.model.Statistics;
import javax.ws.rs.client.Entity;
@@ -71,7 +72,7 @@ public class StatisticsManager {
String url = Context.getConfig().getString("server.statistics");
if (url != null) {
- String time = Context.DATE_FORMATTER.format(statistics.getCaptureTime().toInstant());
+ String time = DateUtil.formatDate(statistics.getCaptureTime());
Form form = new Form();
form.param("version", Context.getAppVersion());
diff --git a/src/org/traccar/helper/DateUtil.java b/src/org/traccar/helper/DateUtil.java
index e09ac6d0e..20a483e3c 100644
--- a/src/org/traccar/helper/DateUtil.java
+++ b/src/org/traccar/helper/DateUtil.java
@@ -15,9 +15,10 @@
*/
package org.traccar.helper;
-import org.traccar.Context;
-
+import java.text.SimpleDateFormat;
import java.time.Instant;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
@@ -59,7 +60,19 @@ public final class DateUtil {
}
public static Date parseDate(String value) {
- return Date.from(Instant.from(Context.DATE_FORMATTER.parse(value)));
+ return Date.from(Instant.from(DateTimeFormatter.ISO_ZONED_DATE_TIME.parse(value)));
+ }
+
+ public static String formatDate(Date date) {
+ return formatDate(date, true);
+ }
+
+ public static String formatDate(Date date, boolean zoned) {
+ if (zoned) {
+ return DateTimeFormatter.ISO_OFFSET_DATE_TIME.withZone(ZoneId.systemDefault()).format(date.toInstant());
+ } else {
+ return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
+ }
}
}
diff --git a/src/org/traccar/protocol/OsmAndProtocolDecoder.java b/src/org/traccar/protocol/OsmAndProtocolDecoder.java
index e2aaeb7b6..3bc71de81 100644
--- a/src/org/traccar/protocol/OsmAndProtocolDecoder.java
+++ b/src/org/traccar/protocol/OsmAndProtocolDecoder.java
@@ -20,9 +20,9 @@ import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.QueryStringDecoder;
import org.traccar.BaseHttpProtocolDecoder;
-import org.traccar.Context;
import org.traccar.DeviceSession;
import org.traccar.Protocol;
+import org.traccar.helper.DateUtil;
import org.traccar.model.CellTower;
import org.traccar.model.Network;
import org.traccar.model.Position;
@@ -32,7 +32,6 @@ import java.net.SocketAddress;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
-import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.Map;
@@ -83,8 +82,7 @@ public class OsmAndProtocolDecoder extends BaseHttpProtocolDecoder {
position.setTime(new Date(timestamp));
} catch (NumberFormatException error) {
if (value.contains("T")) {
- position.setTime(
- Date.from(Instant.from(Context.DATE_FORMATTER.parse(value))));
+ position.setTime(DateUtil.parseDate(value));
} else {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
position.setTime(dateFormat.parse(value));
diff --git a/src/org/traccar/web/CsvBuilder.java b/src/org/traccar/web/CsvBuilder.java
index cd4efea9f..3fe7e408f 100644
--- a/src/org/traccar/web/CsvBuilder.java
+++ b/src/org/traccar/web/CsvBuilder.java
@@ -31,6 +31,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.traccar.Context;
+import org.traccar.helper.DateUtil;
public class CsvBuilder {
@@ -90,7 +91,7 @@ public class CsvBuilder {
addSeparator();
} else if (method.getReturnType().equals(Date.class)) {
Date value = (Date) method.invoke(object);
- builder.append(Context.DATE_FORMATTER.format(value.toInstant()));
+ builder.append(DateUtil.formatDate(value));
addSeparator();
} else if (method.getReturnType().equals(Map.class)) {
Map value = (Map) method.invoke(object);
diff --git a/src/org/traccar/web/GpxBuilder.java b/src/org/traccar/web/GpxBuilder.java
index 41f406eb7..638d100e5 100644
--- a/src/org/traccar/web/GpxBuilder.java
+++ b/src/org/traccar/web/GpxBuilder.java
@@ -18,7 +18,7 @@ package org.traccar.web;
import java.util.Collection;
-import org.traccar.Context;
+import org.traccar.helper.DateUtil;
import org.traccar.helper.UnitsConverter;
import org.traccar.model.Position;
@@ -51,7 +51,7 @@ public class GpxBuilder {
public void addPosition(Position position) {
builder.append(String.format(POINT, position.getLatitude(), position.getLongitude(),
- Context.DATE_FORMATTER.format(position.getFixTime().toInstant()), position.getAltitude(),
+ DateUtil.formatDate(position.getFixTime()), position.getAltitude(),
position.getCourse(), UnitsConverter.mpsFromKnots(position.getSpeed())));
}