]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/TimestampUtc.java
Version 19.1, August 2018
[GpsPrune.git] / tim / prune / data / TimestampUtc.java
1 package tim.prune.data;
2
3 import java.text.DateFormat;
4 import java.text.ParsePosition;
5 import java.text.SimpleDateFormat;
6 import java.util.Calendar;
7 import java.util.Date;
8 import java.util.TimeZone;
9 import java.util.regex.Matcher;
10 import java.util.regex.Pattern;
11
12
13 /**
14  * Class to hold a UTC-based timestamp, for example of a track point.
15  * When the selected timezone changes, this timestamp will keep its
16  * numerical value but the date and time will change accordingly.
17  */
18 public class TimestampUtc extends Timestamp
19 {
20         private boolean _valid = false;
21         private long _milliseconds = 0L;
22         private String _text = null;
23
24         private static final DateFormat ISO_8601_FORMAT_NOZ = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
25         private static DateFormat[] ALL_DATE_FORMATS = null;
26         private static Calendar CALENDAR = null;
27         private static final Pattern ISO8601_FRACTIONAL_PATTERN
28                 = Pattern.compile("(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2})(?:[\\.,](\\d{1,3}))?(Z|[\\+-]\\d{2}(?::?\\d{2})?)?");
29                 //                    year     month     day T  hour    minute    sec             millisec   Z or +/-  hours  :   minutes
30         private static final Pattern GENERAL_TIMESTAMP_PATTERN
31                 = Pattern.compile("(\\d{4})\\D(\\d{2})\\D(\\d{2})\\D(\\d{2})\\D(\\d{2})\\D(\\d{2})");
32         private static long SECS_SINCE_1970 = 0L;
33         private static long SECS_SINCE_GARTRIP = 0L;
34         private static long MSECS_SINCE_1970 = 0L;
35         private static long MSECS_SINCE_1990 = 0L;
36         private static long TWENTY_YEARS_IN_SECS = 0L;
37         private static final long GARTRIP_OFFSET = 631065600L;
38
39         /** Identifier for the parsing strategy to use */
40         private enum ParseType
41         {
42                 NONE,
43                 ISO8601_FRACTIONAL,
44                 LONG,
45                 FIXED_FORMAT0,
46                 FIXED_FORMAT1,
47                 FIXED_FORMAT2,
48                 FIXED_FORMAT3,
49                 FIXED_FORMAT4,
50                 FIXED_FORMAT5,
51                 FIXED_FORMAT6,
52                 FIXED_FORMAT7,
53                 FIXED_FORMAT8,
54                 GENERAL_STRING
55         }
56
57         /** Array of parse types to loop through (first one is changed to last successful type) */
58         private static ParseType[] ALL_PARSE_TYPES = {ParseType.NONE, ParseType.ISO8601_FRACTIONAL, ParseType.LONG,
59                 ParseType.FIXED_FORMAT0, ParseType.FIXED_FORMAT1, ParseType.FIXED_FORMAT2, ParseType.FIXED_FORMAT3,
60                 ParseType.FIXED_FORMAT4, ParseType.FIXED_FORMAT5, ParseType.FIXED_FORMAT6, ParseType.FIXED_FORMAT7,
61                 ParseType.FIXED_FORMAT8, ParseType.GENERAL_STRING};
62
63         // Static block to initialise offsets
64         static
65         {
66                 CALENDAR = Calendar.getInstance();
67                 TimeZone gmtZone = TimeZone.getTimeZone("GMT");
68                 CALENDAR.setTimeZone(gmtZone);
69                 MSECS_SINCE_1970 = CALENDAR.getTimeInMillis();
70                 SECS_SINCE_1970 = MSECS_SINCE_1970 / 1000L;
71                 SECS_SINCE_GARTRIP = SECS_SINCE_1970 - GARTRIP_OFFSET;
72                 CALENDAR.add(Calendar.YEAR, -20);
73                 MSECS_SINCE_1990 = CALENDAR.getTimeInMillis();
74                 TWENTY_YEARS_IN_SECS = (MSECS_SINCE_1970 - MSECS_SINCE_1990) / 1000L;
75                 // Date formats
76                 ALL_DATE_FORMATS = new DateFormat[]
77                 {
78                         DEFAULT_DATETIME_FORMAT,
79                         new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy"),
80                         new SimpleDateFormat("HH:mm:ss dd MMM yyyy"),
81                         new SimpleDateFormat("dd MMM yyyy HH:mm:ss"),
82                         new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss"),
83                         new SimpleDateFormat("yyyy MMM dd HH:mm:ss"),
84                         new SimpleDateFormat("MMM dd, yyyy hh:mm:ss aa"),
85                         ISO_8601_FORMAT, ISO_8601_FORMAT_NOZ
86                 };
87                 for (DateFormat df : ALL_DATE_FORMATS)
88                 {
89                         df.setLenient(false);
90                         df.setTimeZone(gmtZone);
91                 }
92         }
93
94
95         /**
96          * Constructor
97          * @param inString String containing timestamp
98          */
99         public TimestampUtc(String inString)
100         {
101                 _valid = false;
102                 _text = null;
103                 if (inString != null && !inString.equals(""))
104                 {
105                         // Try each of the parse types in turn
106                         for (ParseType type : ALL_PARSE_TYPES)
107                         {
108                                 if (parseString(inString, type))
109                                 {
110                                         ALL_PARSE_TYPES[0] = type;
111                                         _valid = true;
112                                         _text = inString;
113                                         return;
114                                 }
115                         }
116                 }
117         }
118
119         /**
120          * Try to parse the given string in the specified way
121          * @param inString String to parse
122          * @param inType parse type to use
123          * @return true if successful
124          */
125         private boolean parseString(String inString, ParseType inType)
126         {
127                 if (inString == null || inString.equals("")) {
128                         return false;
129                 }
130                 switch (inType)
131                 {
132                         case NONE: return false;
133                         case LONG:
134                                 // Try to parse into a long
135                                 try
136                                 {
137                                         long rawValue = Long.parseLong(inString.trim());
138                                         _milliseconds = getMilliseconds(rawValue);
139                                         return true;
140                                 }
141                                 catch (NumberFormatException nfe)
142                                 {}
143                                 break;
144
145                         case ISO8601_FRACTIONAL:
146                                 final Matcher fmatcher = ISO8601_FRACTIONAL_PATTERN.matcher(inString);
147                                 if (fmatcher.matches())
148                                 {
149                                         try {
150                                                 _milliseconds = getMilliseconds(Integer.parseInt(fmatcher.group(1)), // year
151                                                         Integer.parseInt(fmatcher.group(2)), // month
152                                                         Integer.parseInt(fmatcher.group(3)), // day
153                                                         Integer.parseInt(fmatcher.group(4)), // hour
154                                                         Integer.parseInt(fmatcher.group(5)), // minute
155                                                         Integer.parseInt(fmatcher.group(6)), // second
156                                                         fmatcher.group(7),                   // fractional seconds
157                                                         fmatcher.group(8));                  // timezone, if any
158                                                 return true;
159                                         }
160                                         catch (NumberFormatException nfe) {}
161                                 }
162                                 break;
163
164                         case FIXED_FORMAT0: return parseString(inString, ALL_DATE_FORMATS[0]);
165                         case FIXED_FORMAT1: return parseString(inString, ALL_DATE_FORMATS[1]);
166                         case FIXED_FORMAT2: return parseString(inString, ALL_DATE_FORMATS[2]);
167                         case FIXED_FORMAT3: return parseString(inString, ALL_DATE_FORMATS[3]);
168                         case FIXED_FORMAT4: return parseString(inString, ALL_DATE_FORMATS[4]);
169                         case FIXED_FORMAT5: return parseString(inString, ALL_DATE_FORMATS[5]);
170                         case FIXED_FORMAT6: return parseString(inString, ALL_DATE_FORMATS[6]);
171                         case FIXED_FORMAT7: return parseString(inString, ALL_DATE_FORMATS[7]);
172                         case FIXED_FORMAT8: return parseString(inString, ALL_DATE_FORMATS[8]);
173
174                         case GENERAL_STRING:
175                                 if (inString.length() == 19)
176                                 {
177                                         final Matcher matcher = GENERAL_TIMESTAMP_PATTERN.matcher(inString);
178                                         if (matcher.matches())
179                                         {
180                                                 try {
181                                                         _milliseconds = getMilliseconds(Integer.parseInt(matcher.group(1)),
182                                                                 Integer.parseInt(matcher.group(2)),
183                                                                 Integer.parseInt(matcher.group(3)),
184                                                                 Integer.parseInt(matcher.group(4)),
185                                                                 Integer.parseInt(matcher.group(5)),
186                                                                 Integer.parseInt(matcher.group(6)),
187                                                                 null, null); // no fractions of a second and no timezone
188                                                         return true;
189                                                 }
190                                                 catch (NumberFormatException nfe2) {} // parse shouldn't fail if matcher matched
191                                         }
192                                 }
193                                 return false;
194                 }
195                 return false;
196         }
197
198
199         /**
200          * Try to parse the given string with the given date format
201          * @param inString String to parse
202          * @param inDateFormat Date format to use
203          * @return true if successful
204          */
205         private boolean parseString(String inString, DateFormat inDateFormat)
206         {
207                 ParsePosition pPos = new ParsePosition(0);
208                 Date date = inDateFormat.parse(inString, pPos);
209                 if (date != null && inString.length() == pPos.getIndex()) // require use of _all_ the string, not just the beginning
210                 {
211                         CALENDAR.setTime(date);
212                         _milliseconds = CALENDAR.getTimeInMillis();
213                         return true;
214                 }
215
216                 return false;
217         }
218
219
220         /**
221          * Constructor giving millis
222          * @param inMillis milliseconds since 1970
223          */
224         public TimestampUtc(long inMillis)
225         {
226                 _milliseconds = inMillis;
227                 _valid = true;
228         }
229
230
231         /**
232          * Convert the given timestamp parameters into a number of milliseconds
233          * @param inYear year
234          * @param inMonth month, beginning with 1
235          * @param inDay day of month, beginning with 1
236          * @param inHour hour of day, 0-24
237          * @param inMinute minute
238          * @param inSecond seconds
239          * @param inFraction fractions of a second
240          * @param inTimezone timezone, if any
241          * @return number of milliseconds
242          */
243         private static long getMilliseconds(int inYear, int inMonth, int inDay,
244                 int inHour, int inMinute, int inSecond, String inFraction, String inTimezone)
245         {
246                 Calendar cal = Calendar.getInstance();
247                 // Timezone, if any
248                 if (inTimezone == null || inTimezone.equals("") || inTimezone.equals("Z")) {
249                         // No timezone, use zulu
250                         cal.setTimeZone(TimeZone.getTimeZone("GMT"));
251                 }
252                 else {
253                         // Timezone specified, pass to calendar
254                         cal.setTimeZone(TimeZone.getTimeZone("GMT" + inTimezone));
255                 }
256                 cal.set(Calendar.YEAR, inYear);
257                 cal.set(Calendar.MONTH, inMonth - 1);
258                 cal.set(Calendar.DAY_OF_MONTH, inDay);
259                 cal.set(Calendar.HOUR_OF_DAY, inHour);
260                 cal.set(Calendar.MINUTE, inMinute);
261                 cal.set(Calendar.SECOND, inSecond);
262                 int millis = 0;
263                 if (inFraction != null)
264                 {
265                         try {
266                                 int frac = Integer.parseInt(inFraction);
267                                 final int fracLen = inFraction.length();
268                                 switch (fracLen) {
269                                         case 1: millis = frac * 100; break;
270                                         case 2: millis = frac * 10;  break;
271                                         case 3: millis = frac;       break;
272                                 }
273                         }
274                         catch (NumberFormatException nfe) {} // ignore errors, millis stay at 0
275                 }
276                 cal.set(Calendar.MILLISECOND, millis);
277                 return cal.getTimeInMillis();
278         }
279
280         /**
281          * Convert the given long parameters into a number of milliseconds
282          * @param inRawValue long value representing seconds / milliseconds
283          * @return number of milliseconds
284          */
285         private static long getMilliseconds(long inRawValue)
286         {
287                 // check for each format possibility and pick nearest
288                 long diff1 = Math.abs(SECS_SINCE_1970 - inRawValue);
289                 long diff2 = Math.abs(MSECS_SINCE_1970 - inRawValue);
290                 long diff3 = Math.abs(MSECS_SINCE_1990 - inRawValue);
291                 long diff4 = Math.abs(SECS_SINCE_GARTRIP - inRawValue);
292
293                 // Start off with "seconds since 1970" format
294                 long smallestDiff = diff1;
295                 long millis = inRawValue * 1000;
296                 // Now check millis since 1970
297                 if (diff2 < smallestDiff)
298                 {
299                         // milliseconds since 1970
300                         millis = inRawValue;
301                         smallestDiff = diff2;
302                 }
303                 // Now millis since 1990
304                 if (diff3 < smallestDiff)
305                 {
306                         // milliseconds since 1990
307                         millis = inRawValue + TWENTY_YEARS_IN_SECS * 1000L;
308                         smallestDiff = diff3;
309                 }
310                 // Lastly, check gartrip offset
311                 if (diff4 < smallestDiff)
312                 {
313                         // seconds since gartrip offset
314                         millis = (inRawValue + GARTRIP_OFFSET) * 1000L;
315                 }
316                 return millis;
317         }
318
319         /**
320          * @return true if timestamp is valid
321          */
322         public boolean isValid()
323         {
324                 return _valid;
325         }
326
327         /**
328          * @return true if the timestamp has non-zero milliseconds
329          */
330         protected boolean hasMilliseconds()
331         {
332                 return isValid() && (_milliseconds % 1000L) > 0;
333         }
334
335         /**
336          * @return the milliseconds according to the given timezone
337          */
338         public long getMilliseconds(TimeZone inZone)
339         {
340                 return _milliseconds;
341         }
342
343
344         /**
345          * Add the given number of seconds offset
346          * @param inOffset number of seconds to add/subtract
347          */
348         public void addOffsetSeconds(long inOffset)
349         {
350                 _milliseconds += (inOffset * 1000L);
351                 _text = null;
352         }
353
354
355         /**
356          * @param inFormat format of timestamp
357          * @param inTimezone timezone to use
358          * @return Description of timestamp in required format
359          */
360         public String getText(Format inFormat, TimeZone inTimezone)
361         {
362                 // Use the cached text if possible
363                 if (isValid()
364                         && _text != null
365                         && inFormat == Format.ORIGINAL)
366                 {
367                         return _text;
368                 }
369
370                 // Nothing cached, so use the regular one
371                 return super.getText(inFormat, inTimezone);
372         }
373
374         /**
375          * Utility method for formatting dates / times
376          * @param inFormat formatter object
377          * @param inTimezone timezone to use
378          * @return formatted String
379          */
380         protected String format(DateFormat inFormat, TimeZone inTimezone)
381         {
382                 CALENDAR.setTimeZone(TimeZone.getTimeZone("GMT"));
383                 inFormat.setTimeZone(inTimezone == null ? TimeZone.getTimeZone("GMT") : inTimezone);
384
385                 CALENDAR.setTimeInMillis(_milliseconds);
386                 return inFormat.format(CALENDAR.getTime());
387         }
388
389         /**
390          * @return a Calendar object representing this timestamp
391          */
392         public Calendar getCalendar(TimeZone inZone)
393         {
394                 Calendar cal = Calendar.getInstance();
395                 cal.setTimeZone(TimeZone.getTimeZone("GMT"));
396                 cal.setTimeInMillis(_milliseconds);
397                 return cal;
398         }
399 }