Search in sources :

Example 71 with TimeZone

use of android.icu.util.TimeZone in project j2objc by google.

the class TimeZoneNamesImpl method initialize.

/**
 * Initialize the transient fields, called from the constructor and
 * readObject.
 *
 * @param locale The locale
 */
private void initialize(ULocale locale) {
    ICUResourceBundle bundle = (ICUResourceBundle) ICUResourceBundle.getBundleInstance(ICUData.ICU_ZONE_BASE_NAME, locale);
    _zoneStrings = (ICUResourceBundle) bundle.get(ZONE_STRINGS_BUNDLE);
    // TODO: Access is synchronized, can we use a non-concurrent map?
    _tzNamesMap = new ConcurrentHashMap<String, ZNames>();
    _mzNamesMap = new ConcurrentHashMap<String, ZNames>();
    _namesFullyLoaded = false;
    _namesTrie = new TextTrieMap<NameInfo>(true);
    _namesTrieFullyLoaded = false;
    // Preload zone strings for the default time zone
    TimeZone tz = TimeZone.getDefault();
    String tzCanonicalID = ZoneMeta.getCanonicalCLDRID(tz);
    if (tzCanonicalID != null) {
        loadStrings(tzCanonicalID);
    }
}
Also used : TimeZone(android.icu.util.TimeZone)

Example 72 with TimeZone

use of android.icu.util.TimeZone in project j2objc by google.

the class CalendarRegressionTest method Test4083167.

