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