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;
19 private static DateFormat DEFAULT_DATE_FORMAT = DateFormat.getDateTimeInstance();
20 private static DateFormat[] ALL_DATE_FORMATS = null;
21 private static Calendar CALENDAR = null;
22 private static long SECS_SINCE_1970 = 0L;
23 private static long SECS_SINCE_GARTRIP = 0L;
24 private static long MSECS_SINCE_1970 = 0L;
25 private static long MSECS_SINCE_1990 = 0L;
26 private static long TWENTY_YEARS_IN_SECS = 0L;
28 private static final long GARTRIP_OFFSET = 631065600L;
30 // Static block to initialise offsets
33 CALENDAR = Calendar.getInstance();
34 MSECS_SINCE_1970 = CALENDAR.getTimeInMillis();
35 SECS_SINCE_1970 = MSECS_SINCE_1970 / 1000L;
36 SECS_SINCE_GARTRIP = SECS_SINCE_1970 - GARTRIP_OFFSET;
37 CALENDAR.add(Calendar.YEAR, -20);
38 MSECS_SINCE_1990 = CALENDAR.getTimeInMillis();
39 TWENTY_YEARS_IN_SECS = (MSECS_SINCE_1970 - MSECS_SINCE_1990) / 1000L;
41 ALL_DATE_FORMATS = new DateFormat[] {
43 new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy"),
44 new SimpleDateFormat("HH:mm:ss dd MMM yyyy"),
45 new SimpleDateFormat("dd MMM yyyy HH:mm:ss"),
46 new SimpleDateFormat("yyyy MMM dd HH:mm:ss")
54 public Timestamp(String inString)
56 if (inString != null && !inString.equals(""))
58 // Try to parse into a long
61 long rawValue = Long.parseLong(inString.trim());
62 // check for each format possibility and pick nearest
63 long diff1 = Math.abs(SECS_SINCE_1970 - rawValue);
64 long diff2 = Math.abs(MSECS_SINCE_1970 - rawValue);
65 long diff3 = Math.abs(MSECS_SINCE_1990 - rawValue);
66 long diff4 = Math.abs(SECS_SINCE_GARTRIP - rawValue);
68 // Start off with "seconds since 1970" format
69 long smallestDiff = diff1;
71 // Now check millis since 1970
72 if (diff2 < smallestDiff)
74 // milliseconds since 1970
75 _seconds = rawValue / 1000L;
78 // Now millis since 1990
79 if (diff3 < smallestDiff)
81 // milliseconds since 1990
82 _seconds = rawValue / 1000L + TWENTY_YEARS_IN_SECS;
85 // Lastly, check garmin offset
86 if (diff4 < smallestDiff)
88 // milliseconds since garmin offset
89 _seconds = rawValue + GARTRIP_OFFSET;
93 catch (NumberFormatException nfe)
95 // String is not a long, so try a date/time string instead
96 // try each of the date formatters in turn
98 for (int i=0; i<ALL_DATE_FORMATS.length && !_valid; i++)
102 date = ALL_DATE_FORMATS[i].parse(inString);
103 CALENDAR.setTime(date);
104 _seconds = CALENDAR.getTimeInMillis() / 1000L;
107 catch (ParseException e) {}
115 * @return true if timestamp is valid
117 public boolean isValid()
124 * Calculate the difference between two Timestamps in seconds
125 * @param inOther other, earlier Timestamp
126 * @return number of seconds since other timestamp
128 public long getSecondsSince(Timestamp inOther)
130 return _seconds - inOther._seconds;
135 * @return Description of timestamp in locale-specific format
137 public String getText()
143 CALENDAR.setTimeInMillis(_seconds * 1000L);
144 _text = DEFAULT_DATE_FORMAT.format(CALENDAR.getTime());