Changeset 15343 in josm for trunk


Ignore:
Timestamp:
2019-09-09T23:22:13+02:00 (5 years ago)
Author:
Don-vip
Message:

fix #18114 - support short date/time format in RTKLib .pos files

Location:
trunk
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/rtklib/RtkLibPosReader.java

    r15250 r15343  
    1212import java.util.Collection;
    1313import java.util.Collections;
     14import java.util.Date;
    1415import java.util.Locale;
    1516import java.util.Objects;
     
    4950    private static final int IDX_RATIO = 14;
    5051
    51     private final SimpleDateFormat dateTimeFmt = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS", Locale.ENGLISH); // 2019/06/08 08:23:15.000
     52    private final SimpleDateFormat dateTimeFmtS = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.ENGLISH); // 2019/06/08 08:23:15
     53    private final SimpleDateFormat dateTimeFmtL = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS", Locale.ENGLISH); // 2019/06/08 08:23:15.000
    5254
    5355    private final InputStream source;
     
    6264    public RtkLibPosReader(InputStream source) throws IOException {
    6365        this.source = Objects.requireNonNull(source);
    64         dateTimeFmt.setTimeZone(DateUtils.UTC);
     66        dateTimeFmtS.setTimeZone(DateUtils.UTC);
     67        dateTimeFmtL.setTimeZone(DateUtils.UTC);
     68    }
     69
     70    private Date parseDate(String date) throws ParseException {
     71        return (date.length() > 20 ? dateTimeFmtL : dateTimeFmtS).parse(date);
    6572    }
    6673
     
    8491                                    Double.parseDouble(fields[IDX_LON])));
    8592                            currentwp.put(GpxConstants.PT_ELE, fields[IDX_HEIGHT]);
    86                             currentwp.setTime(dateTimeFmt.parse(fields[IDX_DATE]+" "+fields[IDX_TIME]));
     93                            currentwp.setTime(parseDate(fields[IDX_DATE]+" "+fields[IDX_TIME]));
    8794                            currentwp.put(GpxConstants.RTKLIB_Q, Integer.parseInt(fields[IDX_Q]));
    8895                            currentwp.put(GpxConstants.PT_SAT, fields[IDX_NS]);
  • trunk/test/unit/org/openstreetmap/josm/io/rtklib/RtkLibPosReaderTest.java

    r15250 r15343  
    44import static org.junit.Assert.assertEquals;
    55
     6import java.io.IOException;
    67import java.nio.file.Files;
    78import java.nio.file.Paths;
     
    1920import org.openstreetmap.josm.testutils.JOSMTestRules;
    2021import org.openstreetmap.josm.tools.date.DateUtils;
     22import org.xml.sax.SAXException;
    2123
    2224import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
     
    4345    }
    4446
     47    private static RtkLibPosReader read(String path) throws IOException, SAXException {
     48        TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin"));
     49        RtkLibPosReader in = new RtkLibPosReader(Files.newInputStream(Paths.get(path)));
     50        in.parse(true);
     51        return in;
     52    }
     53
    4554    /**
    4655     * Tests reading a RTKLib pos file.
     
    4958    @Test
    5059    public void testReader() throws Exception {
    51         TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin"));
    52         RtkLibPosReader in = new RtkLibPosReader(Files.newInputStream(Paths.get("data_nodist/rtklib_example.pos")));
    53         in.parse(true);
     60        RtkLibPosReader in = read("data_nodist/rtklib_example.pos");
    5461        assertEquals(137, in.getNumberOfCoordinates());
    5562
     
    7077        assertEquals("2.2090015", wayPoints.get(0).get(GpxConstants.PT_HDOP).toString().trim());
    7178    }
     79
     80    /**
     81     * Tests reading another RTKLib pos file with different date format.
     82     * @throws Exception if any error occurs
     83     */
     84    @Test
     85    public void testReader2() throws Exception {
     86        RtkLibPosReader in = read("data_nodist/rtklib_example2.pos");
     87        assertEquals(6, in.getNumberOfCoordinates());
     88    }
    7289}
Note: See TracChangeset for help on using the changeset viewer.