Date AddDays(Date date, int days) { GregorianCalendar cal = new GregorianCalendar(); cal.Time = date; cal.Add(CalendarField.Date, days); return(cal.Time); }
private void PrintBlockDeletionTime(Logger log) { log.Info(DFSConfigKeys.DfsNamenodeStartupDelayBlockDeletionSecKey + " is set to " + DFSUtil.DurationToString(pendingPeriodInMs)); SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd HH:mm:ss"); Calendar calendar = new GregorianCalendar(); calendar.Add(Calendar.Second, (int)(this.pendingPeriodInMs / 1000)); log.Info("The block deletion will start around " + sdf.Format(calendar.GetTime()) ); }
public virtual void Yesterday() { GregorianCalendar cal = new GregorianCalendar(SystemReader.GetInstance().GetTimeZone (), SystemReader.GetInstance().GetLocale()); DateTime parse = GitDateParser.Parse("yesterday", cal); cal.Add(Calendar.DATE, -1); cal.Set(Calendar.HOUR_OF_DAY, 0); cal.Set(Calendar.MINUTE, 0); cal.Set(Calendar.SECOND, 0); cal.Set(Calendar.MILLISECOND, 0); cal.Set(Calendar.MILLISECOND, 0); NUnit.Framework.Assert.AreEqual(cal.GetTime(), parse); }
private static void GenerateKeyPair(Context context, String alias) { Calendar start = new GregorianCalendar(); Calendar end = new GregorianCalendar(); end.Add(CalendarField.Year, 100); KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(context) .SetAlias(alias) .SetSubject(new X500Principal("CN=" + alias)) .SetSerialNumber(BigInteger.One) .SetStartDate(start.Time) .SetEndDate(end.Time) .Build(); KeyPairGenerator gen = KeyPairGenerator.GetInstance("RSA", "AndroidKeyStore"); gen.Initialize(spec); gen.GenerateKeyPair(); }
/// <summary> /// Test the functions getXxxMinimum() and getXxxMaximum() by marching a test /// calendar 'cal' through 'numberOfDays' sequential days starting with /// 'startDate'. For each date, read a field value along with its reported /// actual minimum and actual maximum. These values are checked against one /// another as well as against getMinimum(), getGreatestMinimum(), /// getLeastMaximum(), and getMaximum(). We expect to see: /// 1. minimum <= actualMinimum <= greatestMinimum <= leastMaximum <= /// actualMaximum <= maximum /// 2. actualMinimum <= value <= actualMaximum /// Note: In addition to outright failures, this test reports some results as /// warnings. These are not generally of concern, but they should be /// evaluated by a human. To see these, run this test in verbose mode. /// </summary> /// /// <param name="cal">the calendar to be tested</param> /// <param name="fieldsToTest">an array of field values to be tested, e.g., new int[] {Calendar.MONTH, Calendar.DAY_OF_MONTH }. It only makes senseto test the day fields; the time fields are not tested by thismethod. If null, then test all standard fields.</param> /// <param name="startDate">the first date to test</param> /// <param name="testDuration">if positive, the number of days to be tested. If negative, thenumber of seconds to run the test.</param> public void DoLimitsTest(Calendar cal, int[] fieldsToTest, DateTime startDate, int testDuration) { GregorianCalendar greg = new GregorianCalendar(); greg.SetTime(startDate); Logln("Start: " + startDate); if (fieldsToTest == null) { fieldsToTest = new int[] { IBM.ICU.Util.Calendar.ERA, IBM.ICU.Util.Calendar.YEAR, IBM.ICU.Util.Calendar.MONTH, IBM.ICU.Util.Calendar.WEEK_OF_YEAR, IBM.ICU.Util.Calendar.WEEK_OF_MONTH, IBM.ICU.Util.Calendar.DAY_OF_MONTH, IBM.ICU.Util.Calendar.DAY_OF_YEAR, IBM.ICU.Util.Calendar.DAY_OF_WEEK_IN_MONTH, IBM.ICU.Util.Calendar.YEAR_WOY, IBM.ICU.Util.Calendar.EXTENDED_YEAR }; } // Keep a record of minima and maxima that we actually see. // These are kept in an array of arrays of hashes. Hashtable[][] limits = (Hashtable[][])ILOG.J2CsMapping.Collections.Arrays.CreateJaggedArray(typeof(Hashtable), fieldsToTest.Length, 2); Object nub = new Object(); // Meaningless placeholder // This test can run for a long time; show progress. long millis = DateTime.Now.Millisecond; long mark = millis + 5000; // 5 sec millis -= testDuration * 1000; // stop time if testDuration<0 for (int i = 0; (testDuration > 0) ? i < testDuration : DateTime.Now.Millisecond < millis; ++i) { if (DateTime.Now.Millisecond >= mark) { Logln("(" + i + " days)"); mark += 5000; // 5 sec } cal.SetTimeInMillis(greg.GetTimeInMillis()); for (int j = 0; j < fieldsToTest.Length; ++j) { int f = fieldsToTest[j]; int v = cal.Get(f); int minActual = cal.GetActualMinimum(f); int maxActual = cal.GetActualMaximum(f); int minLow = cal.GetMinimum(f); int minHigh = cal.GetGreatestMinimum(f); int maxLow = cal.GetLeastMaximum(f); int maxHigh = cal.GetMaximum(f); // Fetch the hash for this field and keep track of the // minima and maxima. Hashtable[] h = limits[j]; if (h[0] == null) { h[0] = new Hashtable(); h[1] = new Hashtable(); } ILOG.J2CsMapping.Collections.Collections.Put(h[0], ((int)(minActual)), nub); ILOG.J2CsMapping.Collections.Collections.Put(h[1], ((int)(maxActual)), nub); if (minActual < minLow || minActual > minHigh) { Errln("Fail: " + YmdToString(cal) + " Range for min of " + FIELD_NAME[f] + "=" + minLow + ".." + minHigh + ", actual_min=" + minActual); } if (maxActual < maxLow || maxActual > maxHigh) { Errln("Fail: " + YmdToString(cal) + " Range for max of " + FIELD_NAME[f] + "=" + maxLow + ".." + maxHigh + ", actual_max=" + maxActual); } if (v < minActual || v > maxActual) { Errln("Fail: " + YmdToString(cal) + " " + FIELD_NAME[f] + "=" + v + ", actual range=" + minActual + ".." + maxActual + ", allowed=(" + minLow + ".." + minHigh + ")..(" + maxLow + ".." + maxHigh + ")"); } } greg.Add(IBM.ICU.Util.Calendar.DAY_OF_YEAR, 1); } // Check actual maxima and minima seen against ranges returned // by API. StringBuilder buf = new StringBuilder(); for (int j_0 = 0; j_0 < fieldsToTest.Length; ++j_0) { int f_1 = fieldsToTest[j_0]; buf.Length = 0; buf.Append(FIELD_NAME[f_1]); Hashtable[] h_2 = limits[j_0]; bool fullRangeSeen = true; for (int k = 0; k < 2; ++k) { int rangeLow = (k == 0) ? cal.GetMinimum(f_1) : cal .GetLeastMaximum(f_1); int rangeHigh = (k == 0) ? cal.GetGreatestMinimum(f_1) : cal .GetMaximum(f_1); // If either the top of the range or the bottom was never // seen, then there may be a problem. if (h_2[k][((int)(rangeLow))] == null || h_2[k][((int)(rangeHigh))] == null) { fullRangeSeen = false; } buf.Append((k == 0) ? " minima seen=(" : "; maxima seen=("); for (IIterator e = new ILOG.J2CsMapping.Collections.IteratorAdapter(h_2[k].Keys.GetEnumerator()); e.HasNext();) { int v_3 = ((Int32)e.Next()); buf.Append(" " + v_3); } buf.Append(") range=" + rangeLow + ".." + rangeHigh); } if (fullRangeSeen) { Logln("OK: " + buf.ToString()); } else { // This may or may not be an error -- if the range of dates // we scan over doesn't happen to contain a minimum or // maximum, it doesn't mean some other range won't. Logln("Warning: " + buf.ToString()); } } Logln("End: " + greg.GetTime()); }