]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/data/Timestamp.java
Version 19, May 2018
[GpsPrune.git] / tim / prune / data / Timestamp.java
index ddb380818fc45d091c92ccd57111b5ae40d34eee..ac144bb8c59e8247deb67438a9f5c8504c258ca9 100644 (file)
 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.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 static DateFormat DEFAULT_DATE_FORMAT = DateFormat.getDateTimeInstance();
-       private static DateFormat[] ALL_DATE_FORMATS = null;
-       private static Calendar CALENDAR = null;
-       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;
-
-       // Static block to initialise offsets
+       private static final DateFormat DEFAULT_DATE_FORMAT = DateFormat.getDateInstance();
+       private static final DateFormat DEFAULT_TIME_FORMAT = DateFormat.getTimeInstance();
+
+       protected static final DateFormat DEFAULT_DATETIME_FORMAT = DateFormat.getDateTimeInstance();
+
+       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'");
+
+       private static boolean MillisAddedToTimeFormat = false;
+
+
+       /** Possible formats for parsing and displaying timestamps */
+       public enum Format
+       {
+               ORIGINAL,
+               LOCALE,
+               ISO8601
+       }
+
+
+       // Static block to initialise date formats
        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")
-               };
+               // Set timezone for output
+               TimeZone gmtZone = TimeZone.getTimeZone("GMT");
+               ISO_8601_FORMAT.setTimeZone(gmtZone);
+               ISO_8601_FORMAT_WITH_MILLIS.setTimeZone(gmtZone);
+               DEFAULT_DATETIME_FORMAT.setTimeZone(gmtZone);
        }
 
 
        /**
-        * Constructor
+        * @return true if valid
+        */
+       public abstract boolean isValid();
+
+       /**
+        * Get a calendar representing this timestamp
+        */
+       public abstract Calendar getCalendar(TimeZone inZone);
+
+       /**
+        * @return the milliseconds according to the given timezone
+        */
+       public abstract long getMilliseconds(TimeZone inZone);
+
+       /**
+        * @return true if this timestamp is after the other one
         */
-       public Timestamp(String inString)
+       public boolean isAfter(Timestamp inOther)
        {
-               // 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());
-                               // 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)
-                               {
-                                       // milliseconds since 1970
-                                       _seconds = rawValue / 1000L;
-                                       smallestDiff = diff2;
-                               }
-                               // Now millis since 1990
-                               if (diff3 < smallestDiff)
-                               {
-                                       // milliseconds since 1990
-                                       _seconds = rawValue / 1000L + TWENTY_YEARS_IN_SECS;
-                                       smallestDiff = diff3;
-                               }
-                               // Lastly, check garmin offset
-                               if (diff4 < smallestDiff)
-                               {
-                                       // seconds since garmin offset
-                                       _seconds = rawValue + GARTRIP_OFFSET;
-                               }
-                               _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<ALL_DATE_FORMATS.length && !_valid; i++)
-                               {
-                                       try
-                                       {
-                                               date = ALL_DATE_FORMATS[i].parse(inString);
-                                               CALENDAR.setTime(date);
-                                               _seconds = CALENDAR.getTimeInMillis() / 1000L;
-                                               _valid = true;
-                                       }
-                                       catch (ParseException e) {}
-                               }
-                       }
-               }
+               return getMillisecondsSince(inOther) > 0;
        }
 
-
        /**
-        * Constructor giving each field value individually
-        * @param inYear year
-        * @param inMonth month, beginning with 1
-        * @param inDay day of month, beginning with 1
-        * @param inHour hour of day, 0-24
-        * @param inMinute minute
-        * @param inSecond seconds
+        * @return true if this timestamp is before the other one
         */
-       public Timestamp(int inYear, int inMonth, int inDay, int inHour, int inMinute, int inSecond)
+       public boolean isBefore(Timestamp inOther)
        {
-               Calendar cal = Calendar.getInstance();
-               cal.set(Calendar.YEAR, inYear);
-               cal.set(Calendar.MONTH, inMonth - 1);
-               cal.set(Calendar.DAY_OF_MONTH, inDay);
-               cal.set(Calendar.HOUR_OF_DAY, inHour);
-               cal.set(Calendar.MINUTE, inMinute);
-               cal.set(Calendar.SECOND, inSecond);
-               cal.set(Calendar.MILLISECOND, 0);
-               _seconds = cal.getTimeInMillis() / 1000;
-               _valid = true;
+               return getMillisecondsSince(inOther) < 0;
        }
 
+       /**
+        * @return true if this timestamp is equal to the other one
+        */
+       public boolean isEqual(Timestamp inOther)
+       {
+               return getMillisecondsSince(inOther) == 0;
+       }
 
        /**
-        * Constructor giving millis since 1970
-        * @param inMillis
+        * @return the number of seconds since the other timestamp
         */
-       public Timestamp(long inMillis)
+       public long getSecondsSince(Timestamp inOther)
        {
-               _seconds = inMillis / 1000;
-               _valid = true;
+               return getMillisecondsSince(inOther) / 1000L;
        }
 
+       /**
+        * Calculate the difference between two Timestamps in milliseconds
+        * @param inOther other, earlier Timestamp
+        * @return number of milliseconds since other timestamp
+        */
+       public long getMillisecondsSince(Timestamp inOther)
+       {
+               return getMilliseconds(null) - inOther.getMilliseconds(null);
+       }
 
        /**
-        * @return true if timestamp is valid
+        * @return the number of seconds since the other timestamp using the given timezone
         */
-       public boolean isValid()
+       public long getSecondsSince(Timestamp inOther, TimeZone inTimezone)
        {
-               return _valid;
+               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);
 
        /**
-        * Calculate the difference between two Timestamps in seconds
-        * @param inOther other, earlier Timestamp
-        * @return number of seconds since other timestamp
+        * @return true if the timestamp has non-zero milliseconds
         */
-       public long getSecondsSince(Timestamp inOther)
+       protected abstract boolean hasMilliseconds();
+
+
+       /**
+        * @return date part of timestamp in locale-specific format
+        */
+       public String getDateText(TimeZone inTimezone)
        {
-               return _seconds - inOther._seconds;
+               if (!isValid()) return "";
+               return format(DEFAULT_DATE_FORMAT, inTimezone);
        }
 
-
        /**
-        * @return Description of timestamp in locale-specific format
+        * @return Description of time part of timestamp in locale-specific format
         */
-       public String getText()
+       public String getTimeText(TimeZone inTimezone)
        {
-               if (_text == null)
+               if (!isValid()) 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, inTimezone);
        }
+
+       /**
+        * Utility method for formatting dates / times
+        */
+       protected abstract String format(DateFormat inFormat, TimeZone inTimezone);
+
+
+       /**
+        * @return Description of timestamp in locale-specific format
+        */
+       public String getText(TimeZone inTimezone)
+       {
+               return getText(Format.LOCALE, inTimezone);
+       }
+
+       /**
+        * @param inFormat format of timestamp
+        * @return Description of timestamp in required format
+        */
+       public String getText(Format inFormat, TimeZone inTimezone)
+       {
+               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);
+               }
+       }
+
 }