Search in sources :

Example 11 with TimeZone

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

the class AstroTest method TestSunriseTimes.

@Test
public void TestSunriseTimes() {
    // logln("Sunrise/Sunset times for San Jose, California, USA");
    // CalendarAstronomer astro = new CalendarAstronomer(-121.55, 37.20);
    // TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
    // We'll use a table generated by the UNSO website as our reference
    // From: http://aa.usno.navy.mil/
    // -Location: W079 25, N43 40
    // -Rise and Set for the Sun for 2001
    // -Zone:  4h West of Greenwich
    int[] USNO = { 6, 59, 19, 45, 6, 57, 19, 46, 6, 56, 19, 47, 6, 54, 19, 48, 6, 52, 19, 49, 6, 50, 19, 51, 6, 48, 19, 52, 6, 47, 19, 53, 6, 45, 19, 54, 6, 43, 19, 55, 6, 42, 19, 57, 6, 40, 19, 58, 6, 38, 19, 59, 6, 36, 20, 0, 6, 35, 20, 1, 6, 33, 20, 3, 6, 31, 20, 4, 6, 30, 20, 5, 6, 28, 20, 6, 6, 27, 20, 7, 6, 25, 20, 8, 6, 23, 20, 10, 6, 22, 20, 11, 6, 20, 20, 12, 6, 19, 20, 13, 6, 17, 20, 14, 6, 16, 20, 16, 6, 14, 20, 17, 6, 13, 20, 18, 6, 11, 20, 19 };
    logln("Sunrise/Sunset times for Toronto, Canada");
    CalendarAstronomer astro = new CalendarAstronomer(-(79 + 25 / 60), 43 + 40 / 60);
    // As of ICU4J 2.8 the ICU4J time zones implement pass-through
    // to the underlying JDK.  Because of variation in the
    // underlying JDKs, we have to use a fixed-offset
    // SimpleTimeZone to get consistent behavior between JDKs.
    // The offset we want is [-18000000, 3600000] (raw, dst).
    // [aliu 10/15/03]
    // TimeZone tz = TimeZone.getTimeZone("America/Montreal");
    TimeZone tz = new SimpleTimeZone(-18000000 + 3600000, "Montreal(FIXED)");
    GregorianCalendar cal = new GregorianCalendar(tz, Locale.US);
    GregorianCalendar cal2 = new GregorianCalendar(tz, Locale.US);
    cal.clear();
    cal.set(Calendar.YEAR, 2001);
    cal.set(Calendar.MONTH, Calendar.APRIL);
    cal.set(Calendar.DAY_OF_MONTH, 1);
    // must be near local noon for getSunRiseSet to work
    cal.set(Calendar.HOUR_OF_DAY, 12);
    DateFormat df = DateFormat.getTimeInstance(cal, DateFormat.MEDIUM, Locale.US);
    DateFormat df2 = DateFormat.getDateTimeInstance(cal, DateFormat.MEDIUM, DateFormat.MEDIUM, Locale.US);
    DateFormat day = DateFormat.getDateInstance(cal, DateFormat.MEDIUM, Locale.US);
    for (int i = 0; i < 30; i++) {
        astro.setDate(cal.getTime());
        Date sunrise = new Date(astro.getSunRiseSet(true));
        Date sunset = new Date(astro.getSunRiseSet(false));
        cal2.setTime(cal.getTime());
        cal2.set(Calendar.SECOND, 0);
        cal2.set(Calendar.MILLISECOND, 0);
        cal2.set(Calendar.HOUR_OF_DAY, USNO[4 * i + 0]);
        cal2.set(Calendar.MINUTE, USNO[4 * i + 1]);
        Date exprise = cal2.getTime();
        cal2.set(Calendar.HOUR_OF_DAY, USNO[4 * i + 2]);
        cal2.set(Calendar.MINUTE, USNO[4 * i + 3]);
        Date expset = cal2.getTime();
        // Compute delta of what we got to the USNO data, in seconds
        int deltarise = Math.abs((int) (sunrise.getTime() - exprise.getTime()) / 1000);
        int deltaset = Math.abs((int) (sunset.getTime() - expset.getTime()) / 1000);
        // Allow a deviation of 0..MAX_DEV seconds
        // It would be nice to get down to 60 seconds, but at this
        // point that appears to be impossible without a redo of the
        // algorithm using something more advanced than Duffett-Smith.
        final int MAX_DEV = 180;
        if (deltarise > MAX_DEV || deltaset > MAX_DEV) {
            if (deltarise > MAX_DEV) {
                errln("FAIL: " + day.format(cal.getTime()) + ", Sunrise: " + df2.format(sunrise) + " (USNO " + df.format(exprise) + " d=" + deltarise + "s)");
            } else {
                logln(day.format(cal.getTime()) + ", Sunrise: " + df.format(sunrise) + " (USNO " + df.format(exprise) + ")");
            }
            if (deltaset > MAX_DEV) {
                errln("FAIL: " + day.format(cal.getTime()) + ", Sunset: " + df2.format(sunset) + " (USNO " + df.format(expset) + " d=" + deltaset + "s)");
            } else {
                logln(day.format(cal.getTime()) + ", Sunset: " + df.format(sunset) + " (USNO " + df.format(expset) + ")");
            }
        } else {
            logln(day.format(cal.getTime()) + ", Sunrise: " + df.format(sunrise) + " (USNO " + df.format(exprise) + ")" + ", Sunset: " + df.format(sunset) + " (USNO " + df.format(expset) + ")");
        }
        cal.add(Calendar.DATE, 1);
    }
// CalendarAstronomer a = new CalendarAstronomer(-(71+5/60), 42+37/60);
// cal.clear();
// cal.set(cal.YEAR, 1986);
// cal.set(cal.MONTH, cal.MARCH);
// cal.set(cal.DATE, 10);
// cal.set(cal.YEAR, 1988);
// cal.set(cal.MONTH, cal.JULY);
// cal.set(cal.DATE, 27);
// a.setDate(cal.getTime());
// long r = a.getSunRiseSet2(true);
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) SimpleTimeZone(android.icu.util.SimpleTimeZone) DateFormat(android.icu.text.DateFormat) GregorianCalendar(android.icu.util.GregorianCalendar) CalendarAstronomer(android.icu.impl.CalendarAstronomer) Date(java.util.Date) Test(org.junit.Test)

