Changeset 1721 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2009-07-02T18:02:27+02:00 (15 years ago)
Author:
david
Message:

trac 2784: properly take account of timezones in GPX files.
Previously it wasn't relevant because audio was relative and synchronized,
but in the new case of matching of timestamps to file times it is absolute and therefore important.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/gpx/WayPoint.java

    r1425 r1721  
    77import java.text.SimpleDateFormat;
    88import java.util.Date;
     9import java.util.regex.Pattern;
    910import java.awt.Color;
    1011
     
    3637     */
    3738    public final static SimpleDateFormat GPXTIMEFMT =
    38         new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); // ignore timezone
    39     public final static SimpleDateFormat GPXTIMEFMT2 =
    40         new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); // ignore timezone
     39        new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
     40    public final static SimpleDateFormat GPXTIMEFMT_nofrac =
     41        new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
     42    public final static SimpleDateFormat GPXTIMEFMT_tz =
     43        new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
     44    public final static SimpleDateFormat GPXTIMEFMT_tz_nofrac =
     45        new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    4146
    42     public void setTime() {
     47        private final static Pattern colontz = Pattern.compile(".*[+-][0-9][0-9]:[0-9][0-9]\\z");
     48        private final static Pattern colontzreplacement = Pattern.compile("([+-][0-9][0-9]):([0-9][0-9])\\z");
     49
     50        public void setTime() {
    4351        if (! attr.containsKey("time")) {
    4452            return;
    4553        }
    46         Date d = GPXTIMEFMT.parse(attr.get("time").toString(), new ParsePosition(0));
     54        String timestring = attr.get("time").toString();
     55       
     56        /* make the string timzeone be conanonical - unfortunately the allowed timezone in a
     57         * GPX is Z or +/-hh:mm whereas in simpledateformat it is +/-hhmm only (no colon)
     58         * If no timezone is given, the time will be interpreted as local time by parse. */
     59        if (timestring.substring(timestring.length() - 1).equals("Z")) {
     60                timestring = timestring.substring(0, timestring.length() - 1) + "+0000";
     61        } else if (colontz.matcher(timestring).matches()) {
     62                timestring = colontzreplacement.matcher(timestring).replaceFirst("$1$2");
     63        }
     64        Date d = GPXTIMEFMT_tz.parse(timestring, new ParsePosition(0));
    4765        if (d == null) {
    48             d = GPXTIMEFMT2.parse(attr.get("time").toString(), new ParsePosition(0));
     66                d = GPXTIMEFMT_tz_nofrac.parse(timestring, new ParsePosition(0));
     67                if (d == null) {
     68                        /* try without a zimezone indication */
     69                        d = GPXTIMEFMT.parse(timestring, new ParsePosition(0));                 
     70                        if (d == null) {
     71                                d = GPXTIMEFMT_nofrac.parse(timestring, new ParsePosition(0));                 
     72                        }
     73                // date has parsed in local time, and been adjusted to UTC by parse
     74            }
    4975        }
    5076        if (d != null /* parsing ok */) {
    5177            time = d.getTime() / 1000.0; /* ms => seconds */
    5278        }
    53     }
    54 
     79}
    5580    /**
    5681     * Convert a time stamp of the waypoint from the <cmt> or <desc> field
Note: See TracChangeset for help on using the changeset viewer.