X-Git-Url: http://gitweb.fperrin.net/?a=blobdiff_plain;f=tim%2Fprune%2Fdata%2FTimestamp.java;h=d5e065d4371d197c177a09f514498f93f71b6109;hb=ff33ebba6b7c62834f6dae16ce33eb2c710b160e;hp=965853ad036e5b61a44fe1dc58af87154bfc7df0;hpb=312fec956e43f5d0a38617da5d0add9c62563e2c;p=GpsPrune.git diff --git a/tim/prune/data/Timestamp.java b/tim/prune/data/Timestamp.java index 965853a..d5e065d 100644 --- a/tim/prune/data/Timestamp.java +++ b/tim/prune/data/Timestamp.java @@ -1,10 +1,13 @@ 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; /** * Class to hold the timestamp of a track point @@ -13,103 +16,334 @@ import java.util.Date; public class Timestamp { private boolean _valid = false; - private long _seconds = 0L; + private long _milliseconds = 0L; private String _text = null; - private static DateFormat DEFAULT_DATE_FORMAT = DateFormat.getDateTimeInstance(); + private static final DateFormat DEFAULT_DATETIME_FORMAT = DateFormat.getDateTimeInstance(); + private static final DateFormat DEFAULT_DATE_FORMAT = DateFormat.getDateInstance(); + private static final DateFormat DEFAULT_TIME_FORMAT = DateFormat.getTimeInstance(); + private static boolean MillisAddedToTimeFormat = false; + private static final DateFormat ISO_8601_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); + private static final DateFormat ISO_8601_FORMAT_WITH_MILLIS = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); + 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 long SECS_SINCE_1970 = 0L; private static long SECS_SINCE_GARTRIP = 0L; private static long MSECS_SINCE_1970 = 0L; private static long MSECS_SINCE_1990 = 0L; private static long TWENTY_YEARS_IN_SECS = 0L; - private static final long GARTRIP_OFFSET = 631065600L; + /** Possible formats for parsing and displaying timestamps */ + public enum Format + { + ORIGINAL, + LOCALE, + ISO8601 + } + + /** 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, + FIXED_FORMAT7, + FIXED_FORMAT8, + 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.FIXED_FORMAT7, + ParseType.FIXED_FORMAT8, 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); + ISO_8601_FORMAT_WITH_MILLIS.setTimeZone(gmtZone); + DEFAULT_DATETIME_FORMAT.setTimeZone(gmtZone); // Date formats ALL_DATE_FORMATS = new DateFormat[] { - DEFAULT_DATE_FORMAT, + DEFAULT_DATETIME_FORMAT, new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy"), new SimpleDateFormat("HH:mm:ss dd MMM yyyy"), new SimpleDateFormat("dd MMM yyyy HH:mm:ss"), - new SimpleDateFormat("yyyy MMM dd HH:mm:ss") + new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss"), + new SimpleDateFormat("yyyy MMM dd HH:mm:ss"), + new SimpleDateFormat("MMM dd, yyyy hh:mm:ss aa"), + ISO_8601_FORMAT, ISO_8601_FORMAT_NOZ }; + for (DateFormat df : ALL_DATE_FORMATS) { + df.setLenient(false); + } } /** * Constructor + * @param inString String containing timestamp */ public Timestamp(String inString) { + _valid = false; + _text = null; 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()); - // check for each format possibility and pick nearest - long diff1 = Math.abs(SECS_SINCE_1970 - rawValue); - long diff2 = Math.abs(MSECS_SINCE_1970 - rawValue); - long diff3 = Math.abs(MSECS_SINCE_1990 - rawValue); - long diff4 = Math.abs(SECS_SINCE_GARTRIP - rawValue); - - // Start off with "seconds since 1970" format - long smallestDiff = diff1; - _seconds = rawValue; - // Now check millis since 1970 - if (diff2 < smallestDiff) + if (parseString(inString, type)) { - // milliseconds since 1970 - _seconds = rawValue / 1000L; - smallestDiff = diff2; + ALL_PARSE_TYPES[0] = type; + _valid = true; + _text = inString; + return; } - // Now millis since 1990 - if (diff3 < smallestDiff) + } + } + } + + /** + * Try to parse the given string in the specified way + * @param inString String to parse + * @param inType parse type to use + * @return true if successful + */ + private boolean parseString(String inString, ParseType inType) + { + if (inString == null || inString.equals("")) { + return false; + } + switch (inType) + { + case NONE: return false; + case LONG: + // Try to parse into a long + try { - // milliseconds since 1990 - _seconds = rawValue / 1000L + TWENTY_YEARS_IN_SECS; - smallestDiff = diff3; + long rawValue = Long.parseLong(inString.trim()); + _milliseconds = getMilliseconds(rawValue); + return true; } - // Lastly, check garmin offset - if (diff4 < smallestDiff) + catch (NumberFormatException nfe) + {} + break; + + case ISO8601_FRACTIONAL: + final Matcher fmatcher = ISO8601_FRACTIONAL_PATTERN.matcher(inString); + if (fmatcher.matches()) { - // milliseconds since garmin offset - _seconds = rawValue + GARTRIP_OFFSET; + try { + _milliseconds = getMilliseconds(Integer.parseInt(fmatcher.group(1)), // year + Integer.parseInt(fmatcher.group(2)), // month + Integer.parseInt(fmatcher.group(3)), // day + Integer.parseInt(fmatcher.group(4)), // hour + Integer.parseInt(fmatcher.group(5)), // minute + Integer.parseInt(fmatcher.group(6)), // second + fmatcher.group(7), // fractional seconds + fmatcher.group(8)); // timezone, if any + return true; + } + catch (NumberFormatException nfe) {} } - _valid = true; - } - 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 0; + } + /** + * @param inOther other Timestamp + * @return true if this one is at least a millisecond after the other + */ + public boolean isAfter(Timestamp inOther) + { + return getMillisecondsSince(inOther) > 0L; + } /** * Calculate the difference between two Timestamps in seconds @@ -127,7 +376,76 @@ 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 millisecond + */ + public boolean isEqual(Timestamp inOther) + { + return inOther != null && _milliseconds == inOther._milliseconds; + } + + /** + * @param inOther other Timestamp + * @return true if this one is before the other + */ + public boolean isBefore(Timestamp inOther) + { + return getMillisecondsSince(inOther) < 0L; + } + + /** + * Add the given number of seconds offset + * @param inOffset number of seconds to add/subtract + */ + public void addOffset(long inOffset) + { + _milliseconds += (inOffset * 1000L); + _text = null; + } + + /** + * Add the given TimeDifference to this Timestamp + * @param inOffset TimeDifference to add + * @return new Timestamp object + */ + public Timestamp createPlusOffset(TimeDifference inOffset) + { + 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)); + } + + + /** + * Subtract the given TimeDifference from this Timestamp + * @param inOffset TimeDifference to subtract + * @return new Timestamp object + */ + public Timestamp createMinusOffset(TimeDifference inOffset) + { + return new Timestamp(_milliseconds - (inOffset.getTotalSeconds() * 1000L)); } @@ -136,15 +454,83 @@ public class Timestamp */ public String getText() { - if (_text == null) + return getText(Format.LOCALE); + } + + /** + * @param inFormat format of timestamp + * @return Description of timestamp in required format + */ + public String getText(Format inFormat) + { + if (!_valid) {return "";} + switch (inFormat) + { + case ORIGINAL: + if (_text != null) {return _text;} + // otherwise fallthrough to default + //$FALL-THROUGH$ + case LOCALE: + return format(DEFAULT_DATETIME_FORMAT); + case ISO8601: + return format(hasMilliseconds() ? ISO_8601_FORMAT_WITH_MILLIS : ISO_8601_FORMAT); + } + return _text; + } + + /** + * @return date part of timestamp in locale-specific format + */ + public String getDateText() + { + if (!_valid) return ""; + return format(DEFAULT_DATE_FORMAT); + } + + /** + * @return Description of time part of timestamp in locale-specific format + */ + public String getTimeText() + { + if (!_valid) return ""; + // Maybe we should add milliseconds to this format? + if (hasMilliseconds() && !MillisAddedToTimeFormat) { - if (_valid) + try { - CALENDAR.setTimeInMillis(_seconds * 1000L); - _text = DEFAULT_DATE_FORMAT.format(CALENDAR.getTime()); + SimpleDateFormat sdf = (SimpleDateFormat) DEFAULT_TIME_FORMAT; + String pattern = sdf.toPattern(); + if (pattern.indexOf("ss") > 0 && pattern.indexOf("SS") < 0) + { + sdf.applyPattern(pattern.replaceFirst("s+", "$0.SSS")); + MillisAddedToTimeFormat = true; + } } - else _text = ""; + catch (ClassCastException cce) {} } - return _text; + return format(DEFAULT_TIME_FORMAT); + } + + /** + * Utility method for formatting dates / times + * @param inFormat formatter object + * @return formatted String + */ + private String format(DateFormat inFormat) + { + CALENDAR.setTimeZone(TimeZone.getTimeZone("GMT")); + CALENDAR.setTimeInMillis(_milliseconds); + return inFormat.format(CALENDAR.getTime()); + } + + /** + * @return a Calendar object representing this timestamp + */ + public Calendar getCalendar() + { + Calendar cal = Calendar.getInstance(); + cal.setTimeZone(TimeZone.getTimeZone("GMT")); + cal.setTimeInMillis(_milliseconds); + return cal; } }