Ticket #13071: patch-fix-13071-2.patch

File patch-fix-13071-2.patch, 9.3 KB (added by michael2402, 10 years ago)
  • src/org/openstreetmap/josm/tools/date/DateUtils.java

    diff --git a/src/org/openstreetmap/josm/tools/date/DateUtils.java b/src/org/openstreetmap/josm/tools/date/DateUtils.java
    index c148194..c8a3245 100644
    a b public final class DateUtils {  
    3434     */
    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.
    4339     * @since 7299
    public final class DateUtils {  
    5147     * with the timezone lookup, is very expensive.
    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;
    5655
    public final class DateUtils {  
    6766        XML_DATE = fact;
    6867    }
    6968
     69    protected DateUtils() {
     70        // Hide default constructor for utils classes
     71    }
     72
    7073    /**
    7174     * Parses XML date quickly, regardless of current locale.
    7275     * @param str The XML date as string
    7376     * @return The date
    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    }
    7982
    public final class DateUtils {  
    8386     * @return The date in milliseconds since epoch
    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") ||
    8992                checkLayout(str, "xxxx-xx-xxTxx:xx:xx") ||
    public final class DateUtils {  
    9396                checkLayout(str, "xxxx-xx-xxTxx:xx:xx-xx") ||
    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),
    99107                parsePart2(str, 5)-1,
    public final class DateUtils {  
    115123                checkLayout(str, "xxxx:xx:xx xx:xx:xx.xxx") ||
    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),
    121130                parsePart2(str, 5)-1,
    public final class DateUtils {  
    133142        } else {
    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)
    139147                return d.getTime();
    public final class DateUtils {  
    146154        }
    147155    }
    148156
     157    private static Calendar getLocalCalendar() {
     158        final Calendar c = calendarLocale;
     159        c.setTimeZone(TimeZone.getDefault());
     160        return c;
     161    }
     162
    149163    private static String toXmlFormat(GregorianCalendar cal) {
    150164        XMLGregorianCalendar xgc = XML_DATE.newXMLGregorianCalendar(cal);
    151165        if (cal.get(Calendar.MILLISECOND) == 0) {
    public final class DateUtils {  
    315329        CheckParameterUtil.ensureParameterNotNull(datetime, "datetime");
    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}
  • test/unit/org/openstreetmap/josm/tools/ExifReaderTest.java

    diff --git a/test/unit/org/openstreetmap/josm/tools/ExifReaderTest.java b/test/unit/org/openstreetmap/josm/tools/ExifReaderTest.java
    index 7ae3e3a..658ba4f 100644
    a b import java.util.Date;  
    1414import java.util.GregorianCalendar;
    1515import java.util.TimeZone;
    1616
    17 import org.junit.After;
    1817import org.junit.Before;
    1918import org.junit.Rule;
    2019import org.junit.Test;
    import org.openstreetmap.josm.TestUtils;  
    2221import org.openstreetmap.josm.data.coor.LatLon;
    2322import org.openstreetmap.josm.testutils.JOSMTestRules;
    2423import org.openstreetmap.josm.tools.date.DateUtils;
    25 import org.openstreetmap.josm.tools.date.DateUtilsTest;
    2624
    2725import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    2826
    public class ExifReaderTest {  
    3634     */
    3735    @Rule
    3836    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    39     public JOSMTestRules test = new JOSMTestRules();
     37    public JOSMTestRules test = new JOSMTestRules().timeout(60000);
    4038
    4139    private File orientationSampleFile, directionSampleFile;
    4240
    public class ExifReaderTest {  
    4745    public void setUp() {
    4846        directionSampleFile = new File("data_nodist/exif-example_direction.jpg");
    4947        orientationSampleFile = new File("data_nodist/exif-example_orientation=6.jpg");
    50         DateUtilsTest.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
    51     }
    52 
    53     /**
    54      * Clean {@link DateUtils} state
    55      */
    56     @After
    57     public void done() {
    58         DateUtilsTest.setTimeZone(DateUtils.UTC);
    5948    }
    6049
    6150    /**
    public class ExifReaderTest {  
    6554    @Test
    6655    public void testReadTime() throws ParseException {
    6756        Date date = ExifReader.readTime(directionSampleFile);
     57        assertEquals(new GregorianCalendar(2010, Calendar.MAY, 15, 17, 12, 05).getTime(), date);
     58
     59        TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin"));
     60        date = ExifReader.readTime(directionSampleFile);
     61        TimeZone.setDefault(DateUtils.UTC);
    6862        assertEquals(new GregorianCalendar(2010, Calendar.MAY, 15, 15, 12, 05).getTime(), date);
    6963    }
    7064
    public class ExifReaderTest {  
    7670    public void testReadTimeSubSecond1() throws ParseException {
    7771        Date date = ExifReader.readTime(new File("data_nodist/IMG_20150711_193419.jpg"));
    7872        String dateStr = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").format(date);
     73        assertEquals("2015-07-11T19:34:19.100", dateStr);
     74
     75        TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin"));
     76        date = ExifReader.readTime(new File("data_nodist/IMG_20150711_193419.jpg"));
     77        TimeZone.setDefault(DateUtils.UTC);
     78        dateStr = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").format(date);
    7979        assertEquals("2015-07-11T17:34:19.100", dateStr);
    8080    }
    8181
    public class ExifReaderTest {  
    116116    public void testTicket11685() throws IOException {
    117117        File file = new File(TestUtils.getRegressionDataFile(11685, "2015-11-08_15-33-27-Xiaomi_YI-Y0030832.jpg"));
    118118        String dateStr = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").format(ExifReader.readTime(file));
    119         assertEquals("2015-11-08T14:33:27.500", dateStr);
     119        assertEquals("2015-11-08T15:33:27.500", dateStr);
    120120    }
    121121}
  • test/unit/org/openstreetmap/josm/tools/UtilsTest.java

    diff --git a/test/unit/org/openstreetmap/josm/tools/UtilsTest.java b/test/unit/org/openstreetmap/josm/tools/UtilsTest.java
    index bffa099..cbe2b8b 100644
    a b import java.util.Locale;  
    1212import org.junit.Assert;
    1313import org.junit.Test;
    1414import org.openstreetmap.josm.Main;
     15import org.openstreetmap.josm.testutils.JOSMTestRules;
    1516
    1617/**
    1718 * Unit tests of {@link Utils} class.
    1819 */
    1920public class UtilsTest {
     21    /**
     22     * Use default, basic test rules.
     23     */
     24    public JOSMTestRules rules = new JOSMTestRules();
    2025
    2126    /**
    2227     * Test of {@link Utils#strip} method.
  • test/unit/org/openstreetmap/josm/tools/date/DateUtilsTest.java

    diff --git a/test/unit/org/openstreetmap/josm/tools/date/DateUtilsTest.java b/test/unit/org/openstreetmap/josm/tools/date/DateUtilsTest.java
    index 9e55379..06803a7 100644
    a b public class DateUtilsTest {  
    3333     * @param zone the timezone to use
    3434     */
    3535    public static void setTimeZone(TimeZone zone) {
    36         DateUtils.setTimeZone(zone);
    3736        TimeZone.setDefault(zone);
    3837    }
    3938