X-Git-Url: https://gitweb.fperrin.net/?a=blobdiff_plain;f=tim%2Fprune%2Fdata%2FTimestamp.java;h=623d80497eb995e36073e31e6067926192e9c626;hb=7f5ed2be62905bd37717376dc22d09e5ea7edb4d;hp=8de7c4d7d76e62c5b1041d497a6139613be14f24;hpb=140e9d165f85c3d4f0435a311e091209313faa2a;p=GpsPrune.git diff --git a/tim/prune/data/Timestamp.java b/tim/prune/data/Timestamp.java index 8de7c4d..623d804 100644 --- a/tim/prune/data/Timestamp.java +++ b/tim/prune/data/Timestamp.java @@ -1,10 +1,11 @@ package tim.prune.data; import java.text.DateFormat; -import java.text.ParseException; +import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; +import java.util.TimeZone; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -15,7 +16,7 @@ import java.util.regex.Pattern; public class Timestamp { private boolean _valid = false; - private long _seconds = 0L; + private long _milliseconds = 0L; private String _text = null; private String _timeText = null; @@ -25,9 +26,11 @@ public class Timestamp private static final DateFormat ISO_8601_FORMAT_NOZ = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); private static DateFormat[] ALL_DATE_FORMATS = null; private static Calendar CALENDAR = null; + private static final Pattern ISO8601_FRACTIONAL_PATTERN + = Pattern.compile("(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2})(?:[\\.,](\\d{1,3}))?(Z|[\\+-]\\d{2}(?::?\\d{2})?)?"); + // year month day T hour minute sec millisec Z or +/- hours : minutes private static final Pattern GENERAL_TIMESTAMP_PATTERN = Pattern.compile("(\\d{4})\\D(\\d{2})\\D(\\d{2})\\D(\\d{2})\\D(\\d{2})\\D(\\d{2})"); - private static Matcher GENERAL_TIMESTAMP_MATCHER = null; private static long SECS_SINCE_1970 = 0L; private static long SECS_SINCE_GARTRIP = 0L; private static long MSECS_SINCE_1970 = 0L; @@ -42,16 +45,42 @@ public class Timestamp /** Specifies ISO 8601 timestamp format */ public static final int FORMAT_ISO_8601 = 2; + /** Identifier for the parsing strategy to use */ + private enum ParseType + { + NONE, + ISO8601_FRACTIONAL, + LONG, + FIXED_FORMAT0, + FIXED_FORMAT1, + FIXED_FORMAT2, + FIXED_FORMAT3, + FIXED_FORMAT4, + FIXED_FORMAT5, + FIXED_FORMAT6, + GENERAL_STRING + } + + /** Array of parse types to loop through (first one is changed to last successful type) */ + private static ParseType[] ALL_PARSE_TYPES = {ParseType.NONE, ParseType.ISO8601_FRACTIONAL, ParseType.LONG, + ParseType.FIXED_FORMAT0, ParseType.FIXED_FORMAT1, ParseType.FIXED_FORMAT2, ParseType.FIXED_FORMAT3, + ParseType.FIXED_FORMAT4, ParseType.FIXED_FORMAT5, ParseType.FIXED_FORMAT6, ParseType.GENERAL_STRING}; + // Static block to initialise offsets static { CALENDAR = Calendar.getInstance(); + TimeZone gmtZone = TimeZone.getTimeZone("GMT"); + CALENDAR.setTimeZone(gmtZone); MSECS_SINCE_1970 = CALENDAR.getTimeInMillis(); SECS_SINCE_1970 = MSECS_SINCE_1970 / 1000L; SECS_SINCE_GARTRIP = SECS_SINCE_1970 - GARTRIP_OFFSET; CALENDAR.add(Calendar.YEAR, -20); MSECS_SINCE_1990 = CALENDAR.getTimeInMillis(); TWENTY_YEARS_IN_SECS = (MSECS_SINCE_1970 - MSECS_SINCE_1990) / 1000L; + // Set timezone for output + ISO_8601_FORMAT.setTimeZone(gmtZone); + DEFAULT_DATE_FORMAT.setTimeZone(gmtZone); // Date formats ALL_DATE_FORMATS = new DateFormat[] { DEFAULT_DATE_FORMAT, @@ -70,49 +99,119 @@ public class Timestamp */ public Timestamp(String inString) { - // TODO: Does it really help to store timestamps in seconds rather than ms? + _valid = false; if (inString != null && !inString.equals("")) { - // Try to parse into a long - try + // Try each of the parse types in turn + for (ParseType type : ALL_PARSE_TYPES) { - long rawValue = Long.parseLong(inString.trim()); - _seconds = getSeconds(rawValue); - _valid = true; + if (parseString(inString, type)) + { + ALL_PARSE_TYPES[0] = type; + _valid = true; + return; + } } - catch (NumberFormatException nfe) - { - // String is not a long, so try a date/time string instead - // try each of the date formatters in turn - Date date = null; - for (int i=0; i inOther._seconds; + return getSecondsSince(inOther) > 0L; } /** @@ -229,7 +354,35 @@ public class Timestamp */ public long getSecondsSince(Timestamp inOther) { - return _seconds - inOther._seconds; + return (_milliseconds - inOther._milliseconds) / 1000L; + } + + /** + * Calculate the difference between two Timestamps in milliseconds + * @param inOther other, earlier Timestamp + * @return number of millisseconds since other timestamp + */ + public long getMillisecondsSince(Timestamp inOther) + { + return _milliseconds - inOther._milliseconds; + } + + /** + * @param inOther other timestamp to compare + * @return true if they're equal to the nearest second + */ + public boolean isEqual(Timestamp inOther) + { + return getSecondsSince(inOther) == 0L; + } + + /** + * @param inOther other Timestamp + * @return true if this one is before the other + */ + public boolean isBefore(Timestamp inOther) + { + return getSecondsSince(inOther) < 0L; } /** @@ -238,7 +391,7 @@ public class Timestamp */ public void addOffset(long inOffset) { - _seconds += inOffset; + _milliseconds += (inOffset * 1000L); _text = null; } @@ -249,7 +402,17 @@ public class Timestamp */ public Timestamp createPlusOffset(TimeDifference inOffset) { - return new Timestamp((_seconds + inOffset.getTotalSeconds()) * 1000L); + return createPlusOffset(inOffset.getTotalSeconds()); + } + + /** + * Add the given number of seconds to this Timestamp + * @param inSeconds number of seconds to add + * @return new Timestamp object + */ + public Timestamp createPlusOffset(long inSeconds) + { + return new Timestamp(_milliseconds + (inSeconds * 1000L)); } @@ -260,7 +423,7 @@ public class Timestamp */ public Timestamp createMinusOffset(TimeDifference inOffset) { - return new Timestamp((_seconds - inOffset.getTotalSeconds()) * 1000L); + return new Timestamp(_milliseconds - (inOffset.getTotalSeconds() * 1000L)); } @@ -283,7 +446,7 @@ public class Timestamp return format(ISO_8601_FORMAT); } if (_text == null) { - _text = (_valid?format(DEFAULT_DATE_FORMAT):""); + _text = format(DEFAULT_DATE_FORMAT); } return _text; } @@ -310,7 +473,8 @@ public class Timestamp */ private String format(DateFormat inFormat) { - CALENDAR.setTimeInMillis(_seconds * 1000L); + CALENDAR.setTimeZone(TimeZone.getTimeZone("GMT")); + CALENDAR.setTimeInMillis(_milliseconds); return inFormat.format(CALENDAR.getTime()); } @@ -320,7 +484,8 @@ public class Timestamp public Calendar getCalendar() { Calendar cal = Calendar.getInstance(); - cal.setTimeInMillis(_seconds * 1000L); + cal.setTimeZone(TimeZone.getTimeZone("GMT")); + cal.setTimeInMillis(_milliseconds); return cal; } }