Changeset 14434 in josm


Ignore:
Timestamp:
2018-11-20T01:13:10+01:00 (3 weeks ago)
Author:
Don-vip
Message:

fix #16995 - fix timestamp in GPX exports (patch by cmuelle8) + use Java 8 unsigned int API

Location:
trunk/src/org/openstreetmap/josm
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/relation/ExportRelationToGpxAction.java

    r14397 r14434  
    2121import java.util.Set;
    2222import java.util.Stack;
     23import java.util.concurrent.TimeUnit;
    2324
    2425import org.openstreetmap.josm.actions.GpxExportAction;
     
    174175        GpxData gpxData = new GpxData();
    175176        String layerName = " (GPX export)";
    176         long time = System.currentTimeMillis()-24*3600*1000;
     177        long time = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()) - 24*3600;
    177178
    178179        if (!flat.isEmpty()) {
     
    197198                            Relation r = Way.getParentRelations(Arrays.asList(flat.get(i).getWay()))
    198199                                    .stream().filter(relsFound::contains).findFirst().orElseGet(null);
    199                             if (r != null)
     200                            if (r != null) {
    200201                                trkAttr.put("name", r.getName() != null ? r.getName() : r.getId());
     202                                trkAttr.put("desc", tr("based on osm route relation data, timestamps are synthetic"));
     203                            }
    201204                            GpxData.ensureUniqueName(trkAttr, names);
    202205                        }
     
    205208                            Collections.reverse(ln);
    206209                        for (Node n: ln) {
    207                             trkseg.add(OsmDataLayer.nodeToWayPoint(n, time));
    208                             time += 1000;
     210                            trkseg.add(OsmDataLayer.nodeToWayPoint(n, TimeUnit.SECONDS.toMillis(time)));
     211                            time += 1;
    209212                        }
    210213                    }
  • trunk/src/org/openstreetmap/josm/data/gpx/WayPoint.java

    r14159 r14434  
    129129
    130130    /**
    131      * Sets the {@link #time} field as well as the {@link #PT_TIME} attribute to the specified time
     131     * Sets the {@link #time} field as well as the {@link #PT_TIME} attribute to the specified time.
    132132     *
    133133     * @param time the time to set
     
    140140
    141141    /**
    142      * Convert the time stamp of the waypoint into seconds from the epoch
    143      */
     142     * Convert the time stamp of the waypoint into seconds from the epoch.
     143     *
     144     * @deprecated call {@link #setTimeFromAttribute()} directly if you need this
     145     */
     146    @Deprecated
    144147    public void setTime() {
    145148        setTimeFromAttribute();
     
    147150
    148151    /**
    149      * Set the the time stamp of the waypoint into seconds from the epoch,
    150      * @param time millisecond from the epoch
     152     * Sets the {@link #time} field as well as the {@link #PT_TIME} attribute to the specified time.
     153     *
     154     * @param ts seconds from the epoch
    151155     * @since 13210
    152156     */
    153     public void setTime(long time) {
    154         this.time = time / 1000.;
     157    public void setTime(long ts) {
     158        this.time = ts;
     159        this.attr.put(PT_TIME, DateUtils.fromTimestamp(ts));
     160    }
     161
     162    /**
     163     * Sets the {@link #time} field as well as the {@link #PT_TIME} attribute to the specified time.
     164     *
     165     * @param ts milliseconds from the epoch
     166     * @since 14434
     167     */
     168    public void setTimeInMillis(long ts) {
     169        this.time = ts / 1000.;
     170        this.attr.put(PT_TIME, DateUtils.fromTimestampInMillis(ts));
    155171    }
    156172
  • trunk/src/org/openstreetmap/josm/data/osm/AbstractPrimitive.java

    r13913 r14434  
    193193    protected int changesetId;
    194194
     195    /**
     196     * A time value, measured in seconds from the epoch, or in other words,
     197     * a number of seconds that have passed since 1970-01-01T00:00:00Z
     198     */
    195199    protected int timestamp;
    196200
     
    327331    @Override
    328332    public Date getTimestamp() {
    329         return new Date(TimeUnit.SECONDS.toMillis(timestamp));
     333        return new Date(TimeUnit.SECONDS.toMillis(Integer.toUnsignedLong(timestamp)));
    330334    }
    331335
  • trunk/src/org/openstreetmap/josm/gui/layer/OsmDataLayer.java

    r14430 r14434  
    781781     */
    782782    public static WayPoint nodeToWayPoint(Node n) {
    783         return nodeToWayPoint(n, 0);
     783        return nodeToWayPoint(n, Long.MIN_VALUE);
    784784    }
    785785
    786786    /**
    787787     * @param n the {@code Node} to convert
    788      * @param time a time value in milliseconds from the epoch.
     788     * @param time a timestamp value in milliseconds from the epoch.
    789789     * @return {@code WayPoint} object
    790790     * @since 13210
     
    798798
    799799        try {
    800             if (time > 0) {
    801                 wpt.put(GpxConstants.PT_TIME, DateUtils.fromTimestamp(time));
    802                 wpt.setTime(time);
     800            if (time > Long.MIN_VALUE) {
     801                wpt.setTimeInMillis(time);
    803802            } else if (n.hasKey(GpxConstants.PT_TIME)) {
    804                 wpt.put(GpxConstants.PT_TIME, DateUtils.fromString(n.get(GpxConstants.PT_TIME)));
    805                 wpt.setTime();
     803                wpt.setTime(DateUtils.fromString(n.get(GpxConstants.PT_TIME)));
    806804            } else if (!n.isTimestampEmpty()) {
    807                 wpt.put(GpxConstants.PT_TIME, DateUtils.fromTimestamp(n.getRawTimestamp()));
    808                 wpt.setTime();
     805                wpt.setTime(Integer.toUnsignedLong(n.getRawTimestamp()));
    809806            }
    810807        } catch (UncheckedParseException e) {
  • trunk/src/org/openstreetmap/josm/io/GpxReader.java

    r14010 r14434  
    445445                case "desc":
    446446                    currentWayPoint.put(localName, accumulator.toString());
    447                     currentWayPoint.setTime();
     447                    currentWayPoint.setTimeFromAttribute();
    448448                    break;
    449449                case "rtept":
  • trunk/src/org/openstreetmap/josm/io/nmea/NmeaReader.java

    r14083 r14434  
    525525            if (ps.pWp != currentwp) {
    526526                if (ps.pWp != null) {
    527                     ps.pWp.setTime();
     527                    ps.pWp.setTimeFromAttribute();
    528528                }
    529529                ps.pWp = currentwp;
  • trunk/src/org/openstreetmap/josm/tools/date/DateUtils.java

    r14159 r14434  
    153153     * @since 14055
    154154     */
    155     public static synchronized String fromTimestamp(long timestamp) {
    156         final ZonedDateTime temporal = Instant.ofEpochMilli(TimeUnit.SECONDS.toMillis(timestamp)).atZone(ZoneOffset.UTC);
     155    public static String fromTimestamp(long timestamp) {
     156        return fromTimestampInMillis(TimeUnit.SECONDS.toMillis(timestamp));
     157    }
     158
     159    /**
     160     * Formats a date to the XML UTC format regardless of current locale.
     161     * @param timestamp number of milliseconds since the epoch
     162     * @return The formatted date
     163     * @since 14434
     164     */
     165    public static synchronized String fromTimestampInMillis(long timestamp) {
     166        final ZonedDateTime temporal = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.UTC);
    157167        return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(temporal);
    158168    }
     
    164174     */
    165175    public static synchronized String fromTimestamp(int timestamp) {
    166         return fromTimestamp((long) timestamp);
     176        return fromTimestamp(Integer.toUnsignedLong(timestamp));
    167177    }
    168178
Note: See TracChangeset for help on using the changeset viewer.