Search in sources :

Example 46 with TimeZone

use of java.util.TimeZone in project j2objc by google.

the class NativeTimeZoneTest method testResetDefaultTimeZone.

public void testResetDefaultTimeZone() {
    TimeZone defaultTz = TimeZone.getDefault();
    if (defaultTz.getID().equals("Etc/UTC")) {
        // We assume that the test is never run with the environment TZ=Etc/UTC, but just to be safe.
        return;
    }
    TimeZone.setDefault(TimeZone.getTimeZone("Etc/UTC"));
    assertEquals("Etc/UTC", TimeZone.getDefault().getID());
    // Manually post NSSystemTimeZoneDidChangeNotification, which is dispatched immediately to all
    // observers.
    postNSSystemTimeZoneDidChangeNotification();
    // The default time zone should be updated.
    assertNotNull(TimeZone.getDefault());
    assertFalse(TimeZone.getDefault().getID().equals("Etc/UTC"));
}
Also used : TimeZone(java.util.TimeZone)

Example 47 with TimeZone

use of java.util.TimeZone in project j2objc by google.

the class NativeTimeZoneTest method testUSPacificTimeZoneTransitionsSince1970.

/**
   * Test that the TimeZone instance returns accurate offsets for instants in America/Los_Angeles
   * (commonly known as PT, which includes PST and PDT). This also accounts for the daylight
   * saving time (DST) change due to Energy Policy Act of 2005.
   */
public void testUSPacificTimeZoneTransitionsSince1970() {
    TimeZone la = TimeZone.getTimeZone("America/Los_Angeles");
    // 2016-03-13T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(1457863200000L));
    // 2015-11-01T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(1446368400000L));
    // 2015-03-08T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(1425808800000L));
    // 2014-11-02T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(1414918800000L));
    // 2014-03-09T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(1394359200000L));
    // 2013-11-03T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(1383469200000L));
    // 2013-03-10T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(1362909600000L));
    // 2012-11-04T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(1352019600000L));
    // 2012-03-11T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(1331460000000L));
    // 2011-11-06T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(1320570000000L));
    // 2011-03-13T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(1300010400000L));
    // 2010-11-07T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(1289120400000L));
    // 2010-03-14T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(1268560800000L));
    // 2009-11-01T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(1257066000000L));
    // 2009-03-08T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(1236506400000L));
    // 2008-11-02T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(1225616400000L));
    // 2008-03-09T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(1205056800000L));
    // 2007-11-04T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(1194166800000L));
    // 2007-03-11T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(1173607200000L));
    // 2006-10-29T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(1162112400000L));
    // 2006-04-02T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(1143972000000L));
    // 2005-10-30T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(1130662800000L));
    // 2005-04-03T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(1112522400000L));
    // 2004-10-31T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(1099213200000L));
    // 2004-04-04T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(1081072800000L));
    // 2003-10-26T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(1067158800000L));
    // 2003-04-06T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(1049623200000L));
    // 2002-10-27T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(1035709200000L));
    // 2002-04-07T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(1018173600000L));
    // 2001-10-28T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(1004259600000L));
    // 2001-04-01T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(986119200000L));
    // 2000-10-29T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(972810000000L));
    // 2000-04-02T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(954669600000L));
    // 1999-10-31T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(941360400000L));
    // 1999-04-04T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(923220000000L));
    // 1998-10-25T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(909306000000L));
    // 1998-04-05T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(891770400000L));
    // 1997-10-26T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(877856400000L));
    // 1997-04-06T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(860320800000L));
    // 1996-10-27T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(846406800000L));
    // 1996-04-07T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(828871200000L));
    // 1995-10-29T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(814957200000L));
    // 1995-04-02T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(796816800000L));
    // 1994-10-30T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(783507600000L));
    // 1994-04-03T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(765367200000L));
    // 1993-10-31T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(752058000000L));
    // 1993-04-04T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(733917600000L));
    // 1992-10-25T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(720003600000L));
    // 1992-04-05T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(702468000000L));
    // 1991-10-27T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(688554000000L));
    // 1991-04-07T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(671018400000L));
    // 1990-10-28T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(657104400000L));
    // 1990-04-01T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(638964000000L));
    // 1989-10-29T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(625654800000L));
    // 1989-04-02T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(607514400000L));
    // 1988-10-30T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(594205200000L));
    // 1988-04-03T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(576064800000L));
    // 1987-10-25T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(562150800000L));
    // 1987-04-05T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(544615200000L));
    // 1986-10-26T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(530701200000L));
    // 1986-04-27T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(514980000000L));
    // 1985-10-27T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(499251600000L));
    // 1985-04-28T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(483530400000L));
    // 1984-10-28T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(467802000000L));
    // 1984-04-29T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(452080800000L));
    // 1983-10-30T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(436352400000L));
    // 1983-04-24T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(420026400000L));
    // 1982-10-31T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(404902800000L));
    // 1982-04-25T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(388576800000L));
    // 1981-10-25T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(372848400000L));
    // 1981-04-26T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(357127200000L));
    // 1980-10-26T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(341398800000L));
    // 1980-04-27T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(325677600000L));
    // 1979-10-28T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(309949200000L));
    // 1979-04-29T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(294228000000L));
    // 1978-10-29T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(278499600000L));
    // 1978-04-30T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(262778400000L));
    // 1977-10-30T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(247050000000L));
    // 1977-04-24T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(230724000000L));
    // 1976-10-31T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(215600400000L));
    // 1976-04-25T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(199274400000L));
    // 1975-10-26T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(183546000000L));
    // 1975-02-23T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(162381600000L));
    // 1974-10-27T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(152096400000L));
    // 1974-01-06T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(126698400000L));
    // 1973-10-28T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(120646800000L));
    // 1973-04-29T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(104925600000L));
    // 1972-10-29T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(89197200000L));
    // 1972-04-30T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(73476000000L));
    // 1971-10-31T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(57747600000L));
    // 1971-04-25T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(41421600000L));
    // 1970-10-25T01:00:00.000-08:00
    assertEquals(-28800000, la.getOffset(25693200000L));
    // 1970-04-26T03:00:00.000-07:00
    assertEquals(-25200000, la.getOffset(9972000000L));
}
Also used : TimeZone(java.util.TimeZone)

