Search in sources :

Example 6 with CalendarAstronomer

use of android.icu.impl.CalendarAstronomer in project j2objc by google.

the class AstroTest method TestCoordinates.

@Test
public void TestCoordinates() {
    CalendarAstronomer astro = new CalendarAstronomer();
    Equatorial result = astro.eclipticToEquatorial(139.686111 * PI / 180.0, 4.875278 * PI / 180.0);
    logln("result is " + result + ";  " + result.toHmsString());
}
Also used : CalendarAstronomer(android.icu.impl.CalendarAstronomer) Equatorial(android.icu.impl.CalendarAstronomer.Equatorial) Test(org.junit.Test)

Example 7 with CalendarAstronomer

use of android.icu.impl.CalendarAstronomer 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 8 with CalendarAstronomer

use of android.icu.impl.CalendarAstronomer in project android_frameworks_base by crdroidandroid.

the class TwilightService method calculateTwilightState.

/**
     * Calculates the twilight state for a specific location and time.
     *
     * @param location the location to use
     * @param timeMillis the reference time to use
     * @return the calculated {@link TwilightState}, or {@code null} if location is {@code null}
     */
private static TwilightState calculateTwilightState(Location location, long timeMillis) {
    if (location == null) {
        return null;
    }
    final CalendarAstronomer ca = new CalendarAstronomer(location.getLongitude(), location.getLatitude());
    final Calendar noon = Calendar.getInstance();
    noon.setTimeInMillis(timeMillis);
    noon.set(Calendar.HOUR_OF_DAY, 12);
    noon.set(Calendar.MINUTE, 0);
    noon.set(Calendar.SECOND, 0);
    noon.set(Calendar.MILLISECOND, 0);
    ca.setTime(noon.getTimeInMillis());
    long sunriseTimeMillis = ca.getSunRiseSet(true);
    long sunsetTimeMillis = ca.getSunRiseSet(false);
    if (sunsetTimeMillis < timeMillis) {
        noon.add(Calendar.DATE, 1);
        ca.setTime(noon.getTimeInMillis());
        sunriseTimeMillis = ca.getSunRiseSet(true);
    } else if (sunriseTimeMillis > timeMillis) {
        noon.add(Calendar.DATE, -1);
        ca.setTime(noon.getTimeInMillis());
        sunsetTimeMillis = ca.getSunRiseSet(false);
    }
    return new TwilightState(sunriseTimeMillis, sunsetTimeMillis);
}
Also used : Calendar(android.icu.util.Calendar) CalendarAstronomer(android.icu.impl.CalendarAstronomer)

Example 9 with CalendarAstronomer

use of android.icu.impl.CalendarAstronomer in project j2objc by google.

the class ChineseCalendar method readObject.

/**
 * Override readObject.
 */
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
    epochYear = CHINESE_EPOCH_YEAR;
    zoneAstro = CHINA_ZONE;
    stream.defaultReadObject();
    /* set up the transient caches... */
    astro = new CalendarAstronomer();
    winterSolsticeCache = new CalendarCache();
    newYearCache = new CalendarCache();
}
Also used : CalendarCache(android.icu.impl.CalendarCache) CalendarAstronomer(android.icu.impl.CalendarAstronomer)

Example 10 with CalendarAstronomer

use of android.icu.impl.CalendarAstronomer in project j2objc by google.

the class AstroTest method TestBasics.

@Test
public void TestBasics() {
    // Check that our JD computation is the same as the book's (p. 88)
    CalendarAstronomer astro = new CalendarAstronomer();
    GregorianCalendar cal3 = new GregorianCalendar(TimeZone.getTimeZone("GMT"), Locale.US);
    DateFormat d3 = DateFormat.getDateTimeInstance(cal3, DateFormat.MEDIUM, DateFormat.MEDIUM, Locale.US);
    cal3.clear();
    cal3.set(Calendar.YEAR, 1980);
    cal3.set(Calendar.MONTH, Calendar.JULY);
    cal3.set(Calendar.DATE, 27);
    astro.setDate(cal3.getTime());
    double jd = astro.getJulianDay() - 2447891.5;
    double exp = -3444;
    if (jd == exp) {
        logln(d3.format(cal3.getTime()) + " => " + jd);
    } else {
        errln("FAIL: " + d3.format(cal3.getTime()) + " => " + jd + ", expected " + exp);
    }
// cal3.clear();
// cal3.set(cal3.YEAR, 1990);
// cal3.set(cal3.MONTH, Calendar.JANUARY);
// cal3.set(cal3.DATE, 1);
// cal3.add(cal3.DATE, -1);
// astro.setDate(cal3.getTime());
// astro.foo();
}
Also used : DateFormat(android.icu.text.DateFormat) GregorianCalendar(android.icu.util.GregorianCalendar) CalendarAstronomer(android.icu.impl.CalendarAstronomer) Test(org.junit.Test)

Aggregations

CalendarAstronomer (android.icu.impl.CalendarAstronomer)13 Test (org.junit.Test)8 GregorianCalendar (android.icu.util.GregorianCalendar)7 Calendar (android.icu.util.Calendar)5 SimpleTimeZone (android.icu.util.SimpleTimeZone)4 Equatorial (android.icu.impl.CalendarAstronomer.Equatorial)3 DateFormat (android.icu.text.DateFormat)3 Date (java.util.Date)3 Ecliptic (android.icu.impl.CalendarAstronomer.Ecliptic)1 CalendarCache (android.icu.impl.CalendarCache)1 SimpleDateFormat (android.icu.text.SimpleDateFormat)1 BuddhistCalendar (android.icu.util.BuddhistCalendar)1 ChineseCalendar (android.icu.util.ChineseCalendar)1 JapaneseCalendar (android.icu.util.JapaneseCalendar)1 TaiwanCalendar (android.icu.util.TaiwanCalendar)1 TimeZone (android.icu.util.TimeZone)1 ULocale (android.icu.util.ULocale)1