Changeset 17653 in josm


Ignore:
Timestamp:
2021-03-24T00:15:51+01:00 (3 years ago)
Author:
simon04
Message:

see #14176 - DateUtils.parseInstant

File:
1 edited

Legend:

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

    r17114 r17653  
    6767     */
    6868    public static long tsFromString(String str) {
     69        return parseInstant(str).toEpochMilli();
     70    }
     71
     72    /**
     73     * Parses the given date string quickly, regardless of current locale.
     74     * @param str the date string
     75     * @return the parsed instant
     76     */
     77    public static Instant parseInstant(String str) {
    6978        // "2007-07-25T09:26:24{Z|{+|-}01[:00]}"
    7079        if (checkLayout(str, "xxxx-xx-xx") ||
     
    7685                    str.length() > 8 ? parsePart2(str, 8) : 1,
    7786                    0, 0, 0, 0, ZoneOffset.UTC);
    78             return local.toInstant().toEpochMilli();
     87            return local.toInstant();
    7988        } else if (checkLayout(str, "xxxx-xx-xxTxx:xx:xxZ") ||
    8089                checkLayout(str, "xxxx-xx-xxTxx:xx:xx") ||
     
    98107            if (str.length() == 22 || str.length() == 25) {
    99108                final int plusHr = parsePart2(str, 20);
    100                 return local.plusHours(str.charAt(19) == '+' ? -plusHr : plusHr).toInstant().toEpochMilli();
     109                return local.plusHours(str.charAt(19) == '+' ? -plusHr : plusHr).toInstant();
    101110            }
    102             return local.toInstant().toEpochMilli();
     111            return local.toInstant();
    103112        } else if (checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxxZ") ||
    104113                checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxx") ||
     
    118127            if (str.length() == 29) {
    119128                final int plusHr = parsePart2(str, 24);
    120                 return local.plusHours(str.charAt(23) == '+' ? -plusHr : plusHr).toInstant().toEpochMilli();
     129                return local.plusHours(str.charAt(23) == '+' ? -plusHr : plusHr).toInstant();
    121130            }
    122             return local.toInstant().toEpochMilli();
     131            return local.toInstant();
    123132        } else {
    124133            // example date format "18-AUG-08 13:33:03"
     
    126135            Date d = f.parse(str, new ParsePosition(0));
    127136            if (d != null)
    128                 return d.getTime();
     137                return d.toInstant();
    129138        }
    130139
    131140        try {
    132141            // slow path for fractional seconds different from millisecond precision
    133             return ZonedDateTime.parse(str).toInstant().toEpochMilli();
     142            return ZonedDateTime.parse(str).toInstant();
    134143        } catch (IllegalArgumentException | DateTimeParseException ex) {
    135144            throw new UncheckedParseException("The date string (" + str + ") could not be parsed.", ex);
Note: See TracChangeset for help on using the changeset viewer.