]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/Timestamp.java
Version 11, August 2010
[GpsPrune.git] / tim / prune / data / Timestamp.java
1 package tim.prune.data;
2
3 import java.text.DateFormat;
4 import java.text.ParseException;
5 import java.text.SimpleDateFormat;
6 import java.util.Calendar;
7 import java.util.Date;
8 import java.util.regex.Matcher;
9 import java.util.regex.Pattern;
10
11 /**
12  * Class to hold the timestamp of a track point
13  * and provide conversion functions
14  */
15 public class Timestamp
16 {
17         private boolean _valid = false;
18         private long _seconds = 0L;
19         private String _text = null;
20         private String _timeText = null;
21
22         private static final DateFormat DEFAULT_DATE_FORMAT = DateFormat.getDateTimeInstance();
23         private static final DateFormat DEFAULT_TIME_FORMAT = DateFormat.getTimeInstance();
24         private static final DateFormat ISO_8601_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
25         private static final DateFormat ISO_8601_FORMAT_NOZ = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
26         private static DateFormat[] ALL_DATE_FORMATS = null;
27         private static Calendar CALENDAR = null;
28         private static final Pattern GENERAL_TIMESTAMP_PATTERN
29                 = Pattern.compile("(\\d{4})\\D(\\d{2})\\D(\\d{2})\\D(\\d{2})\\D(\\d{2})\\D(\\d{2})");
30         private static Matcher GENERAL_TIMESTAMP_MATCHER = null;
31         private static long SECS_SINCE_1970 = 0L;
32         private static long SECS_SINCE_GARTRIP = 0L;
33         private static long MSECS_SINCE_1970 = 0L;
34         private static long MSECS_SINCE_1990 = 0L;
35         private static long TWENTY_YEARS_IN_SECS = 0L;
36         private static final long GARTRIP_OFFSET = 631065600L;
37
38         /** Specifies original timestamp format */
39         public static final int FORMAT_ORIGINAL = 0;
40         /** Specifies locale-dependent timestamp format */
41         public static final int FORMAT_LOCALE = 1;
42         /** Specifies ISO 8601 timestamp format */
43         public static final int FORMAT_ISO_8601 = 2;
44
45         // Static block to initialise offsets
46         static
47         {
48                 CALENDAR = Calendar.getInstance();
49                 MSECS_SINCE_1970 = CALENDAR.getTimeInMillis();
50                 SECS_SINCE_1970 = MSECS_SINCE_1970 / 1000L;
51                 SECS_SINCE_GARTRIP = SECS_SINCE_1970 - GARTRIP_OFFSET;
52                 CALENDAR.add(Calendar.YEAR, -20);
53                 MSECS_SINCE_1990 = CALENDAR.getTimeInMillis();
54                 TWENTY_YEARS_IN_SECS = (MSECS_SINCE_1970 - MSECS_SINCE_1990) / 1000L;
55                 // Date formats
56                 ALL_DATE_FORMATS = new DateFormat[] {
57                         DEFAULT_DATE_FORMAT,
58                         new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy"),
59                         new SimpleDateFormat("HH:mm:ss dd MMM yyyy"),
60                         new SimpleDateFormat("dd MMM yyyy HH:mm:ss"),
61                         new SimpleDateFormat("yyyy MMM dd HH:mm:ss"),
62                         ISO_8601_FORMAT, ISO_8601_FORMAT_NOZ
63                 };
64         }
65
66
67         /**
68          * Constructor
69          * @param inString String containing timestamp
70          */
71         public Timestamp(String inString)
72         {
73                 // TODO: Does it really help to store timestamps in seconds rather than ms?
74                 if (inString != null && !inString.equals(""))
75                 {
76                         // Try to parse into a long
77                         try
78                         {
79                                 long rawValue = Long.parseLong(inString.trim());
80                                 _seconds = getSeconds(rawValue);
81                                 _valid = true;
82                         }
83                         catch (NumberFormatException nfe)
84                         {
85                                 // String is not a long, so try a date/time string instead
86                                 // try each of the date formatters in turn
87                                 Date date = null;
88                                 for (int i=0; i<ALL_DATE_FORMATS.length && !_valid; i++)
89                                 {
90                                         try
91                                         {
92                                                 date = ALL_DATE_FORMATS[i].parse(inString);
93                                                 CALENDAR.setTime(date);
94                                                 _seconds = CALENDAR.getTimeInMillis() / 1000L;
95                                                 _valid = true;
96                                         }
97                                         catch (ParseException e) {}
98                                 }
99                                 if (!_valid && inString.length() == 19) {
100                                         GENERAL_TIMESTAMP_MATCHER = GENERAL_TIMESTAMP_PATTERN.matcher(inString);
101                                         if (GENERAL_TIMESTAMP_MATCHER.matches()) {
102                                                 try {
103                                                         _seconds = getSeconds(Integer.parseInt(GENERAL_TIMESTAMP_MATCHER.group(1)),
104                                                                 Integer.parseInt(GENERAL_TIMESTAMP_MATCHER.group(2)),
105                                                                 Integer.parseInt(GENERAL_TIMESTAMP_MATCHER.group(3)),
106                                                                 Integer.parseInt(GENERAL_TIMESTAMP_MATCHER.group(4)),
107                                                                 Integer.parseInt(GENERAL_TIMESTAMP_MATCHER.group(5)),
108                                                                 Integer.parseInt(GENERAL_TIMESTAMP_MATCHER.group(6)));
109                                                         _valid = true;
110                                                 }
111                                                 catch (NumberFormatException nfe2) {} // parse shouldn't fail if matcher matched
112                                         }
113                                 }
114                         }
115                 }
116         }
117
118
119         /**
120          * Constructor giving each field value individually
121          * @param inYear year
122          * @param inMonth month, beginning with 1
123          * @param inDay day of month, beginning with 1
124          * @param inHour hour of day, 0-24
125          * @param inMinute minute
126          * @param inSecond seconds
127          */
128         public Timestamp(int inYear, int inMonth, int inDay, int inHour, int inMinute, int inSecond)
129         {
130                 _seconds = getSeconds(inYear, inMonth, inDay, inHour, inMinute, inSecond);
131                 _valid = true;
132         }
133
134
135         /**
136          * Constructor giving millis
137          * @param inMillis milliseconds since 1970
138          */
139         public Timestamp(long inMillis)
140         {
141                 _seconds = inMillis / 1000;
142                 _valid = true;
143         }
144
145
146         /**
147          * Convert the given timestamp parameters into a number of seconds
148          * @param inYear year
149          * @param inMonth month, beginning with 1
150          * @param inDay day of month, beginning with 1
151          * @param inHour hour of day, 0-24
152          * @param inMinute minute
153          * @param inSecond seconds
154          * @return number of seconds
155          */
156         private static long getSeconds(int inYear, int inMonth, int inDay, int inHour, int inMinute, int inSecond)
157         {
158                 Calendar cal = Calendar.getInstance();
159                 cal.set(Calendar.YEAR, inYear);
160                 cal.set(Calendar.MONTH, inMonth - 1);
161                 cal.set(Calendar.DAY_OF_MONTH, inDay);
162                 cal.set(Calendar.HOUR_OF_DAY, inHour);
163                 cal.set(Calendar.MINUTE, inMinute);
164                 cal.set(Calendar.SECOND, inSecond);
165                 cal.set(Calendar.MILLISECOND, 0);
166                 return cal.getTimeInMillis() / 1000;
167         }
168
169         /**
170          * Convert the given long parameters into a number of seconds
171          * @param inRawValue long value representing seconds / milliseconds
172          * @return number of seconds
173          */
174         private static long getSeconds(long inRawValue)
175         {
176                 // check for each format possibility and pick nearest
177                 long diff1 = Math.abs(SECS_SINCE_1970 - inRawValue);
178                 long diff2 = Math.abs(MSECS_SINCE_1970 - inRawValue);
179                 long diff3 = Math.abs(MSECS_SINCE_1990 - inRawValue);
180                 long diff4 = Math.abs(SECS_SINCE_GARTRIP - inRawValue);
181
182                 // Start off with "seconds since 1970" format
183                 long smallestDiff = diff1;
184                 long seconds = inRawValue;
185                 // Now check millis since 1970
186                 if (diff2 < smallestDiff)
187                 {
188                         // milliseconds since 1970
189                         seconds = inRawValue / 1000L;
190                         smallestDiff = diff2;
191                 }
192                 // Now millis since 1990
193                 if (diff3 < smallestDiff)
194                 {
195                         // milliseconds since 1990
196                         seconds = inRawValue / 1000L + TWENTY_YEARS_IN_SECS;
197                         smallestDiff = diff3;
198                 }
199                 // Lastly, check gartrip offset
200                 if (diff4 < smallestDiff)
201                 {
202                         // seconds since gartrip offset
203                         seconds = inRawValue + GARTRIP_OFFSET;
204                 }
205                 return seconds;
206         }
207
208         /**
209          * @return true if timestamp is valid
210          */
211         public boolean isValid()
212         {
213                 return _valid;
214         }
215
216         /**
217          * @param inOther other Timestamp
218          * @return true if this one is after the other
219          */
220         public boolean isAfter(Timestamp inOther)
221         {
222                 return _seconds > inOther._seconds;
223         }
224
225         /**
226          * Calculate the difference between two Timestamps in seconds
227          * @param inOther other, earlier Timestamp
228          * @return number of seconds since other timestamp
229          */
230         public long getSecondsSince(Timestamp inOther)
231         {
232                 return _seconds - inOther._seconds;
233         }
234
235         /**
236          * Add the given number of seconds offset
237          * @param inOffset number of seconds to add/subtract
238          */
239         public void addOffset(long inOffset)
240         {
241                 _seconds += inOffset;
242                 _text = null;
243         }
244
245         /**
246          * Add the given TimeDifference to this Timestamp
247          * @param inOffset TimeDifference to add
248          * @return new Timestamp object
249          */
250         public Timestamp createPlusOffset(TimeDifference inOffset)
251         {
252                 return new Timestamp((_seconds + inOffset.getTotalSeconds()) * 1000L);
253         }
254
255
256         /**
257          * Subtract the given TimeDifference from this Timestamp
258          * @param inOffset TimeDifference to subtract
259          * @return new Timestamp object
260          */
261         public Timestamp createMinusOffset(TimeDifference inOffset)
262         {
263                 return new Timestamp((_seconds - inOffset.getTotalSeconds()) * 1000L);
264         }
265
266
267         /**
268          * @return Description of timestamp in locale-specific format
269          */
270         public String getText()
271         {
272                 return getText(FORMAT_LOCALE);
273         }
274
275         /**
276          * @param inFormat format of timestamp
277          * @return Description of timestamp in required format
278          */
279         public String getText(int inFormat)
280         {
281                 if (!_valid) {return "";}
282                 if (inFormat == FORMAT_ISO_8601) {
283                         return format(ISO_8601_FORMAT);
284                 }
285                 if (_text == null) {
286                         _text = (_valid?format(DEFAULT_DATE_FORMAT):"");
287                 }
288                 return _text;
289         }
290
291         /**
292          * @return Description of time part of timestamp in locale-specific format
293          */
294         public String getTimeText()
295         {
296                 if (_timeText == null)
297                 {
298                         if (_valid) {
299                                 _timeText = format(DEFAULT_TIME_FORMAT);
300                         }
301                         else _timeText = "";
302                 }
303                 return _timeText;
304         }
305
306         /**
307          * Utility method for formatting dates / times
308          * @param inFormat formatter object
309          * @return formatted String
310          */
311         private String format(DateFormat inFormat)
312         {
313                 CALENDAR.setTimeInMillis(_seconds * 1000L);
314                 return inFormat.format(CALENDAR.getTime());
315         }
316
317         /**
318          * @return a Calendar object representing this timestamp
319          */
320         public Calendar getCalendar()
321         {
322                 Calendar cal = Calendar.getInstance();
323                 cal.setTimeInMillis(_seconds * 1000L);
324                 return cal;
325         }
326 }