Example 48 with TimeZone

use of java.util.TimeZone in project j2objc by google.

the class NativeTimeZoneTest method testGetDisplayName.

/**
   * Test the locale-dependent display name of a time zone.
   *
   * JVM and iOS/OS X don't agree on the localized names, nor do they have the same localization
   * coverage, and so we are just testing a few shared traits here.
   */
public void testGetDisplayName() {
    TimeZone tz = TimeZone.getTimeZone("Europe/Paris");
    // JVM says Central European (Summer) Time, OS X/iOS uses Central European Standard/Summer Time.
    // Short names also differ and are not tested here.
    assertTrue(tz.getDisplayName(true, TimeZone.LONG, Locale.ENGLISH).startsWith("Central European"));
    assertTrue(tz.getDisplayName(true, TimeZone.LONG, Locale.ENGLISH).contains("Summer"));
    assertTrue(tz.getDisplayName(false, TimeZone.LONG, Locale.ENGLISH).startsWith("Central European"));
    assertFalse(tz.getDisplayName(false, TimeZone.LONG, Locale.ENGLISH).startsWith("Summer"));
    // French time zone names change across environment and OS versions, and so we only test common
    // substrings here.
    // French for "summer"
    final String ete = "été";
    // French for "forward"
    final String avancee = "avancée";
    String frStdName = tz.getDisplayName(false, TimeZone.LONG, Locale.FRANCE);
    String frDstName = tz.getDisplayName(true, TimeZone.LONG, Locale.FRANCE);
    assertTrue(frStdName.contains("Europe central"));
    assertFalse(frStdName.contains(ete) || frStdName.contains(avancee));
    assertTrue(frDstName.contains("Europe central"));
    assertTrue(frDstName.contains(ete) || frDstName.contains(avancee));
    // Similarly for German, though they finally agree on the DST name.
    assertTrue(tz.getDisplayName(true, TimeZone.LONG, Locale.GERMAN).equals("Mitteleuropäische Sommerzeit"));
    assertTrue(tz.getDisplayName(false, TimeZone.LONG, Locale.GERMAN).startsWith("Mitteleuropäische"));
    assertFalse(tz.getDisplayName(false, TimeZone.LONG, Locale.GERMAN).contains("Sommer"));
}
Also used : TimeZone(java.util.TimeZone)

