From 5548bf13796fe08b32ebf95ed4c47382382ab721 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 12 Feb 2013 21:11:16 +1300 Subject: Improve timezone handling --- .../traccar/protocol/Gps103ProtocolDecoder.java | 26 +++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/protocol/Gps103ProtocolDecoder.java b/src/org/traccar/protocol/Gps103ProtocolDecoder.java index e79ae109f..8980c702f 100644 --- a/src/org/traccar/protocol/Gps103ProtocolDecoder.java +++ b/src/org/traccar/protocol/Gps103ProtocolDecoder.java @@ -45,10 +45,11 @@ public class Gps103ProtocolDecoder extends BaseProtocolDecoder { "imei:" + "(\\d+)," + // IMEI "([^,]+)," + // Alarm - "(\\d{2})(\\d{2})(\\d{2})\\d+," + // Date + "(\\d{2})(\\d{2})(\\d{2})" + // Local Date + "(\\d{2})(\\d{2})," + // Local Time "[^,]*," + "[FL]," + // F - full / L - low - "(\\d{2})(\\d{2})(\\d{2})\\.(\\d{3})," + // Time (HHMMSS.SSS) + "(\\d{2})(\\d{2})(\\d{2})\\.(\\d{3})," + // Time UTC (HHMMSS.SSS) "([AV])," + // Validity "(\\d{2})(\\d{2}\\.\\d{4})," + // Latitude (DDMM.MMMM) "([NS])," + @@ -106,19 +107,34 @@ public class Gps103ProtocolDecoder extends BaseProtocolDecoder { extendedInfo.append(""); extendedInfo.append(parser.group(index++)); extendedInfo.append(""); - + // Date Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC")); time.clear(); time.set(Calendar.YEAR, 2000 + Integer.valueOf(parser.group(index++))); time.set(Calendar.MONTH, Integer.valueOf(parser.group(index++)) - 1); time.set(Calendar.DAY_OF_MONTH, Integer.valueOf(parser.group(index++))); + + int localHours = Integer.valueOf(parser.group(index++)); + int localMinutes = Integer.valueOf(parser.group(index++)); + + int utcHours = Integer.valueOf(parser.group(index++)); + int utcMinutes = Integer.valueOf(parser.group(index++)); // Time - time.set(Calendar.HOUR, Integer.valueOf(parser.group(index++))); - time.set(Calendar.MINUTE, Integer.valueOf(parser.group(index++))); + time.set(Calendar.HOUR, localHours); + time.set(Calendar.MINUTE, localMinutes); time.set(Calendar.SECOND, Integer.valueOf(parser.group(index++))); time.set(Calendar.MILLISECOND, Integer.valueOf(parser.group(index++))); + + // Timezone calculation + int deltaMinutes = (localHours - utcHours) * 60 + localMinutes - utcMinutes; + if (deltaMinutes <= -12 * 60) { + deltaMinutes += 24 * 60; + } else if (deltaMinutes > 12) { + deltaMinutes -= 24 * 60; + } + time.add(Calendar.MINUTE, deltaMinutes); position.setTime(time.getTime()); // Validity -- cgit v1.2.3