Example 12 with TimeZone

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

the class CompatibilityTest method TestEpochStartFields.

// Verify that the fields are as expected (mostly zero) at the epoch start.
// Note that we adjust for the default timezone to get most things to zero.
@Test
public void TestEpochStartFields() {
    TimeZone z = TimeZone.getDefault();
    Calendar c = Calendar.getInstance();
    Date d = new Date(-z.getRawOffset());
    if (z.inDaylightTime(d)) {
        logln("Warning: Skipping test because " + d + " is in DST.");
    } else {
        c.setTime(d);
        c.setMinimalDaysInFirstWeek(1);
        for (int i = 0; i < Calendar.ZONE_OFFSET; ++i) {
            if (c.get(i) != EPOCH_FIELDS[i])
                errln("Expected field " + i + " to have value " + EPOCH_FIELDS[i] + "; saw " + c.get(i) + " instead");
        }
        if (c.get(Calendar.ZONE_OFFSET) != z.getRawOffset())
            errln("Expected field ZONE_OFFSET to have value " + z.getRawOffset() + "; saw " + c.get(Calendar.ZONE_OFFSET) + " instead");
        if (c.get(Calendar.DST_OFFSET) != 0)
            errln("Expected field DST_OFFSET to have value 0" + "; saw " + c.get(Calendar.DST_OFFSET) + " instead");
    }
}
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 13 with TimeZone

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

the class DateFormatRegressionTest method Test4106807.

