Changeset 17718 in josm


Ignore:
Timestamp:
2021-04-09T00:19:56+02:00 (4 years ago)
Author:
simon04
Message:

see #14176 - Utils.getDurationString in GpxLayer.formatTimespan

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/layer/GpxLayer.java

    r17715 r17718  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.gui.layer;
    3 
    4 import static org.openstreetmap.josm.tools.I18n.tr;
    5 import static org.openstreetmap.josm.tools.I18n.trn;
    6 
    7 import java.awt.Color;
    8 import java.awt.Dimension;
    9 import java.awt.Graphics2D;
    10 import java.awt.event.ActionEvent;
    11 import java.io.File;
    12 import java.time.Instant;
    13 import java.time.format.DateTimeFormatter;
    14 import java.time.format.FormatStyle;
    15 import java.time.temporal.ChronoUnit;
    16 import java.util.ArrayList;
    17 import java.util.Arrays;
    18 import java.util.Collections;
    19 import java.util.Date;
    20 import java.util.List;
    21 import java.util.NoSuchElementException;
    22 import java.util.stream.Collectors;
    23 
    24 import javax.swing.AbstractAction;
    25 import javax.swing.Action;
    26 import javax.swing.Icon;
    27 import javax.swing.JScrollPane;
    28 import javax.swing.SwingUtilities;
    293
    304import org.openstreetmap.josm.actions.AutoScaleAction;
     
    6842import org.openstreetmap.josm.tools.date.DateUtils;
    6943
     44import javax.swing.AbstractAction;
     45import javax.swing.Action;
     46import javax.swing.Icon;
     47import javax.swing.JScrollPane;
     48import javax.swing.SwingUtilities;
     49import java.awt.Color;
     50import java.awt.Dimension;
     51import java.awt.Graphics2D;
     52import java.awt.event.ActionEvent;
     53import java.io.File;
     54import java.time.Instant;
     55import java.time.format.DateTimeFormatter;
     56import java.time.format.FormatStyle;
     57import java.util.ArrayList;
     58import java.util.Arrays;
     59import java.util.Collections;
     60import java.util.Date;
     61import java.util.List;
     62import java.util.NoSuchElementException;
     63import java.util.stream.Collectors;
     64
     65import static org.openstreetmap.josm.tools.I18n.tr;
     66import static org.openstreetmap.josm.tools.I18n.trn;
     67
    7068/**
    7169 * A layer that displays data from a Gpx file / the OSM gpx downloads.
     
    178176            DateTimeFormatter tf = DateUtils.getTimeFormatter(FormatStyle.SHORT);
    179177            ts += earliestDate + ' ';
    180             ts += tf.format(bounds[0]) + " - " + tf.format(bounds[1]);
     178            ts += tf.format(bounds[0]) + " \u2013 " + tf.format(bounds[1]);
    181179        } else {
    182180            DateTimeFormatter dtf = DateUtils.getDateTimeFormatter(FormatStyle.SHORT, FormatStyle.MEDIUM);
    183             ts += dtf.format(bounds[0]) + " - " + dtf.format(bounds[1]);
    184         }
    185 
    186         long diff = ChronoUnit.SECONDS.between(bounds[1], bounds[0]);
    187         ts += String.format(" (%d:%02d)", diff / 3600, (diff % 3600) / 60);
     181            ts += dtf.format(bounds[0]) + " \u2013 " + dtf.format(bounds[1]);
     182        }
     183
     184        ts += String.format(" (%s)", Utils.getDurationString(bounds[1].toEpochMilli() - bounds[0].toEpochMilli()));
    188185        return ts;
    189186    }
  • trunk/test/unit/org/openstreetmap/josm/gui/layer/GpxLayerTest.java

    r17275 r17718  
    1010import java.awt.Color;
    1111import java.io.IOException;
     12import java.time.Instant;
    1213import java.util.ArrayList;
    1314import java.util.Collection;
    1415import java.util.HashMap;
     16import java.util.Locale;
    1517import java.util.TimeZone;
    1618
    1719import javax.swing.JScrollPane;
    1820
     21import org.junit.jupiter.api.BeforeEach;
    1922import org.junit.jupiter.api.Test;
    2023import org.junit.jupiter.api.extension.RegisterExtension;
     
    3033import org.openstreetmap.josm.io.GpxReaderTest;
    3134import org.openstreetmap.josm.testutils.JOSMTestRules;
     35import org.openstreetmap.josm.tools.date.DateUtils;
    3236import org.xml.sax.SAXException;
    3337
     
    4549    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    4650    public JOSMTestRules test = new JOSMTestRules().main().projection().i18n().metricSystem();
     51
     52    /**
     53     * Setup test.
     54     */
     55    @BeforeEach
     56    void setUp() {
     57        Locale.setDefault(Locale.ROOT);
     58        DateUtils.PROP_ISO_DATES.put(true);
     59    }
    4760
    4861    private static String getHtml(GpxLayer layer) {
     
    167180                     "        </td>\n" +
    168181                     "        <td>\n" +
    169                      "          1/3/16 11:59 AM - 12:00 PM (0:00)\n" +
     182                     "          2016-01-03 11:59:58 &#8211; 12:00:00 (2.0 s)\n" +
    170183                     "        </td>\n" +
    171184                     "        <td>\n" +
     
    197210                new GpxTrack(new ArrayList<Collection<WayPoint>>(), new HashMap<String, Object>())));
    198211
    199         assertEquals("1/3/16 11:59 AM - 12:00 PM (0:00)", GpxLayer.getTimespanForTrack(getMinimalGpxData().tracks.iterator().next()));
     212        assertEquals("2016-01-03 11:59:58 \u2013 12:00:00 (2.0 s)", GpxLayer.getTimespanForTrack(getMinimalGpxData().tracks.iterator().next()));
    200213
    201214        TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin"));
    202         assertEquals("1/3/16 12:59 PM - 1:00 PM (0:00)", GpxLayer.getTimespanForTrack(getMinimalGpxData().tracks.iterator().next()));
     215        assertEquals("2016-01-03 12:59:58 \u2013 13:00:00 (2.0 s)", GpxLayer.getTimespanForTrack(getMinimalGpxData().tracks.iterator().next()));
     216    }
     217
     218    /**
     219     * Unit test of {@link GpxLayer#formatTimespan}.
     220     */
     221    @Test
     222    void testFormatTimespan() {
     223        Instant[] timespan = {Instant.parse("2021-03-01T17:53:16Z"), Instant.parse("2021-04-03T08:19:19Z")};
     224        assertEquals("2021-03-01T17:53:16 \u2013 2021-04-03T08:19:19 (32 days 14 h)", GpxLayer.formatTimespan(timespan));
    203225    }
    204226
Note: See TracChangeset for help on using the changeset viewer.