Changeset 10513 in josm
- Timestamp:
- 2016-07-03T22:16:57+02:00 (9 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/date/DateUtils.java
r10483 r10513 35 35 public static final TimeZone UTC = TimeZone.getTimeZone("UTC"); 36 36 37 protected DateUtils() {38 // Hide default constructor for utils classes39 }40 41 37 /** 42 38 * Property to enable display of ISO dates globally. … … 52 48 */ 53 49 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 */ 54 53 private static final GregorianCalendar calendarLocale = new GregorianCalendar(TimeZone.getDefault()); 55 54 private static final DatatypeFactory XML_DATE; … … 68 67 } 69 68 69 protected DateUtils() { 70 // Hide default constructor for utils classes 71 } 72 70 73 /** 71 74 * Parses XML date quickly, regardless of current locale. … … 74 77 * @throws UncheckedParseException if the date does not match any of the supported date formats 75 78 */ 76 public static synchronized Date fromString(String str) throws UncheckedParseException{79 public static synchronized Date fromString(String str) { 77 80 return new Date(tsFromString(str)); 78 81 } … … 84 87 * @throws UncheckedParseException if the date does not match any of the supported date formats 85 88 */ 86 public static synchronized long tsFromString(String str) throws UncheckedParseException{89 public static synchronized long tsFromString(String str) { 87 90 // "2007-07-25T09:26:24{Z|{+|-}01[:00]}" 88 91 if (checkLayout(str, "xxxx-xx-xxTxx:xx:xxZ") || … … 94 97 checkLayout(str, "xxxx-xx-xxTxx:xx:xx+xx:00") || 95 98 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 } 97 105 c.set( 98 106 parsePart4(str, 0), … … 116 124 checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxx+xx:00") || 117 125 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; 119 128 c.set( 120 129 parsePart4(str, 0), … … 134 143 // example date format "18-AUG-08 13:33:03" 135 144 SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yy HH:mm:ss"); 136 f.setTimeZone(calendarLocale.getTimeZone());137 145 Date d = f.parse(str, new ParsePosition(0)); 138 146 if (d != null) … … 145 153 throw new UncheckedParseException("The date string (" + str + ") could not be parsed.", ex); 146 154 } 155 } 156 157 private static Calendar getLocalCalendar() { 158 final Calendar c = calendarLocale; 159 c.setTimeZone(TimeZone.getDefault()); 160 return c; 147 161 } 148 162 … … 316 330 return getDateTimeFormat(dateStyle, timeStyle).format(datetime); 317 331 } 318 319 /**320 * Allows to override the timezone for unit tests.321 * @param zone the timezone to use322 */323 protected static synchronized void setTimeZone(TimeZone zone) {324 calendarLocale.setTimeZone(zone);325 }326 332 } -
trunk/test/unit/org/openstreetmap/josm/tools/ExifReaderTest.java
r10475 r10513 15 15 import java.util.TimeZone; 16 16 17 import org.junit.After;18 17 import org.junit.Before; 19 18 import org.junit.Rule; … … 23 22 import org.openstreetmap.josm.testutils.JOSMTestRules; 24 23 import org.openstreetmap.josm.tools.date.DateUtils; 25 import org.openstreetmap.josm.tools.date.DateUtilsTest;26 24 27 25 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; … … 37 35 @Rule 38 36 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") 39 public JOSMTestRules test = new JOSMTestRules(); 37 public JOSMTestRules test = new JOSMTestRules().timeout(60000); 40 38 41 39 private File orientationSampleFile, directionSampleFile; … … 48 46 directionSampleFile = new File("data_nodist/exif-example_direction.jpg"); 49 47 orientationSampleFile = new File("data_nodist/exif-example_orientation=6.jpg"); 50 DateUtilsTest.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));51 }52 53 /**54 * Clean {@link DateUtils} state55 */56 @After57 public void done() {58 DateUtilsTest.setTimeZone(DateUtils.UTC);59 48 } 60 49 … … 66 55 public void testReadTime() throws ParseException { 67 56 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); 68 62 assertEquals(new GregorianCalendar(2010, Calendar.MAY, 15, 15, 12, 05).getTime(), date); 69 63 } … … 77 71 Date date = ExifReader.readTime(new File("data_nodist/IMG_20150711_193419.jpg")); 78 72 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); 79 79 assertEquals("2015-07-11T17:34:19.100", dateStr); 80 80 } … … 117 117 File file = new File(TestUtils.getRegressionDataFile(11685, "2015-11-08_15-33-27-Xiaomi_YI-Y0030832.jpg")); 118 118 String dateStr = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").format(ExifReader.readTime(file)); 119 assertEquals("2015-11-08T1 4:33:27.500", dateStr);119 assertEquals("2015-11-08T15:33:27.500", dateStr); 120 120 } 121 121 } -
trunk/test/unit/org/openstreetmap/josm/tools/UtilsTest.java
r10378 r10513 13 13 import org.junit.Test; 14 14 import org.openstreetmap.josm.Main; 15 import org.openstreetmap.josm.testutils.JOSMTestRules; 15 16 16 17 /** … … 18 19 */ 19 20 public class UtilsTest { 21 /** 22 * Use default, basic test rules. 23 */ 24 public JOSMTestRules rules = new JOSMTestRules(); 20 25 21 26 /** -
trunk/test/unit/org/openstreetmap/josm/tools/date/DateUtilsTest.java
r10495 r10513 34 34 */ 35 35 public static void setTimeZone(TimeZone zone) { 36 DateUtils.setTimeZone(zone);37 36 TimeZone.setDefault(zone); 38 37 }
Note:
See TracChangeset
for help on using the changeset viewer.