Example 49 with TimeZone

use of java.util.TimeZone in project j2objc by google.

the class Support_SimpleDateFormat method t_format_with_FieldPosition.

public void t_format_with_FieldPosition() {
    TimeZone tz = TimeZone.getTimeZone("EST");
    Calendar cal = new GregorianCalendar(tz);
    cal.set(1999, Calendar.SEPTEMBER, 13, 17, 19, 01);
    cal.set(Calendar.MILLISECOND, 0);
    Date date = cal.getTime();
    SimpleDateFormat format = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.US);
    format.setTimeZone(tz);
    // test with all pattern chars, and multiple occurences
    format.applyPattern("G GGGG y yy yyyy M MM MMM MMMM d dd ddd " + "k kk kkk H HH HHH h hh hhh m mmm s ss sss S SS SSS EE EEEE " + "D DD DDD F FF w www W WWW " + "a  aaa  K KKK z zzzz Z ZZZZ");
    StringBuffer textBuffer = new StringBuffer();
    // Really, GGGG should be "Anno Domini", but the RI doesn't support that and no one cares.
    textBuffer.append("AD AD 1999 99 1999 9 09 Sep September 13 13 013 ");
    textBuffer.append("17 17 017 17 17 017 5 05 005 19 019 1 01 001 0 00 000 Mon Monday ");
    textBuffer.append("256 256 256 2 02 38 038 3 003 ");
    textBuffer.append("PM  PM  5 005 GMT-5 GMT-05:00 -0500 GMT-05:00");
    // to avoid passing the huge StringBuffer each time.
    super.text = textBuffer.toString();
    // test if field positions are set correctly for these fields occurring
    // multiple times.
    t_FormatWithField(0, format, date, null, Field.ERA, 0, 2);
    t_FormatWithField(1, format, date, null, Field.YEAR, 6, 10);
    t_FormatWithField(2, format, date, null, Field.MONTH, 19, 20);
    t_FormatWithField(3, format, date, null, Field.DAY_OF_MONTH, 38, 40);
    t_FormatWithField(4, format, date, null, Field.HOUR_OF_DAY1, 48, 50);
    t_FormatWithField(5, format, date, null, Field.HOUR_OF_DAY0, 58, 60);
    t_FormatWithField(6, format, date, null, Field.HOUR1, 68, 69);
    t_FormatWithField(7, format, date, null, Field.MINUTE, 77, 79);
    t_FormatWithField(8, format, date, null, Field.SECOND, 84, 85);
    t_FormatWithField(9, format, date, null, Field.MILLISECOND, 93, 94);
    t_FormatWithField(10, format, date, null, Field.DAY_OF_WEEK, 102, 105);
    t_FormatWithField(11, format, date, null, Field.DAY_OF_YEAR, 113, 116);
    t_FormatWithField(12, format, date, null, Field.DAY_OF_WEEK_IN_MONTH, 125, 126);
    t_FormatWithField(13, format, date, null, Field.WEEK_OF_YEAR, 130, 132);
    t_FormatWithField(14, format, date, null, Field.WEEK_OF_MONTH, 137, 138);
    t_FormatWithField(15, format, date, null, Field.AM_PM, 143, 145);
    t_FormatWithField(16, format, date, null, Field.HOUR0, 151, 152);
    t_FormatWithField(17, format, date, null, Field.TIME_ZONE, 157, 162);
    // test fields that are not included in the formatted text
    t_FormatWithField(18, format, date, null, NumberFormat.Field.EXPONENT_SIGN, 0, 0);
    // test with simple example
    format.applyPattern("h:m z");
    super.text = "5:19 GMT-5";
    t_FormatWithField(21, format, date, null, Field.HOUR1, 0, 1);
    t_FormatWithField(22, format, date, null, Field.MINUTE, 2, 4);
    t_FormatWithField(23, format, date, null, Field.TIME_ZONE, 5, 10);
    // test fields that are not included in the formatted text
    t_FormatWithField(24, format, date, null, Field.ERA, 0, 0);
    t_FormatWithField(25, format, date, null, Field.YEAR, 0, 0);
    t_FormatWithField(26, format, date, null, Field.MONTH, 0, 0);
    t_FormatWithField(27, format, date, null, Field.DAY_OF_MONTH, 0, 0);
    t_FormatWithField(28, format, date, null, Field.HOUR_OF_DAY1, 0, 0);
    t_FormatWithField(29, format, date, null, Field.HOUR_OF_DAY0, 0, 0);
    t_FormatWithField(30, format, date, null, Field.SECOND, 0, 0);
    t_FormatWithField(31, format, date, null, Field.MILLISECOND, 0, 0);
    t_FormatWithField(32, format, date, null, Field.DAY_OF_WEEK, 0, 0);
    t_FormatWithField(33, format, date, null, Field.DAY_OF_YEAR, 0, 0);
    t_FormatWithField(34, format, date, null, Field.DAY_OF_WEEK_IN_MONTH, 0, 0);
    t_FormatWithField(35, format, date, null, Field.WEEK_OF_YEAR, 0, 0);
    t_FormatWithField(36, format, date, null, Field.WEEK_OF_MONTH, 0, 0);
    t_FormatWithField(37, format, date, null, Field.AM_PM, 0, 0);
    t_FormatWithField(38, format, date, null, Field.HOUR0, 0, 0);
    t_FormatWithField(39, format, date, null, NumberFormat.Field.EXPONENT, 0, 0);
    // test with simple example with pattern char Z
    format.applyPattern("h:m Z z");
    super.text = "5:19 -0500 GMT-5";
    t_FormatWithField(40, format, date, null, Field.HOUR1, 0, 1);
    t_FormatWithField(41, format, date, null, Field.MINUTE, 2, 4);
    t_FormatWithField(42, format, date, null, Field.TIME_ZONE, 5, 10);
}
Also used : TimeZone(java.util.TimeZone) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) GregorianCalendar(java.util.GregorianCalendar) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 50 with TimeZone

