1 package tim.prune.data;
3 import java.text.DateFormat;
4 import java.text.ParseException;
5 import java.text.SimpleDateFormat;
6 import java.util.Calendar;
10 * Class to hold the timestamp of a track point
11 * and provide conversion functions
13 public class Timestamp
15 private boolean _valid = false;
16 private long _seconds = 0L;
17 private String _text = null;
18 private String _timeText = null;
20 private static DateFormat DEFAULT_DATE_FORMAT = DateFormat.getDateTimeInstance();
21 private static DateFormat DEFAULT_TIME_FORMAT = DateFormat.getTimeInstance();
22 private static DateFormat ISO_8601_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
23 private static DateFormat[] ALL_DATE_FORMATS = null;
24 private static Calendar CALENDAR = null;
25 private static long SECS_SINCE_1970 = 0L;
26 private static long SECS_SINCE_GARTRIP = 0L;
27 private static long MSECS_SINCE_1970 = 0L;
28 private static long MSECS_SINCE_1990 = 0L;
29 private static long TWENTY_YEARS_IN_SECS = 0L;
30 private static final long GARTRIP_OFFSET = 631065600L;
32 /** Specifies original timestamp format */
33 public static final int FORMAT_ORIGINAL = 0;
34 /** Specifies locale-dependent timestamp format */
35 public static final int FORMAT_LOCALE = 1;
36 /** Specifies ISO 8601 timestamp format */
37 public static final int FORMAT_ISO_8601 = 2;
39 // Static block to initialise offsets
42 CALENDAR = Calendar.getInstance();
43 MSECS_SINCE_1970 = CALENDAR.getTimeInMillis();
44 SECS_SINCE_1970 = MSECS_SINCE_1970 / 1000L;
45 SECS_SINCE_GARTRIP = SECS_SINCE_1970 - GARTRIP_OFFSET;
46 CALENDAR.add(Calendar.YEAR, -20);
47 MSECS_SINCE_1990 = CALENDAR.getTimeInMillis();
48 TWENTY_YEARS_IN_SECS = (MSECS_SINCE_1970 - MSECS_SINCE_1990) / 1000L;
50 ALL_DATE_FORMATS = new DateFormat[] {
52 new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy"),
53 new SimpleDateFormat("HH:mm:ss dd MMM yyyy"),
54 new SimpleDateFormat("dd MMM yyyy HH:mm:ss"),
55 new SimpleDateFormat("yyyy MMM dd HH:mm:ss"),
63 * @param inString String containing timestamp
65 public Timestamp(String inString)
67 // TODO: Does it really help to store timestamps in seconds rather than ms?
68 if (inString != null && !inString.equals(""))
70 // Try to parse into a long
73 long rawValue = Long.parseLong(inString.trim());
74 // check for each format possibility and pick nearest
75 long diff1 = Math.abs(SECS_SINCE_1970 - rawValue);
76 long diff2 = Math.abs(MSECS_SINCE_1970 - rawValue);
77 long diff3 = Math.abs(MSECS_SINCE_1990 - rawValue);
78 long diff4 = Math.abs(SECS_SINCE_GARTRIP - rawValue);
80 // Start off with "seconds since 1970" format
81 long smallestDiff = diff1;
83 // Now check millis since 1970
84 if (diff2 < smallestDiff)
86 // milliseconds since 1970
87 _seconds = rawValue / 1000L;
90 // Now millis since 1990
91 if (diff3 < smallestDiff)
93 // milliseconds since 1990
94 _seconds = rawValue / 1000L + TWENTY_YEARS_IN_SECS;
97 // Lastly, check gartrip offset
98 if (diff4 < smallestDiff)
100 // seconds since gartrip offset
101 _seconds = rawValue + GARTRIP_OFFSET;
105 catch (NumberFormatException nfe)
107 // String is not a long, so try a date/time string instead
108 // try each of the date formatters in turn
110 for (int i=0; i<ALL_DATE_FORMATS.length && !_valid; i++)
114 date = ALL_DATE_FORMATS[i].parse(inString);
115 CALENDAR.setTime(date);
116 _seconds = CALENDAR.getTimeInMillis() / 1000L;
119 catch (ParseException e) {}
127 * Constructor giving each field value individually
129 * @param inMonth month, beginning with 1
130 * @param inDay day of month, beginning with 1
131 * @param inHour hour of day, 0-24
132 * @param inMinute minute
133 * @param inSecond seconds
135 public Timestamp(int inYear, int inMonth, int inDay, int inHour, int inMinute, int inSecond)
137 Calendar cal = Calendar.getInstance();
138 cal.set(Calendar.YEAR, inYear);
139 cal.set(Calendar.MONTH, inMonth - 1);
140 cal.set(Calendar.DAY_OF_MONTH, inDay);
141 cal.set(Calendar.HOUR_OF_DAY, inHour);
142 cal.set(Calendar.MINUTE, inMinute);
143 cal.set(Calendar.SECOND, inSecond);
144 cal.set(Calendar.MILLISECOND, 0);
145 _seconds = cal.getTimeInMillis() / 1000;
151 * Constructor giving millis
152 * @param inMillis milliseconds since 1970
154 public Timestamp(long inMillis)
156 _seconds = inMillis / 1000;
162 * @return true if timestamp is valid
164 public boolean isValid()
171 * Calculate the difference between two Timestamps in seconds
172 * @param inOther other, earlier Timestamp
173 * @return number of seconds since other timestamp
175 public long getSecondsSince(Timestamp inOther)
177 return _seconds - inOther._seconds;
182 * Add the given TimeDifference to this Timestamp
183 * @param inOffset TimeDifference to add
184 * @return new Timestamp object
186 public Timestamp addOffset(TimeDifference inOffset)
188 return new Timestamp((_seconds + inOffset.getTotalSeconds()) * 1000L);
193 * Subtract the given TimeDifference from this Timestamp
194 * @param inOffset TimeDifference to subtract
195 * @return new Timestamp object
197 public Timestamp subtractOffset(TimeDifference inOffset)
199 return new Timestamp((_seconds - inOffset.getTotalSeconds()) * 1000L);
204 * @return Description of timestamp in locale-specific format
206 public String getText()
208 return getText(FORMAT_LOCALE);
212 * @param inFormat format of timestamp
213 * @return Description of timestamp in required format
215 public String getText(int inFormat)
217 if (inFormat == FORMAT_ISO_8601) {
218 return format(ISO_8601_FORMAT);
223 _text = format(DEFAULT_DATE_FORMAT);
231 * @return Description of time part of timestamp in locale-specific format
233 public String getTimeText()
235 if (_timeText == null)
238 _timeText = format(DEFAULT_TIME_FORMAT);
246 * Utility method for formatting dates / times
247 * @param inFormat formatter object
248 * @return formatted String
250 private String format(DateFormat inFormat)
252 CALENDAR.setTimeInMillis(_seconds * 1000L);
253 return inFormat.format(CALENDAR.getTime());
257 * @return a Calendar object representing this timestamp
259 public Calendar getCalendar()
261 Calendar cal = Calendar.getInstance();
262 cal.setTimeInMillis(_seconds * 1000L);