Changeset 9383 in josm
- Timestamp:
- 2016-01-10T13:04:06+01:00 (9 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/gpx/WayPoint.java
r8846 r9383 12 12 import org.openstreetmap.josm.data.coor.LatLon; 13 13 import org.openstreetmap.josm.data.projection.Projections; 14 import org.openstreetmap.josm.tools.date. PrimaryDateParser;14 import org.openstreetmap.josm.tools.date.DateUtils; 15 15 import org.openstreetmap.josm.tools.template_engine.TemplateEngineDataProvider; 16 16 17 17 public class WayPoint extends WithAttributes implements Comparable<WayPoint>, TemplateEngineDataProvider { 18 19 private static ThreadLocal<PrimaryDateParser> dateParser = new ThreadLocal<PrimaryDateParser>() {20 @Override protected PrimaryDateParser initialValue() {21 return new PrimaryDateParser();22 }23 };24 18 25 19 public double time; … … 99 93 100 94 /** 95 * Sets the {@link #time} field as well as the {@link #PT_TIME} attribute to the specified time 96 * 97 * @param time the time to set 98 * @since 9383 99 */ 100 public void setTime(Date time) { 101 this.time = time.getTime() / 1000.; 102 this.attr.put(PT_TIME, DateUtils.fromDate(time)); 103 } 104 105 /** 101 106 * Convert the time stamp of the waypoint into seconds from the epoch 102 107 */ 103 108 public void setTime() { 109 setTimeFromAttribute(); 110 } 111 112 /** 113 * Convert the time stamp of the waypoint into seconds from the epoch 114 * @return The parsed time if successful, or {@code null} 115 * @since 9383 116 */ 117 public Date setTimeFromAttribute() { 104 118 if (attr.containsKey(PT_TIME)) { 105 119 try { 106 time = dateParser.get().parse(get(PT_TIME).toString()).getTime() / 1000.; /* ms => seconds */ 120 final Date time = DateUtils.fromString(get(PT_TIME).toString()); 121 setTime(time); 122 return time; 107 123 } catch (Exception e) { 124 Main.warn(e); 108 125 time = 0; 109 126 } 110 127 } 128 return null; 111 129 } 112 130 -
trunk/src/org/openstreetmap/josm/gui/layer/geoimage/CorrelateGpxWithImages.java
r9270 r9383 81 81 import org.openstreetmap.josm.tools.Utils; 82 82 import org.openstreetmap.josm.tools.date.DateUtils; 83 import org.openstreetmap.josm.tools.date.PrimaryDateParser;84 83 import org.xml.sax.SAXException; 85 84 … … 1014 1013 1015 1014 List<ImageEntry> imgs = getSortedImgList(); 1016 PrimaryDateParser dateParser = new PrimaryDateParser();1017 1015 1018 1016 // no images found, exit … … 1032 1030 for (GpxTrackSegment segment : trk.getSegments()) { 1033 1031 for (WayPoint curWp : segment.getWayPoints()) { 1034 String curDateWpStr = curWp.getString(GpxConstants.PT_TIME);1035 if (curDateWpStr == null) {1036 continue;1037 }1038 1039 1032 try { 1040 firstGPXDate = dateParser.parse(curDateWpStr).getTime()/1000; 1041 break outer; 1033 final Date parsedTime = curWp.setTimeFromAttribute(); 1034 if (parsedTime != null) { 1035 firstGPXDate = parsedTime.getTime(); 1036 break outer; 1037 } 1042 1038 } catch (Exception e) { 1043 1039 Main.warn(e); … … 1153 1149 int ret = 0; 1154 1150 1155 PrimaryDateParser dateParser = new PrimaryDateParser();1156 1157 1151 for (GpxTrack trk : selectedGpx.tracks) { 1158 1152 for (GpxTrackSegment segment : trk.getSegments()) { … … 1162 1156 1163 1157 for (WayPoint curWp : segment.getWayPoints()) { 1164 1165 String curWpTimeStr = curWp.getString(GpxConstants.PT_TIME); 1166 if (curWpTimeStr != null) { 1167 1168 try { 1169 long curWpTime = dateParser.parse(curWpTimeStr).getTime() + offset; 1158 try { 1159 final Date parsedTime = curWp.setTimeFromAttribute(); 1160 if (parsedTime != null) { 1161 final long curWpTime = parsedTime.getTime() + offset; 1170 1162 ret += matchPoints(images, prevWp, prevWpTime, curWp, curWpTime, offset); 1171 1163 1172 1164 prevWp = curWp; 1173 1165 prevWpTime = curWpTime; 1174 1175 } catch (ParseException e) { 1176 Main.error("Error while parsing date \"" + curWpTimeStr + '"'); 1177 Main.error(e); 1178 prevWp = null; 1179 prevWpTime = 0; 1166 continue; 1180 1167 } 1181 } else { 1182 prevWp = null; 1183 prevWpTime = 0; 1168 } catch (Exception e) { 1169 Main.warn(e); 1184 1170 } 1171 prevWp = null; 1172 prevWpTime = 0; 1185 1173 } 1186 1174 } -
trunk/src/org/openstreetmap/josm/gui/layer/geoimage/ImageEntry.java
r9331 r9383 5 5 import java.io.File; 6 6 import java.io.IOException; 7 import java.text.ParseException;8 7 import java.util.Calendar; 9 8 import java.util.Collections; … … 449 448 try { 450 449 setExifTime(ExifReader.readTime(file)); 451 } catch ( ParseException ex) {450 } catch (RuntimeException ex) { 452 451 setExifTime(null); 453 452 } -
trunk/src/org/openstreetmap/josm/tools/ExifReader.java
r8509 r9383 5 5 import java.io.File; 6 6 import java.io.IOException; 7 import java.text.ParseException;8 7 import java.util.Date; 9 8 10 9 import org.openstreetmap.josm.Main; 11 10 import org.openstreetmap.josm.data.coor.LatLon; 12 import org.openstreetmap.josm.tools.date. PrimaryDateParser;11 import org.openstreetmap.josm.tools.date.DateUtils; 13 12 14 13 import com.drew.imaging.jpeg.JpegMetadataReader; … … 38 37 * @param filename The JPEG file to read 39 38 * @return The date/time read in the EXIF section, or {@code null} if not found 40 * @throws ParseException if {@link PrimaryDateParser#parse} fails to parse date/time 41 */ 42 public static Date readTime(File filename) throws ParseException { 39 */ 40 public static Date readTime(File filename) { 43 41 try { 44 42 Metadata metadata = JpegMetadataReader.readMetadata(filename); … … 60 58 if (dateStr != null) { 61 59 dateStr = dateStr.replace('/', ':'); // workaround for HTC Sensation bug, see #7228 62 return new PrimaryDateParser().parse(dateStr); 63 } 64 } catch (ParseException e) { 65 throw e; 60 return DateUtils.fromString(dateStr); 61 } 66 62 } catch (Exception e) { 67 63 Main.error(e); -
trunk/src/org/openstreetmap/josm/tools/date/DateUtils.java
r9059 r9383 45 45 * with the timezone lookup, is very expensive. 46 46 */ 47 private static GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC")); 47 private static final GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC")); 48 private static final GregorianCalendar calendarLocale = new GregorianCalendar(TimeZone.getDefault()); 48 49 private static final DatatypeFactory XML_DATE; 49 50 50 51 static { 51 52 calendar.setTimeInMillis(0); 53 calendarLocale.setTimeInMillis(0); 52 54 53 55 DatatypeFactory fact = null; … … 78 80 if (checkLayout(str, "xxxx-xx-xxTxx:xx:xxZ") || 79 81 checkLayout(str, "xxxx-xx-xxTxx:xx:xx") || 82 checkLayout(str, "xxxx:xx:xx xx:xx:xx") || 80 83 checkLayout(str, "xxxx-xx-xx xx:xx:xx UTC") || 81 84 checkLayout(str, "xxxx-xx-xxTxx:xx:xx+xx:00") || 82 85 checkLayout(str, "xxxx-xx-xxTxx:xx:xx-xx:00")) { 83 calendar.set( 86 final Calendar c = checkLayout(str, "xxxx:xx:xx xx:xx:xx") ? calendarLocale : calendar; // consider EXIF date in default timezone 87 c.set( 84 88 parsePart4(str, 0), 85 89 parsePart2(str, 5)-1, … … 92 96 int plusHr = parsePart2(str, 20); 93 97 int mul = str.charAt(19) == '+' ? -3600000 : 3600000; 94 return c alendar.getTimeInMillis()+plusHr*mul;98 return c.getTimeInMillis()+plusHr*mul; 95 99 } 96 100 97 return c alendar.getTimeInMillis();101 return c.getTimeInMillis(); 98 102 } else if (checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxxZ") || 99 103 checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxx") || 104 checkLayout(str, "xxxx:xx:xx xx:xx:xx.xxx") || 100 105 checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxx+xx:00") || 101 106 checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxx-xx:00")) { 102 calendar.set( 107 final Calendar c = checkLayout(str, "xxxx:xx:xx xx:xx:xx.xxx") ? calendarLocale : calendar; // consider EXIF date in default timezone 108 c.set( 103 109 parsePart4(str, 0), 104 110 parsePart2(str, 5)-1, … … 112 118 } 113 119 114 return c alendar.getTimeInMillis() + millis;120 return c.getTimeInMillis() + millis; 115 121 } else { 116 122 // example date format "18-AUG-08 13:33:03" -
trunk/src/org/openstreetmap/josm/tools/date/PrimaryDateParser.java
r9078 r9383 17 17 * 18 18 * @author Brett Henderson 19 * @deprecated Use {@link DateUtils} instead! 19 20 */ 21 @Deprecated 20 22 public class PrimaryDateParser { 21 23 private DatatypeFactory datatypeFactory; -
trunk/test/unit/org/openstreetmap/josm/tools/date/DateUtilsTest.java
r8876 r9383 4 4 import static org.junit.Assert.assertEquals; 5 5 6 import java.util.TimeZone; 7 8 import org.junit.BeforeClass; 6 9 import org.junit.Test; 7 10 … … 10 13 */ 11 14 public class DateUtilsTest { 15 16 /** 17 * Setup test. 18 */ 19 @BeforeClass 20 public static void setUp() { 21 TimeZone.setDefault(TimeZone.getTimeZone("GMT+8:00")); 22 } 12 23 13 24 /** … … 26 37 assertEquals(1417298930000L, DateUtils.fromString("2014-11-29 22:08:50 UTC").getTime()); 27 38 } 39 40 /** 41 * Test to parse date as used in EXIF structures. 42 */ 43 @Test 44 public void testExifDate() { 45 assertEquals(1443038712000L - 8 * 3600 * 1000, DateUtils.fromString("2015:09:23 20:05:12").getTime()); 46 assertEquals(1443038712888L - 8 * 3600 * 1000, DateUtils.fromString("2015:09:23 20:05:12.888").getTime()); 47 } 48 49 /** 50 * Test to parse date as used in GPX files 51 */ 52 @Test 53 public void testGPXDate() { 54 assertEquals(1277465405000L, DateUtils.fromString("2010-06-25T11:30:05.000Z").getTime()); 55 } 56 57 /** 58 * Test to parse date as defined in <a href="https://tools.ietf.org/html/rfc3339">RFC 3339</a> 59 */ 60 @Test 61 public void testRfc3339() { 62 // examples taken from RFC 63 assertEquals(482196050520L, DateUtils.fromString("1985-04-12T23:20:50.52Z").getTime()); 64 assertEquals(851042397000L, DateUtils.fromString("1996-12-19T16:39:57-08:00").getTime()); 65 assertEquals(-1041337172130L, DateUtils.fromString("1937-01-01T12:00:27.87+00:20").getTime()); 66 } 28 67 }
Note:
See TracChangeset
for help on using the changeset viewer.