public void TestIsOn() { // jb 1901 SimpleHoliday sh = new SimpleHoliday(IBM.ICU.Util.Calendar.AUGUST, 15, "Doug's Day", 1958, 2058); IBM.ICU.Util.Calendar cal_0 = new IBM.ICU.Util.GregorianCalendar(); cal_0.Clear(); cal_0.Set(IBM.ICU.Util.Calendar.YEAR, 2000); cal_0.Set(IBM.ICU.Util.Calendar.MONTH, IBM.ICU.Util.Calendar.AUGUST); cal_0.Set(IBM.ICU.Util.Calendar.DAY_OF_MONTH, 15); DateTime d0 = cal_0.GetTime(); cal_0.Add(IBM.ICU.Util.Calendar.SECOND, 1); DateTime d1 = cal_0.GetTime(); cal_0.Add(IBM.ICU.Util.Calendar.SECOND, -2); DateTime d2 = cal_0.GetTime(); cal_0.Add(IBM.ICU.Util.Calendar.DAY_OF_MONTH, 1); DateTime d3 = cal_0.GetTime(); cal_0.Add(IBM.ICU.Util.Calendar.SECOND, 1); DateTime d4 = cal_0.GetTime(); cal_0.Add(IBM.ICU.Util.Calendar.SECOND, -2); cal_0.Set(IBM.ICU.Util.Calendar.YEAR, 1957); DateTime d5 = cal_0.GetTime(); cal_0.Set(IBM.ICU.Util.Calendar.YEAR, 1958); DateTime d6 = cal_0.GetTime(); cal_0.Set(IBM.ICU.Util.Calendar.YEAR, 2058); DateTime d7 = cal_0.GetTime(); cal_0.Set(IBM.ICU.Util.Calendar.YEAR, 2059); DateTime d8 = cal_0.GetTime(); DateTime[] dates = { d0, d1, d2, d3, d4, d5, d6, d7, d8 }; bool[] isOns = { true, true, false, true, false, false, true, true, false }; for (int i = 0; i < dates.Length; ++i) { DateTime d = dates[i]; Logln("\ndate: " + d); bool isOn = sh.IsOn(d); Logln("isOnDate: " + isOn); if (isOn != isOns[i]) { Errln("date: " + d + " should be on Doug's Day!"); } DateTime h = sh.FirstAfter(d); Logln("firstAfter: " + h); } }
public void TestYear() { // Gregorian Calendar IBM.ICU.Util.Calendar gCal = new IBM.ICU.Util.GregorianCalendar(); DateTime gToday = gCal.GetTime(); gCal.Add(IBM.ICU.Util.Calendar.MONTH, 2); DateTime gFuture = gCal.GetTime(); DateFormat gDF = IBM.ICU.Text.DateFormat.GetDateInstance(gCal, IBM.ICU.Text.DateFormat.FULL); Logln("gregorian calendar: " + gDF.Format(gToday) + " + 2 months = " + gDF.Format(gFuture)); // Coptic Calendar CopticCalendar cCal = new CopticCalendar(); DateTime cToday = cCal.GetTime(); cCal.Add(IBM.ICU.Util.Calendar.MONTH, 2); DateTime cFuture = cCal.GetTime(); DateFormat cDF = IBM.ICU.Text.DateFormat.GetDateInstance(cCal, IBM.ICU.Text.DateFormat.FULL); Logln("coptic calendar: " + cDF.Format(cToday) + " + 2 months = " + cDF.Format(cFuture)); // EthiopicCalendar EthiopicCalendar eCal = new EthiopicCalendar(); DateTime eToday = eCal.GetTime(); eCal.Add(IBM.ICU.Util.Calendar.MONTH, 2); // add 2 months eCal.SetAmeteAlemEra(false); DateTime eFuture = eCal.GetTime(); DateFormat eDF = IBM.ICU.Text.DateFormat.GetDateInstance(eCal, IBM.ICU.Text.DateFormat.FULL); Logln("ethiopic calendar: " + eDF.Format(eToday) + " + 2 months = " + eDF.Format(eFuture)); }
public void TestZeroDOM() { ChineseCalendar cal = new ChineseCalendar(); IBM.ICU.Util.GregorianCalendar greg = new IBM.ICU.Util.GregorianCalendar(1989, IBM.ICU.Util.Calendar.SEPTEMBER, 1); Logln("Start: " + greg.GetTime()); for (int i = 0; i < 1000; ++i) { cal.SetTimeInMillis(greg.GetTimeInMillis()); if (cal.Get(IBM.ICU.Util.Calendar.DAY_OF_MONTH) == 0) { Errln("Fail: " + greg.GetTime() + " -> " + cal.Get(IBM.ICU.Util.Calendar.EXTENDED_YEAR) + "/" + cal.Get(IBM.ICU.Util.Calendar.MONTH) + ((cal.Get(IBM.ICU.Util.ChineseCalendar.IS_LEAP_MONTH) == 1) ? "(leap)" : "") + "/" + cal.Get(IBM.ICU.Util.Calendar.DAY_OF_MONTH)); } greg.Add(IBM.ICU.Util.Calendar.DAY_OF_YEAR, 1); } Logln("End: " + greg.GetTime()); }
/// <summary> /// Compute the month and date on which this holiday falls in the year /// containing the date "date". First figure out which date Easter lands on /// in this year, and then add the offset for this holiday to get the right /// date. /// <p> /// The algorithm here is taken from the <a /// href="http://www.faqs.org/faqs/calendars/faq/">Calendar FAQ</a>. /// </summary> /// private DateTime ComputeInYear(DateTime date, GregorianCalendar cal) { if (cal == null) { cal = calendar; } lock (cal) { cal.SetTime(date); int year = cal.Get(IBM.ICU.Util.Calendar.YEAR); int g = year % 19; // "Golden Number" of year - 1 int i = 0; // # of days from 3/21 to the Paschal full moon int j = 0; // Weekday (0-based) of Paschal full moon if (ILOG.J2CsMapping.Util.DateUtil.After(cal.GetTime(), cal.GetGregorianChange())) { // We're past the Gregorian switchover, so use the Gregorian // rules. int c = year / 100; int h = (c - c / 4 - (8 * c + 13) / 25 + 19 * g + 15) % 30; i = h - (h / 28) * (1 - (h / 28) * (29 / (h + 1)) * ((21 - g) / 11)); j = (year + year / 4 + i + 2 - c + c / 4) % 7; } else { // Use the old Julian rules. i = (19 * g + 15) % 30; j = (year + year / 4 + i) % 7; } int l = i - j; int m = 3 + (l + 40) / 44; // 1-based month in which Easter falls int d = l + 28 - 31 * (m / 4); // Date of Easter within that month cal.Clear(); cal.Set(IBM.ICU.Util.Calendar.ERA, IBM.ICU.Util.GregorianCalendar.AD); cal.Set(IBM.ICU.Util.Calendar.YEAR, year); cal.Set(IBM.ICU.Util.Calendar.MONTH, m - 1); // 0-based cal.Set(IBM.ICU.Util.Calendar.DATE, d); cal.GetTime(); // JDK 1.1.2 bug workaround cal.Add(IBM.ICU.Util.Calendar.DATE, daysAfterEaster); return(cal.GetTime()); } }
public void TestYear() { // Gregorian Calendar IBM.ICU.Util.Calendar gCal = new IBM.ICU.Util.GregorianCalendar(); DateTime gToday = gCal.GetTime(); gCal.Add(IBM.ICU.Util.Calendar.MONTH, 2); DateTime gFuture = gCal.GetTime(); DateFormat gDF = IBM.ICU.Text.DateFormat.GetDateInstance(gCal, IBM.ICU.Text.DateFormat.FULL); Logln("gregorian calendar: " + gDF.Format(gToday) + " + 2 months = " + gDF.Format(gFuture)); // Indian Calendar IndianCalendar iCal = new IndianCalendar(); DateTime iToday = iCal.GetTime(); iCal.Add(IBM.ICU.Util.Calendar.MONTH, 2); DateTime iFuture = iCal.GetTime(); DateFormat iDF = IBM.ICU.Text.DateFormat.GetDateInstance(iCal, IBM.ICU.Text.DateFormat.FULL); Logln("Indian calendar: " + iDF.Format(iToday) + " + 2 months = " + iDF.Format(iFuture)); }
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"); IBM.ICU.Util.TimeZone tz = new SimpleTimeZone(-18000000 + 3600000, "Montreal(FIXED)"); IBM.ICU.Util.GregorianCalendar cal = new IBM.ICU.Util.GregorianCalendar(tz, Locale.US); IBM.ICU.Util.GregorianCalendar cal2 = new IBM.ICU.Util.GregorianCalendar(tz, Locale.US); cal.Clear(); cal.Set(IBM.ICU.Util.Calendar.YEAR, 2001); cal.Set(IBM.ICU.Util.Calendar.MONTH, IBM.ICU.Util.Calendar.APRIL); cal.Set(IBM.ICU.Util.Calendar.DAY_OF_MONTH, 1); cal.Set(IBM.ICU.Util.Calendar.HOUR_OF_DAY, 12); // must be near local noon for // getSunRiseSet to work IBM.ICU.Text.DateFormat df = IBM.ICU.Text.DateFormat.GetTimeInstance(cal, IBM.ICU.Text.DateFormat.MEDIUM, Locale.US); IBM.ICU.Text.DateFormat df2 = IBM.ICU.Text.DateFormat.GetDateTimeInstance(cal, IBM.ICU.Text.DateFormat.MEDIUM, IBM.ICU.Text.DateFormat.MEDIUM, Locale.US); IBM.ICU.Text.DateFormat day = IBM.ICU.Text.DateFormat.GetDateInstance(cal, IBM.ICU.Text.DateFormat.MEDIUM, Locale.US); for (int i = 0; i < 30; i++) { astro.SetDate(cal.GetTime()); DateTime sunrise = DateUtil.DateFromJavaMillis(astro.GetSunRiseSet(true)); DateTime sunset = DateUtil.DateFromJavaMillis(astro.GetSunRiseSet(false)); cal2.SetTime(cal.GetTime()); cal2.Set(IBM.ICU.Util.Calendar.SECOND, 0); cal2.Set(IBM.ICU.Util.Calendar.MILLISECOND, 0); cal2.Set(IBM.ICU.Util.Calendar.HOUR_OF_DAY, USNO[4 * i + 0]); cal2.Set(IBM.ICU.Util.Calendar.MINUTE, USNO[4 * i + 1]); DateTime exprise = cal2.GetTime(); cal2.Set(IBM.ICU.Util.Calendar.HOUR_OF_DAY, USNO[4 * i + 2]); cal2.Set(IBM.ICU.Util.Calendar.MINUTE, USNO[4 * i + 3]); DateTime expset = cal2.GetTime(); // Compute delta of what we got to the USNO data, in seconds int deltarise = Math.Abs((int)((sunrise.Ticks / 10000) - (exprise.Ticks / 10000)) / 1000); int deltaset = Math.Abs((int)((sunset.Ticks / 10000) - (expset.Ticks / 10000)) / 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. 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(IBM.ICU.Util.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); }