* including all non-preferred timezones.
* @return map of offset key to timezone
*/
public static Map<String,TimeZone> createTimeZoneByOffSetMap (List preferredTimezones, boolean sparse)
{
Map<String,TimeZone> timezoneByOffsetKeys = new GrowOnlyHashtable();
/*
Changed the STD_YEAR to use the current year instead of hardcoding
2002. If you pass a previous year to get the offset, java
will return timezone as of that year, any changes to timezones the
occured since then are not included.
This change will work correctly for most cases execpt for changes
to day light time saving changes that occur in countries in the
southern hemisphere where the day light time saving period
spans 2 calendar years. The logic that assume the day light time zone
between Feb 1 and Aug 1, might not be correct for those countries.
*/
STD_YEAR = Calendar.getInstance().get(Calendar.YEAR);
DST_YEAR = STD_YEAR;
String[] timezones = TimeZone.getAvailableIDs();
for (int i = 0; i < timezones.length; ++i) {
// iterate through the list of timezones returned by the JVM and
// construct "key" for the given timezone
TimeZone timezone = TimeZone.getTimeZone(timezones[i]);
int febOffset = timezone.getOffset(1, STD_YEAR, STD_MONTH, STD_DATE, 7, 0);
int augOffset = timezone.getOffset(1, DST_YEAR, DST_MONTH, DST_DATE, 6, 0);
String newKey = Fmt.S("%s:%s", Constants.getInteger(febOffset), Constants.getInteger(augOffset));
// now insert key/timezone into the timezone map. If the key has already
// been inserted (ie, a different timezone name has already been inserted
// for the same timezone, GMT-8 is US/SanFrancisco or US/LosAngeles), then
// check if the current value (ie timezone id) is a preferred value. If not
// then overwrite, if so, then keep the preferred value.
// The "last entry" wins mechanism needs to be maintained for backward
// compatibility, but we've added the extra "preferred timezone" concept.
// Note that this caches the value at the system level and we'll need to
// add a per-realm override and possibly a per-user override in the future.
if (timezoneByOffsetKeys.containsKey(newKey)) {
String currId = timezoneByOffsetKeys.get(newKey).getID();
if (preferredTimezones == null || !preferredTimezones.contains(currId)) {
Log.aribaweb.debug("Overwriting existing timezone key: %s value: %s with new value: %s",
newKey, currId, timezones[i]);
timezoneByOffsetKeys.put(newKey, timezone);
}
else {
Log.aribaweb.debug("Preferred timezone not overwritten: %s", currId);
}
}
else if (sparse) {
if (preferredTimezones != null && preferredTimezones.contains(timezone.getID())) {
// for sparse maps, only insert preferred timezones
timezoneByOffsetKeys.put(newKey, timezone);
}
}
else {
// not sparse so create full map by inserting non-preferred timezones
// last timezone from TimeZone.getAvailableIDs() will win for any given offset key
timezoneByOffsetKeys.put(newKey, timezone);
}
}
return timezoneByOffsetKeys;
}