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


Ignore:
Timestamp:
2016-07-03T22:16:57+02:00 (8 years ago)
Author:
Don-vip
Message:

fix #13071 - Make DateUtilsTest.testTsFromString and ExifReaderTest.test* pass (patch by michael2402) - gsoc-core

File:
1 edited

Legend:

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

    r10483 r10513  
    3535    public static final TimeZone UTC = TimeZone.getTimeZone("UTC");
    3636
    37     protected DateUtils() {
    38         // Hide default constructor for utils classes
    39     }
    40 
    4137    /**
    4238     * Property to enable display of ISO dates globally.
     
    5248     */
    5349    private static final GregorianCalendar calendar = new GregorianCalendar(UTC);
     50    /**
     51     * A shared instance to convert local times. The time zone should be set before every conversion.
     52     */
    5453    private static final GregorianCalendar calendarLocale = new GregorianCalendar(TimeZone.getDefault());
    5554    private static final DatatypeFactory XML_DATE;
     
    6867    }
    6968
     69    protected DateUtils() {
     70        // Hide default constructor for utils classes
     71    }
     72
    7073    /**
    7174     * Parses XML date quickly, regardless of current locale.
     
    7477     * @throws UncheckedParseException if the date does not match any of the supported date formats
    7578     */
    76     public static synchronized Date fromString(String str) throws UncheckedParseException {
     79    public static synchronized Date fromString(String str) {
    7780        return new Date(tsFromString(str));
    7881    }
     
    8487     * @throws UncheckedParseException if the date does not match any of the supported date formats
    8588     */
    86     public static synchronized long tsFromString(String str) throws UncheckedParseException {
     89    public static synchronized long tsFromString(String str) {
    8790        // "2007-07-25T09:26:24{Z|{+|-}01[:00]}"
    8891        if (checkLayout(str, "xxxx-xx-xxTxx:xx:xxZ") ||
     
    9497                checkLayout(str, "xxxx-xx-xxTxx:xx:xx+xx:00") ||
    9598                checkLayout(str, "xxxx-xx-xxTxx:xx:xx-xx:00")) {
    96             final Calendar c = checkLayout(str, "xxxx:xx:xx xx:xx:xx") ? calendarLocale : calendar; // consider EXIF date in default timezone
     99            final Calendar c; // consider EXIF date in default timezone
     100            if (checkLayout(str, "xxxx:xx:xx xx:xx:xx")) {
     101                c = getLocalCalendar();
     102            } else {
     103                c = calendar;
     104            }
    97105            c.set(
    98106                parsePart4(str, 0),
     
    116124                checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxx+xx:00") ||
    117125                checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxx-xx:00")) {
    118             final Calendar c = checkLayout(str, "xxxx:xx:xx xx:xx:xx.xxx") ? calendarLocale : calendar; // consider EXIF date in default timezone
     126            // consider EXIF date in default timezone
     127            final Calendar c = checkLayout(str, "xxxx:xx:xx xx:xx:xx.xxx") ? getLocalCalendar() : calendar;
    119128            c.set(
    120129                parsePart4(str, 0),
     
    134143            // example date format "18-AUG-08 13:33:03"
    135144            SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yy HH:mm:ss");
    136             f.setTimeZone(calendarLocale.getTimeZone());
    137145            Date d = f.parse(str, new ParsePosition(0));
    138146            if (d != null)
     
    145153            throw new UncheckedParseException("The date string (" + str + ") could not be parsed.", ex);
    146154        }
     155    }
     156
     157    private static Calendar getLocalCalendar() {
     158        final Calendar c = calendarLocale;
     159        c.setTimeZone(TimeZone.getDefault());
     160        return c;
    147161    }
    148162
     
    316330        return getDateTimeFormat(dateStyle, timeStyle).format(datetime);
    317331    }
    318 
    319     /**
    320      * Allows to override the timezone for unit tests.
    321      * @param zone the timezone to use
    322      */
    323     protected static synchronized void setTimeZone(TimeZone zone) {
    324         calendarLocale.setTimeZone(zone);
    325     }
    326332}
Note: See TracChangeset for help on using the changeset viewer.