X-Git-Url: https://gitweb.fperrin.net/?a=blobdiff_plain;f=tim%2Fprune%2Fdata%2FTimestamp.java;h=ac144bb8c59e8247deb67438a9f5c8504c258ca9;hb=92dad5df664287acb51728e9ea599f150765d34a;hp=e7a0ab90e3af6e5625bbdc833810e2c60fb5e92b;hpb=f35b6d628f68e3b5ef19965ad8988d0dd1eb8efa;p=GpsPrune.git diff --git a/tim/prune/data/Timestamp.java b/tim/prune/data/Timestamp.java index e7a0ab9..ac144bb 100644 --- a/tim/prune/data/Timestamp.java +++ b/tim/prune/data/Timestamp.java @@ -1,337 +1,193 @@ package tim.prune.data; + import java.text.DateFormat; -import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; -import java.util.Date; -import java.util.regex.Matcher; -import java.util.regex.Pattern; +import java.util.TimeZone; + /** - * Class to hold the timestamp of a track point - * and provide conversion functions + * Superclass of all timestamp implementations */ -public class Timestamp +public abstract class Timestamp { - private boolean _valid = false; - private long _seconds = 0L; - private String _text = null; - private String _timeText = null; - - private static final DateFormat DEFAULT_DATE_FORMAT = DateFormat.getDateTimeInstance(); + private static final DateFormat DEFAULT_DATE_FORMAT = DateFormat.getDateInstance(); private static final DateFormat DEFAULT_TIME_FORMAT = DateFormat.getTimeInstance(); - private static final DateFormat ISO_8601_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'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 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; - /** Specifies original timestamp format */ - public static final int FORMAT_ORIGINAL = 0; - /** Specifies locale-dependent timestamp format */ - public static final int FORMAT_LOCALE = 1; - /** Specifies ISO 8601 timestamp format */ - public static final int FORMAT_ISO_8601 = 2; + protected static final DateFormat DEFAULT_DATETIME_FORMAT = DateFormat.getDateTimeInstance(); - // Static block to initialise offsets - static - { - CALENDAR = Calendar.getInstance(); - 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; - // Date formats - ALL_DATE_FORMATS = new DateFormat[] { - DEFAULT_DATE_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"), - ISO_8601_FORMAT, ISO_8601_FORMAT_NOZ - }; - } + protected static final DateFormat ISO_8601_FORMAT + = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); + protected static final DateFormat ISO_8601_FORMAT_WITH_MILLIS + = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); - - /** - * Constructor - * @param inString String containing timestamp - */ - public Timestamp(String inString) - { - // TODO: Does it really help to store timestamps in seconds rather than ms? - if (inString != null && !inString.equals("")) - { - // Try to parse into a long - try - { - long rawValue = Long.parseLong(inString.trim()); - _seconds = getSeconds(rawValue); - _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 inOther._seconds; + return getMillisecondsSince(inOther) > 0; } /** - * Calculate the difference between two Timestamps in seconds - * @param inOther other, earlier Timestamp - * @return number of seconds since other timestamp + * @return true if this timestamp is before the other one */ - public long getSecondsSince(Timestamp inOther) + public boolean isBefore(Timestamp inOther) { - return _seconds - inOther._seconds; + return getMillisecondsSince(inOther) < 0; } /** - * Add the given number of seconds offset - * @param inOffset number of seconds to add/subtract + * @return true if this timestamp is equal to the other one */ - public void addOffset(long inOffset) + public boolean isEqual(Timestamp inOther) { - _seconds += inOffset; - _text = null; + return getMillisecondsSince(inOther) == 0; } /** - * Add the given TimeDifference to this Timestamp - * @param inOffset TimeDifference to add - * @return new Timestamp object + * @return the number of seconds since the other timestamp */ - public Timestamp createPlusOffset(TimeDifference inOffset) + public long getSecondsSince(Timestamp inOther) { - return createPlusOffset(inOffset.getTotalSeconds()); + return getMillisecondsSince(inOther) / 1000L; } /** - * Add the given number of seconds to this Timestamp - * @param inSeconds number of seconds to add - * @return new Timestamp object + * Calculate the difference between two Timestamps in milliseconds + * @param inOther other, earlier Timestamp + * @return number of milliseconds since other timestamp */ - public Timestamp createPlusOffset(long inSeconds) + public long getMillisecondsSince(Timestamp inOther) { - return new Timestamp((_seconds + inSeconds) * 1000L); + return getMilliseconds(null) - inOther.getMilliseconds(null); } - /** - * Subtract the given TimeDifference from this Timestamp - * @param inOffset TimeDifference to subtract - * @return new Timestamp object + * @return the number of seconds since the other timestamp using the given timezone */ - public Timestamp createMinusOffset(TimeDifference inOffset) + public long getSecondsSince(Timestamp inOther, TimeZone inTimezone) { - return new Timestamp((_seconds - inOffset.getTotalSeconds()) * 1000L); + return (getMilliseconds(inTimezone) - inOther.getMilliseconds(inTimezone)) / 1000L; } + /** + * Add the given number of seconds offset + * @param inOffset number of seconds to add/subtract + */ + public abstract void addOffsetSeconds(long inOffset); /** - * @return Description of timestamp in locale-specific format + * @return true if the timestamp has non-zero milliseconds */ - public String getText() - { - return getText(FORMAT_LOCALE); - } + protected abstract boolean hasMilliseconds(); + /** - * @param inFormat format of timestamp - * @return Description of timestamp in required format + * @return date part of timestamp in locale-specific format */ - public String getText(int inFormat) + public String getDateText(TimeZone inTimezone) { - if (!_valid) {return "";} - if (inFormat == FORMAT_ISO_8601) { - return format(ISO_8601_FORMAT); - } - if (_text == null) { - _text = (_valid?format(DEFAULT_DATE_FORMAT):""); - } - return _text; + if (!isValid()) return ""; + return format(DEFAULT_DATE_FORMAT, inTimezone); } /** * @return Description of time part of timestamp in locale-specific format */ - public String getTimeText() + public String getTimeText(TimeZone inTimezone) { - if (_timeText == null) + if (!isValid()) return ""; + // Maybe we should add milliseconds to this format? + if (hasMilliseconds() && !MillisAddedToTimeFormat) { - if (_valid) { - _timeText = format(DEFAULT_TIME_FORMAT); + try + { + 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 _timeText = ""; + catch (ClassCastException cce) {} } - return _timeText; + return format(DEFAULT_TIME_FORMAT, inTimezone); } /** * Utility method for formatting dates / times - * @param inFormat formatter object - * @return formatted String */ - private String format(DateFormat inFormat) + protected abstract String format(DateFormat inFormat, TimeZone inTimezone); + + + /** + * @return Description of timestamp in locale-specific format + */ + public String getText(TimeZone inTimezone) { - CALENDAR.setTimeInMillis(_seconds * 1000L); - return inFormat.format(CALENDAR.getTime()); + return getText(Format.LOCALE, inTimezone); } /** - * @return a Calendar object representing this timestamp + * @param inFormat format of timestamp + * @return Description of timestamp in required format */ - public Calendar getCalendar() + public String getText(Format inFormat, TimeZone inTimezone) { - Calendar cal = Calendar.getInstance(); - cal.setTimeInMillis(_seconds * 1000L); - return cal; + if (!isValid()) { + return ""; + } + switch (inFormat) + { + case ORIGINAL: + case LOCALE: + default: + return format(DEFAULT_DATETIME_FORMAT, inTimezone); + case ISO8601: + return format(hasMilliseconds() ? ISO_8601_FORMAT_WITH_MILLIS : ISO_8601_FORMAT, + inTimezone); + } } + }