Skip to content

Commit

Permalink
JodaOrg#524 Don't change of time zone Id in a call to DateTimeZone.forID
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Wolkenstein authored and Thomas Wolkenstein committed Feb 20, 2020
1 parent 767c94e commit fbfbf7c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
34 changes: 26 additions & 8 deletions src/main/java/org/joda/time/tz/ZoneInfoProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import java.lang.ref.SoftReference;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.AbstractMap.SimpleEntry;
import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -157,22 +159,33 @@ public DateTimeZone getZone(String id) {
return null;
}

if (obj instanceof SoftReference<?>) {
if (obj instanceof Entry) {
// If this point is reached, mapping must link to another.
@SuppressWarnings("unchecked")
Entry<String, SoftReference<DateTimeZone>> entry = (Entry<String, SoftReference<DateTimeZone>>) obj;
SoftReference<DateTimeZone> ref = entry.getValue();
DateTimeZone tz = ref.get();
if (tz != null) {
return tz;
}
// Reference cleared; load data again.
return loadZoneData(entry.getKey(), id);
} else if (obj instanceof SoftReference<?>) {
@SuppressWarnings("unchecked")
SoftReference<DateTimeZone> ref = (SoftReference<DateTimeZone>) obj;
DateTimeZone tz = ref.get();
if (tz != null) {
return tz;
}
// Reference cleared; load data again.
return loadZoneData(id);
return loadZoneData(id, id);
} else if (id.equals(obj)) {
// Load zone data for the first time.
return loadZoneData(id);
return loadZoneData(id, id);
}

// If this point is reached, mapping must link to another.
return getZone((String)obj);
return loadZoneData((String) obj, id);
}

/**
Expand Down Expand Up @@ -231,15 +244,20 @@ public InputStream run() {
/**
* Loads the time zone data for one id.
*
* @param id the id to load
* @param dataId the id of the time zone in the time zone data base (for renamed time zones this is the new one)
* @param id the id of the time zone in the returned object (for renamed time zones this is the old one)
* @return the zone
*/
private DateTimeZone loadZoneData(String id) {
private DateTimeZone loadZoneData(String dataId, String id) {
InputStream in = null;
try {
in = openResource(id);
in = openResource(dataId);
DateTimeZone tz = DateTimeZoneBuilder.readFrom(in, id);
iZoneInfoMap.put(id, new SoftReference<DateTimeZone>(tz));
if (!dataId.equals(id)) {
iZoneInfoMap.put(id, new SimpleEntry<String, SoftReference<DateTimeZone>>(dataId, new SoftReference<DateTimeZone>(tz)));
} else {
iZoneInfoMap.put(id, new SoftReference<DateTimeZone>(tz));
}
return tz;
} catch (IOException ex) {
uncaughtException(ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ public void test_printParseZoneGMT() {
DateTimeFormatter f = bld.toFormatter();

DateTime dt = new DateTime(2007, 3, 4, 12, 30, 0, DateTimeZone.forID("GMT"));
assertEquals("2007-03-04 12:30 Etc/GMT", f.print(dt));
assertEquals("2007-03-04 12:30 GMT", f.print(dt));
assertEquals(dt, f.parseDateTime("2007-03-04 12:30 GMT"));
}

Expand All @@ -501,7 +501,7 @@ public void test_printParseZoneGMT_suffix() {
DateTimeFormatter f = bld.toFormatter();

DateTime dt = new DateTime(2007, 3, 4, 12, 30, 0, DateTimeZone.forID("GMT"));
assertEquals("2007-03-04 12:30 Etc/GMT]", f.print(dt));
assertEquals("2007-03-04 12:30 GMT]", f.print(dt));
assertEquals(dt, f.parseDateTime("2007-03-04 12:30 GMT]"));
}

Expand Down

0 comments on commit fbfbf7c

Please sign in to comment.