Changeset 17114 in josm


Ignore:
Timestamp:
2020-10-08T19:48:57+02:00 (4 years ago)
Author:
simon04
Message:

see #19704 - Enhance DateUtils.tsFromString to fix unit tests

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/date/DateUtils.java

    r17104 r17114  
    6868    public static long tsFromString(String str) {
    6969        // "2007-07-25T09:26:24{Z|{+|-}01[:00]}"
    70         if (checkLayout(str, "xxxx-xx-xx")) {
     70        if (checkLayout(str, "xxxx-xx-xx") ||
     71                checkLayout(str, "xxxx-xx") ||
     72                checkLayout(str, "xxxx")) {
    7173            final ZonedDateTime local = ZonedDateTime.of(
    7274                    parsePart4(str, 0),
    73                     parsePart2(str, 5),
    74                     parsePart2(str, 8),
     75                    str.length() > 5 ? parsePart2(str, 5) : 1,
     76                    str.length() > 8 ? parsePart2(str, 8) : 1,
    7577                    0, 0, 0, 0, ZoneOffset.UTC);
    7678            return local.toInstant().toEpochMilli();
     
    129131        try {
    130132            // slow path for fractional seconds different from millisecond precision
    131             return Instant.parse(str).toEpochMilli();
     133            return ZonedDateTime.parse(str).toInstant().toEpochMilli();
    132134        } catch (IllegalArgumentException | DateTimeParseException ex) {
    133135            throw new UncheckedParseException("The date string (" + str + ") could not be parsed.", ex);
  • trunk/test/unit/org/openstreetmap/josm/data/osm/search/SearchCompilerTest.java

    r16618 r17114  
    432432    @Test
    433433    public void testTimestamp() throws SearchParseError {
    434         final Match search = SearchCompiler.compile("timestamp:2010/2011");
    435434        final Node n1 = new Node();
    436435        n1.setTimestamp(DateUtils.fromString("2010-01-22"));
    437         assertTrue(search.match(n1));
     436        assertTrue(SearchCompiler.compile("timestamp:2010/2011").match(n1));
     437        assertTrue(SearchCompiler.compile("timestamp:2010-01/2011").match(n1));
     438        assertTrue(SearchCompiler.compile("timestamp:2010-01-22/2011").match(n1));
     439        assertFalse(SearchCompiler.compile("timestamp:2010-01-23/2011").match(n1));
     440        assertFalse(SearchCompiler.compile("timestamp:2010/2010-01-21").match(n1));
    438441        n1.setTimestamp(DateUtils.fromString("2016-01-22"));
    439         assertFalse(search.match(n1));
     442        assertFalse(SearchCompiler.compile("timestamp:2010/2011").match(n1));
    440443    }
    441444
  • trunk/test/unit/org/openstreetmap/josm/tools/date/DateUtilsTest.java

    r17104 r17114  
    9696        assertEquals(851042397000L, DateUtils.fromString("1996-12-19T16:39:57-08:00").getTime());
    9797        assertEquals(-1041337172130L, DateUtils.fromString("1937-01-01T12:00:27.87+00:20").getTime());
     98        // (partial) dates
     99        assertEquals(482112000000L, DateUtils.fromString("1985-04-12").getTime());
     100        assertEquals(481161600000L, DateUtils.fromString("1985-04").getTime());
     101        assertEquals(473385600000L, DateUtils.fromString("1985").getTime());
    98102    }
    99103
Note: See TracChangeset for help on using the changeset viewer.