use of java.util.TimeZone in project j2objc by google.

the class DateTest method test_toString.

/**
     * java.util.Date#toString()
     */
public void test_toString() {
    // Test for method java.lang.String java.util.Date.toString()
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.DATE, 1);
    cal.set(Calendar.MONTH, Calendar.JANUARY);
    cal.set(Calendar.YEAR, 1970);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    Date d = cal.getTime();
    String result = d.toString();
    assertTrue("Incorrect result: " + d, result.startsWith("Thu Jan 01 00:00:00") && result.endsWith("1970"));
    TimeZone tz = TimeZone.getDefault();
    TimeZone.setDefault(TimeZone.getTimeZone("GMT-5"));
    try {
        Date d1 = new Date(0);
        assertTrue("Returned incorrect string: " + d1, d1.toString().equals("Wed Dec 31 19:00:00 GMT-05:00 1969"));
    } finally {
        TimeZone.setDefault(tz);
    }
    // Test for HARMONY-5468
    TimeZone.setDefault(TimeZone.getTimeZone("MST"));
    Date d2 = new Date(108, 7, 27);
    assertTrue("Returned incorrect string: " + d2, d2.toString().startsWith("Wed Aug 27 00:00:00") && d2.toString().endsWith("2008"));
}
Also used : TimeZone(java.util.TimeZone) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) Date(java.util.Date)

Aggregations

TimeZone (java.util.TimeZone)577 Date (java.util.Date)140 Calendar (java.util.Calendar)137 SimpleTimeZone (java.util.SimpleTimeZone)89 Test (org.junit.Test)85 SimpleDateFormat (java.text.SimpleDateFormat)69 Locale (java.util.Locale)69 GregorianCalendar (java.util.GregorianCalendar)56 ArrayList (java.util.ArrayList)32 DateFormat (java.text.DateFormat)24 ParseException (java.text.ParseException)23 IOException (java.io.IOException)20 Map (java.util.Map)15 HashSet (java.util.HashSet)13 PreparedStatement (java.sql.PreparedStatement)11 HashMap (java.util.HashMap)11 DateTimeZone (org.joda.time.DateTimeZone)11 Support_TimeZone (tests.support.Support_TimeZone)10 Resources (android.content.res.Resources)9 SQLException (java.sql.SQLException)9