@Test
public void Test4083167() {
    TimeZone saveZone = TimeZone.getDefault();
    try {
        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
        Date firstDate = new Date();
        Calendar cal = new GregorianCalendar();
        cal.setTime(firstDate);
        long firstMillisInDay = cal.get(Calendar.HOUR_OF_DAY) * 3600000L + cal.get(Calendar.MINUTE) * 60000L + cal.get(Calendar.SECOND) * 1000L + cal.get(Calendar.MILLISECOND);
        logln("Current time: " + firstDate.toString());
        for (int validity = 0; validity < 30; validity++) {
            Date lastDate = new Date(firstDate.getTime() + (long) validity * 1000 * 24 * 60 * 60);
            cal.setTime(lastDate);
            long millisInDay = cal.get(Calendar.HOUR_OF_DAY) * 3600000L + cal.get(Calendar.MINUTE) * 60000L + cal.get(Calendar.SECOND) * 1000L + cal.get(Calendar.MILLISECOND);
            if (firstMillisInDay != millisInDay)
                errln("Day has shifted " + lastDate);
        }
    } finally {
        TimeZone.setDefault(saveZone);
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) GregorianCalendar(android.icu.util.GregorianCalendar) Date(java.util.Date) Test(org.junit.Test)

Example 73 with TimeZone

use of android.icu.util.TimeZone in project j2objc by google.

the class CalendarRegressionTest method Test4086724.

@Test
public void Test4086724() {
    SimpleDateFormat date;
    TimeZone saveZone = TimeZone.getDefault();
    Locale saveLocale = Locale.getDefault();
    try {
        Locale.setDefault(Locale.UK);
        TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
        date = new SimpleDateFormat("dd MMM yyy (zzzz) 'is in week' ww");
        Calendar cal = Calendar.getInstance();
        cal.set(1997, Calendar.SEPTEMBER, 30);
        Date now = cal.getTime();
        logln(date.format(now));
        cal.set(1997, Calendar.JANUARY, 1);
        now = cal.getTime();
        logln(date.format(now));
        cal.set(1997, Calendar.JANUARY, 8);
        now = cal.getTime();
        logln(date.format(now));
        cal.set(1996, Calendar.DECEMBER, 31);
        now = cal.getTime();
        logln(date.format(now));
    } finally {
        Locale.setDefault(saveLocale);
        TimeZone.setDefault(saveZone);
    }
    logln("*** THE RESULTS OF THIS TEST MUST BE VERIFIED MANUALLY ***");
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 74 with TimeZone

use of android.icu.util.TimeZone in project j2objc by google.

the class CalendarRegressionTest method Test4177484.

/**
 * Calendar does not update field values when setTimeZone is called.
 */
@Test
public void Test4177484() {
    TimeZone PST = TimeZone.getTimeZone("PST");
    TimeZone EST = TimeZone.getTimeZone("EST");
    Calendar cal = Calendar.getInstance(PST, Locale.US);
    cal.clear();
    // Arbitrary
    cal.set(1999, 3, 21, 15, 5, 0);
    int h1 = cal.get(Calendar.HOUR_OF_DAY);
    cal.setTimeZone(EST);
    int h2 = cal.get(Calendar.HOUR_OF_DAY);
    if (h1 == h2) {
        errln("FAIL: Fields not updated after setTimeZone");
    }
    // getTime() must NOT change when time zone is changed.
    // getTime() returns zone-independent time in ms.
    cal.clear();
    cal.setTimeZone(PST);
    cal.set(Calendar.HOUR_OF_DAY, 10);
    Date pst10 = cal.getTime();
    cal.setTimeZone(EST);
    Date est10 = cal.getTime();
    if (!pst10.equals(est10)) {
        errln("FAIL: setTimeZone changed time");
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) Date(java.util.Date) Test(org.junit.Test)

Example 75 with TimeZone

use of android.icu.util.TimeZone in project j2objc by google.

the class CalendarRegressionTest method Test4136399.

/**
 * Calendar and GregorianCalendar hashCode() methods need improvement.
 * Calendar needs a good implementation that subclasses can override, and
 * GregorianCalendar should use that implementation.
 */
@Test
public void Test4136399() {
    /*
         * Note: This test is actually more strict than it has to be.
         * Technically, there is no requirement that unequal objects have
         * unequal hashes. We only require equal objects to have equal hashes.
         * It is desirable for unequal objects to have distributed hashes, but
         * there is no hard requirement here.
         * 
         * In this test we make assumptions about certain attributes of calendar
         * objects getting represented in the hash, which need not always be the
         * case (although it does work currently with the given test).
         */
    Calendar a = Calendar.getInstance();
    Calendar b = (Calendar) a.clone();
    if (a.hashCode() != b.hashCode()) {
        errln("Calendar hash code unequal for cloned objects");
    }
    TimeZone atz1 = a.getTimeZone();
    TimeZone atz2 = (TimeZone) atz1.clone();
    if (!atz1.equals(atz2)) {
        errln("The clone timezones are not equal");
    }
    if (atz1.hashCode() != atz2.hashCode()) {
        errln("TimeZone hash code unequal for cloned objects");
    }
    b.setMinimalDaysInFirstWeek(7 - a.getMinimalDaysInFirstWeek());
    if (a.hashCode() == b.hashCode()) {
        errln("Calendar hash code ignores minimal days in first week");
    }
    b.setMinimalDaysInFirstWeek(a.getMinimalDaysInFirstWeek());
    // Next day
    b.setFirstDayOfWeek((a.getFirstDayOfWeek() % 7) + 1);
    if (a.hashCode() == b.hashCode()) {
        errln("Calendar hash code ignores first day of week");
    }
    b.setFirstDayOfWeek(a.getFirstDayOfWeek());
    b.setLenient(!a.isLenient());
    if (a.hashCode() == b.hashCode()) {
        errln("Calendar hash code ignores lenient setting");
    }
    b.setLenient(a.isLenient());
    // Assume getTimeZone() returns a reference, not a clone
    // of a reference -- this is true as of this writing
    TimeZone atz = a.getTimeZone();
    TimeZone btz = b.getTimeZone();
    btz.setRawOffset(atz.getRawOffset() + 60 * 60 * 1000);
    if (atz.hashCode() == btz.hashCode()) {
        errln(atz.hashCode() + "==" + btz.hashCode());
    }
    if (a.getTimeZone() != b.getTimeZone() && a.hashCode() == b.hashCode()) {
        errln("Calendar hash code ignores zone");
    }
    b.getTimeZone().setRawOffset(a.getTimeZone().getRawOffset());
    GregorianCalendar c = new GregorianCalendar();
    GregorianCalendar d = (GregorianCalendar) c.clone();
    if (c.hashCode() != d.hashCode()) {
        errln("GregorianCalendar hash code unequal for clones objects");
    }
    Date cutover = c.getGregorianChange();
    d.setGregorianChange(new Date(cutover.getTime() + 24 * 60 * 60 * 1000));
    if (c.hashCode() == d.hashCode()) {
        errln("GregorianCalendar hash code ignores cutover");
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) GregorianCalendar(android.icu.util.GregorianCalendar) Date(java.util.Date) Test(org.junit.Test)

Aggregations

TimeZone (android.icu.util.TimeZone)125 Test (org.junit.Test)98 SimpleTimeZone (android.icu.util.SimpleTimeZone)76 Date (java.util.Date)63 GregorianCalendar (android.icu.util.GregorianCalendar)49 Calendar (android.icu.util.Calendar)42 BasicTimeZone (android.icu.util.BasicTimeZone)41 SimpleDateFormat (android.icu.text.SimpleDateFormat)36 ULocale (android.icu.util.ULocale)31 RuleBasedTimeZone (android.icu.util.RuleBasedTimeZone)24 VTimeZone (android.icu.util.VTimeZone)23 DateFormat (android.icu.text.DateFormat)19 JavaTimeZone (android.icu.impl.JavaTimeZone)18 NativeTimeZone (com.google.j2objc.util.NativeTimeZone)18 JapaneseCalendar (android.icu.util.JapaneseCalendar)15 FieldPosition (java.text.FieldPosition)15 BuddhistCalendar (android.icu.util.BuddhistCalendar)13 ParsePosition (java.text.ParsePosition)13 ChineseCalendar (android.icu.util.ChineseCalendar)12 ParseException (java.text.ParseException)12