diff --git a/buildSrc/build.gradle b/buildSrc/build.gradle index c6e35408..c1f9652d 100644 --- a/buildSrc/build.gradle +++ b/buildSrc/build.gradle @@ -10,5 +10,5 @@ repositories { } dependencies { - compile 'joda-time:joda-time:2.9.9' + compile 'joda-time:joda-time:2.10.1' } diff --git a/library/build.gradle b/library/build.gradle index c32fdae1..63d35a70 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -28,7 +28,7 @@ repositories { } dependencies { - api 'joda-time:joda-time:2.10:no-tzdb' + api 'joda-time:joda-time:2.10.1:no-tzdb' androidTestImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' diff --git a/library/src/androidTest/java/net/danlew/android/joda/test/TestDateTimeZone.java b/library/src/androidTest/java/net/danlew/android/joda/test/TestDateTimeZone.java index 93aec031..5a36c9f4 100644 --- a/library/src/androidTest/java/net/danlew/android/joda/test/TestDateTimeZone.java +++ b/library/src/androidTest/java/net/danlew/android/joda/test/TestDateTimeZone.java @@ -37,6 +37,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.FilePermission; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.lang.reflect.Modifier; @@ -51,6 +52,7 @@ import java.util.Date; import java.util.HashSet; import java.util.Locale; +import java.util.Random; import java.util.Set; import java.util.TimeZone; @@ -291,6 +293,7 @@ public void testForID_String_old() { map.put("AGT", "America/Argentina/Buenos_Aires"); map.put("BET", "America/Sao_Paulo"); map.put("ART", "Africa/Cairo"); + map.put("CAT", "Africa/Harare"); map.put("EAT", "Africa/Addis_Ababa"); map.put("NET", "Asia/Yerevan"); map.put("PLT", "Asia/Karachi"); @@ -307,14 +310,14 @@ public void testForID_String_old() { String value = map.get(key); TimeZone juZone = TimeZone.getTimeZone(key); DateTimeZone zone = DateTimeZone.forTimeZone(juZone); - assertEquals(value, zone.getID()); + assertEquals(DateTimeZone.forID(value), zone); // System.out.println(juZone); // System.out.println(juZone.getDisplayName()); // System.out.println(zone); // System.out.println("------"); } // gee thanks time-zone db maintainer for damaging the database - // and breaking the long-standing association with CAT + // and breaking the long-standing association with CAT/EAT TimeZone juZone = TimeZone.getTimeZone("CAT"); DateTimeZone zone = DateTimeZone.forTimeZone(juZone); assertTrue(zone.getID().equals("Africa/Harare") || zone.getID().equals("Africa/Maputo")); @@ -427,6 +430,11 @@ public void testForTimeZone_TimeZone() { zone = DateTimeZone.forTimeZone(TimeZone.getTimeZone("EST")); assertEquals("America/New_York", zone.getID()); + + TimeZone tz = TimeZone.getTimeZone("GMT-08:00"); + tz.setID("GMT-\u0660\u0668:\u0660\u0660"); + zone = DateTimeZone.forTimeZone(tz); + assertEquals("-08:00", zone.getID()); } @Test @@ -521,6 +529,40 @@ public void testProviderSecurity() { } } + // We don't use the same resource loading as the base lib! + /* + public void testZoneInfoProviderResourceLoading() { + final Set ids = new HashSet(DateTimeZone.getAvailableIDs()); + ids.remove(DateTimeZone.getDefault().getID()); + final String id = ids.toArray(new String[ids.size()])[new Random().nextInt(ids.size())]; + try { + Policy.setPolicy(new Policy() { + @Override + public PermissionCollection getPermissions(CodeSource codesource) { + Permissions p = new Permissions(); + p.add(new AllPermission()); // enable everything + return p; + } + @Override + public void refresh() { + } + @Override + public boolean implies(ProtectionDomain domain, Permission permission) { + return !(permission instanceof FilePermission) && !permission.getName().contains(id); + } + }); + System.setSecurityManager(new SecurityManager()); + // will throw IllegalArgumentException if the resource can + // not be loaded + final DateTimeZone zone = DateTimeZone.forID(id); + assertNotNull(zone); + } finally { + System.setSecurityManager(null); + Policy.setPolicy(ALLOW); + } + } + */ + static class MockNullIDSProvider implements Provider { public Set getAvailableIDs() { return null; @@ -689,15 +731,27 @@ public void testGetNameKey() { assertEquals("GMT", zone.getNameKey(TEST_TIME_WINTER)); } - static final boolean JDK6; + static final boolean JDK6PLUS; static { - boolean jdk6 = true; - try { - DateFormatSymbols.class.getMethod("getInstance", new Class[] {Locale.class}); - } catch (Exception ex) { - jdk6 = false; - } - JDK6 = jdk6; + boolean jdk6 = true; + try { + DateFormatSymbols.class.getMethod("getInstance", new Class[] { Locale.class }); + } catch (Exception ex) { + jdk6 = false; + } + JDK6PLUS = jdk6; + } + + static final boolean JDK9; + static { + boolean jdk9 = true; + try { + String str = System.getProperty("java.version"); + jdk9 = str.startsWith("9"); + } catch (Exception ex) { + jdk9 = false; + } + JDK9 = jdk9; } // Names are not the same between JDK and Android @@ -713,7 +767,7 @@ public void testGetShortName_berlin() { DateTimeZone berlin = DateTimeZone.forID("Europe/Berlin"); assertEquals("CET", berlin.getShortName(TEST_TIME_WINTER, Locale.ENGLISH)); assertEquals("CEST", berlin.getShortName(TEST_TIME_SUMMER, Locale.ENGLISH)); - if (JDK6) { + if (JDK6PLUS) { assertEquals("MEZ", berlin.getShortName(TEST_TIME_WINTER, Locale.GERMAN)); assertEquals("MESZ", berlin.getShortName(TEST_TIME_SUMMER, Locale.GERMAN)); } else { @@ -746,18 +800,29 @@ public void testGetName() { assertEquals("British Summer Time", zone.getName(TEST_TIME_SUMMER, Locale.ENGLISH)); } - public void testGetName_berlin() { + public void testGetName_berlin_english() { DateTimeZone berlin = DateTimeZone.forID("Europe/Berlin"); - assertEquals("Central European Time", berlin.getName(TEST_TIME_WINTER, Locale.ENGLISH)); - assertEquals("Central European Summer Time", berlin.getName(TEST_TIME_SUMMER, Locale.ENGLISH)); - if (JDK6) { - assertEquals("Mitteleurop\u00e4ische Zeit", berlin.getName(TEST_TIME_WINTER, Locale.GERMAN)); - assertEquals("Mitteleurop\u00e4ische Sommerzeit", berlin.getName(TEST_TIME_SUMMER, Locale.GERMAN)); + if (JDK9) { + assertEquals("Central European Standard Time", berlin.getName(TEST_TIME_WINTER, Locale.ENGLISH)); } else { - assertEquals("Zentraleurop\u00e4ische Zeit", berlin.getName(TEST_TIME_WINTER, Locale.GERMAN)); - assertEquals("Zentraleurop\u00e4ische Sommerzeit", berlin.getName(TEST_TIME_SUMMER, Locale.GERMAN)); + assertEquals("Central European Time", berlin.getName(TEST_TIME_WINTER, Locale.ENGLISH)); } - } + assertEquals("Central European Summer Time", berlin.getName(TEST_TIME_SUMMER, Locale.ENGLISH)); + } + + public void testGetName_berlin_german() { + DateTimeZone berlin = DateTimeZone.forID("Europe/Berlin"); + if (JDK9) { + assertEquals("Mitteleurop\u00e4ische Normalzeit", berlin.getName(TEST_TIME_WINTER, Locale.GERMAN)); + assertEquals("Mitteleurop\u00e4ische Sommerzeit", berlin.getName(TEST_TIME_SUMMER, Locale.GERMAN)); + } else if (JDK6PLUS) { + assertEquals("Mitteleurop\u00e4ische Zeit", berlin.getName(TEST_TIME_WINTER, Locale.GERMAN)); + assertEquals("Mitteleurop\u00e4ische Sommerzeit", berlin.getName(TEST_TIME_SUMMER, Locale.GERMAN)); + } else { + assertEquals("Zentraleurop\u00e4ische Zeit", berlin.getName(TEST_TIME_WINTER, Locale.GERMAN)); + assertEquals("Zentraleurop\u00e4ische Sommerzeit", berlin.getName(TEST_TIME_SUMMER, Locale.GERMAN)); + } + } */ @Test @@ -1024,6 +1089,45 @@ public void testToString() { assertEquals("UTC", DateTimeZone.UTC.toString()); } + //----------------------------------------------------------------------- + @Test + public void testDublin() { + DateTimeZone zone = DateTimeZone.forID("Europe/Dublin"); + DateTime winter = new DateTime(2018, 1, 1, 0, 0, 0, 0, zone); + assertEquals(0, zone.getStandardOffset(winter.getMillis())); + assertEquals(0, zone.getOffset(winter.getMillis())); + assertEquals(true, zone.isStandardOffset(winter.getMillis())); + assertEquals("Greenwich Mean Time", zone.getName(winter.getMillis())); + assertEquals("GMT", zone.getNameKey(winter.getMillis())); + + DateTime summer = winter.plusMonths(6); + assertEquals(0, zone.getStandardOffset(summer.getMillis())); + assertEquals(3600000, zone.getOffset(summer.getMillis())); + assertEquals(false, zone.isStandardOffset(summer.getMillis())); + assertEquals(true, zone.getName(summer.getMillis()).startsWith("Irish ")); + assertEquals("IST", zone.getNameKey(summer.getMillis())); + } + + //----------------------------------------------------------------------- + @Test + public void testWindhoek() { + DateTimeZone zone = DateTimeZone.forID("Africa/Windhoek"); + DateTime dtDec1990 = new DateTime(1990, 12, 1, 0, 0, 0, 0, zone); + assertEquals(3600000, zone.getStandardOffset(dtDec1990.getMillis())); + assertEquals(7200000, zone.getOffset(dtDec1990.getMillis())); + assertEquals(false, zone.isStandardOffset(dtDec1990.getMillis())); + + DateTime dtDec1994 = new DateTime(1994, 12, 1, 0, 0, 0, 0, zone); + assertEquals(3600000, zone.getStandardOffset(dtDec1994.getMillis())); + assertEquals(7200000, zone.getOffset(dtDec1994.getMillis())); + assertEquals(false, zone.isStandardOffset(dtDec1994.getMillis())); + + DateTime dtJun1995 = new DateTime(1995, 6, 1, 0, 0, 0, 0, zone); + assertEquals(3600000, zone.getStandardOffset(dtJun1995.getMillis())); + assertEquals(3600000, zone.getOffset(dtJun1995.getMillis())); + assertEquals(true, zone.isStandardOffset(dtJun1995.getMillis())); + } + //----------------------------------------------------------------------- @Test public void testSerialization1() throws Exception {