/**
 * @bug 4106807
 */
@Test
public void Test4106807() {
    Date dt;
    DateFormat df = DateFormat.getDateTimeInstance();
    SimpleDateFormat[] sdfs = { new SimpleDateFormat("yyyyMMddHHmmss"), new SimpleDateFormat("yyyyMMddHHmmss'Z'"), new SimpleDateFormat("yyyyMMddHHmmss''"), new SimpleDateFormat("yyyyMMddHHmmss'a''a'"), new SimpleDateFormat("yyyyMMddHHmmss %") };
    String[] strings = { "19980211140000", "19980211140000", "19980211140000", "19980211140000a", "19980211140000 " };
    GregorianCalendar gc = new GregorianCalendar();
    TimeZone timeZone = TimeZone.getDefault();
    TimeZone gmt = (TimeZone) timeZone.clone();
    gmt.setRawOffset(0);
    for (int i = 0; i < 5; i++) {
        SimpleDateFormat format = sdfs[i];
        String dateString = strings[i];
        try {
            format.setTimeZone(gmt);
            dt = format.parse(dateString);
            // {sfb} some of these parses will fail purposely
            StringBuffer fmtd = new StringBuffer("");
            FieldPosition pos = new FieldPosition(0);
            fmtd = df.format(dt, fmtd, pos);
            logln(fmtd.toString());
            // logln(df.format(dt));
            gc.setTime(dt);
            logln("" + gc.get(Calendar.ZONE_OFFSET));
            StringBuffer s = new StringBuffer("");
            s = format.format(dt, s, pos);
            logln(s.toString());
        } catch (ParseException e) {
            logln("No way Jose");
        }
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) GregorianCalendar(android.icu.util.GregorianCalendar) ParseException(java.text.ParseException) FieldPosition(java.text.FieldPosition) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 14 with TimeZone

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

the class DataDrivenFormatTest method testConvertDate.

private void testConvertDate(TestDataModule.TestData testData, DataMap settings, boolean fmt) {
    DateFormat basicFmt = new SimpleDateFormat("EEE MMM dd yyyy / YYYY'-W'ww-ee");
    int n = 0;
    for (Iterator iter = testData.getDataIterator(); iter.hasNext(); ) {
        ++n;
        long now = System.currentTimeMillis();
        DataMap currentCase = (DataMap) iter.next();
        String caseString = "[" + testData.getName() + "#" + n + (fmt ? "format" : "parse") + "]";
        String locale = currentCase.getString("locale");
        String zone = currentCase.getString("zone");
        String spec = currentCase.getString("spec");
        String date = currentCase.getString("date");
        String str = currentCase.getString("str");
        Date fromDate = null;
        boolean useDate = false;
        ULocale loc = new ULocale(locale);
        String pattern = null;
        // boolean usePattern = false;
        DateFormat format = null;
        DateTimeStyleSet styleSet;
        CalendarFieldsSet fromSet = null;
        // parse 'spec'  - either 'PATTERN=yy mm dd' or 'DATE=x,TIME=y'
        if (spec.startsWith(kPATTERN)) {
            pattern = spec.substring(kPATTERN.length());
            // usePattern = true;
            format = new SimpleDateFormat(pattern, loc);
        } else {
            styleSet = new DateTimeStyleSet();
            styleSet.parseFrom(spec);
            format = DateFormat.getDateTimeInstance(styleSet.getDateStyle(), styleSet.getTimeStyle(), loc);
        }
        Calendar cal = Calendar.getInstance(loc);
        if (zone.length() > 0) {
            TimeZone tz = TimeZone.getFrozenTimeZone(zone);
            cal.setTimeZone(tz);
            format.setTimeZone(tz);
        }
        // parse 'date' - either 'MILLIS=12345' or  a CalendarFieldsSet
        if (date.startsWith(kMILLIS)) {
            useDate = true;
            fromDate = new Date(Long.parseLong(date.substring(kMILLIS.length())));
        } else if (date.startsWith(kRELATIVE_MILLIS)) {
            useDate = true;
            fromDate = new Date(now + Long.parseLong(date.substring(kRELATIVE_MILLIS.length())));
        } else if (date.startsWith(kRELATIVE_ADD)) {
            // "add" is a string indicating which fields to add
            String add = date.substring(kRELATIVE_ADD.length());
            CalendarFieldsSet addSet = new CalendarFieldsSet();
            addSet.parseFrom(add);
            useDate = true;
            cal.clear();
            cal.setTimeInMillis(now);
            // / perform op on 'to calendar'
            for (int q = 0; q < addSet.fieldCount(); q++) {
                if (addSet.isSet(q)) {
                    if (q == Calendar.DATE) {
                        cal.add(q, addSet.get(q));
                    } else {
                        cal.set(q, addSet.get(q));
                    }
                }
            }
            fromDate = cal.getTime();
        } else {
            fromSet = new CalendarFieldsSet();
            fromSet.parseFrom(date);
        }
        // run the test
        if (fmt) {
            StringBuffer output = new StringBuffer();
            cal.clear();
            FieldPosition pos = new FieldPosition(0);
            if (useDate) {
                output = format.format(fromDate, output, pos);
            } else {
                fromSet.setOnCalendar(cal);
                format.format(cal, output, pos);
            }
            if (output.toString().equals(str)) {
                logln(caseString + " Success - strings match: " + output);
            } else {
                errln(caseString + " FAIL: got " + output + " expected " + str);
            }
        } else {
            // parse
            cal.clear();
            ParsePosition pos = new ParsePosition(0);
            format.parse(str, cal, pos);
            if (useDate) {
                Date gotDate = cal.getTime();
                if (gotDate.equals(fromDate)) {
                    logln(caseString + " SUCCESS: got=parse=" + str);
                } else {
                    errln(caseString + " FAIL: parsed " + str + " but got " + basicFmt.format(gotDate) + " - " + gotDate + "  expected " + basicFmt.format(fromDate));
                }
            } else {
                CalendarFieldsSet diffSet = new CalendarFieldsSet();
                if (!fromSet.matches(cal, diffSet)) {
                    String diffs = diffSet.diffFrom(fromSet);
                    errln(caseString + " FAIL:  differences: " + diffs);
                } else {
                    logln(caseString + " SUCCESS: got=parse: " + str + " - " + fromSet.toString());
                }
            }
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) Calendar(android.icu.util.Calendar) CalendarFieldsSet(android.icu.dev.test.util.CalendarFieldsSet) FieldPosition(java.text.FieldPosition) Date(java.util.Date) DataMap(android.icu.dev.test.TestDataModule.DataMap) TimeZone(android.icu.util.TimeZone) DateTimeStyleSet(android.icu.dev.test.util.DateTimeStyleSet) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) Iterator(java.util.Iterator) SimpleDateFormat(android.icu.text.SimpleDateFormat) ParsePosition(java.text.ParsePosition)

Example 15 with TimeZone

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

the class DateFormatRegressionTest method Test4089106.

/**
 * @bug 4089106
 */
@Test
public void Test4089106() {
    TimeZone def = TimeZone.getDefault();
    try {
        TimeZone z = new SimpleTimeZone((int) (1.25 * 3600000), "FAKEZONE");
        TimeZone.setDefault(z);
        // Android patch (http://b/28949992) start.
        // ICU TimeZone.setDefault() not supported on Android.
        z = TimeZone.getDefault();
        // Android patch (http://b/28949992) end.
        SimpleDateFormat f = new SimpleDateFormat();
        if (!f.getTimeZone().equals(z))
            errln("Fail: SimpleTimeZone should use TimeZone.getDefault()");
    } finally {
        TimeZone.setDefault(def);
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) SimpleTimeZone(android.icu.util.SimpleTimeZone) SimpleDateFormat(android.icu.text.SimpleDateFormat) 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