/// <summary> /// Routine to increment a DateTime value. /// </summary> /// <param name="incrementType">Type of increment defined: seconds, minutes, hours, days or years.</param> /// <param name="sizeOfIncrement">Value of the increment to be applied.</param> /// <param name="origDateTime">Original date/time value to be incremented.</param> /// <param name="startDate">Date to return in the event the increment fails on an error.</param> /// <returns>DateTime value with increment applied.</returns> public DateTime IncrementDateTime(enRandomIncrementType incrementType, int sizeOfIncrement, DateTime origDateTime, DateTime startDate) { DateTime newDateTime = new DateTime(2000, 1, 1); try { switch (incrementType) { case enRandomIncrementType.enYears: newDateTime = origDateTime.AddYears(sizeOfIncrement); break; case enRandomIncrementType.enMonths: newDateTime = origDateTime.AddMonths(sizeOfIncrement); break; case enRandomIncrementType.enDays: newDateTime = origDateTime.AddDays(sizeOfIncrement); break; default: newDateTime = origDateTime.AddDays(sizeOfIncrement); break; } } catch (System.ArgumentOutOfRangeException) { newDateTime = startDate; } return(newDateTime); }
//properties //methods /// <summary> /// Creates a DataTable containing a set of random DateTime values. /// </summary> /// <param name="numRows">Num of rows with DateTime values to generate.</param> /// <param name="dataRequest">RandomDateTimeDataRequest object contains the definition for how to generate the random DateTime values.</param> /// <returns>ADO.NET DataTable containing the set of random values.</returns> public DataTable CreateRandomDataTable(int numRows, RandomDateTimeDataRequest dataRequest) { DataTable dt = null; enRandomOffsetType randOffsetType = enRandomOffsetType.enUnknown; enRandomIncrementType randDateIncrementType = enRandomIncrementType.enUnknown; enDateConversionType dateConversionType = enDateConversionType.DoNotConvert; if (dataRequest.ConvertGeneratedValueToInteger) { if (dataRequest.ConvertDateTo32BitInteger) { dateConversionType = enDateConversionType.ConvertDateTo32bitInt; } else if (dataRequest.ConvertTimeTo32BitInteger) { dateConversionType = enDateConversionType.ConvertTimeTo32bitInt; } else if (dataRequest.ConvertDateTimeTo64BitInteger) { dateConversionType = enDateConversionType.ConvertDateTimeTo64bitInt; } else { dateConversionType = enDateConversionType.DoNotConvert; } } if (dataRequest.RangeOfDates) { dt = CreateRangeDataTable(numRows, dataRequest.EarliestDate, dataRequest.LatestDate, dataRequest.SpecifyTimeForEachDay, dataRequest.EarliestTime, dataRequest.LatestTime, dateConversionType); } else if (dataRequest.OffsetFromCurrentDate) { randOffsetType = GetRandomOffsetType(dataRequest); dt = CreateOffsetFromCurrentDateDataTable(randOffsetType, numRows, dataRequest.MinimumOffset, dataRequest.MaximumOffset, dataRequest.SpecifyTimeForEachDay, dataRequest.EarliestTime, dataRequest.LatestTime, dateConversionType); } else if (dataRequest.OffsetFromDataTableDate) { randOffsetType = GetRandomOffsetType(dataRequest); dt = CreateOffsetFromDataTableDate(randOffsetType, numRows, dataRequest.MinimumOffset, dataRequest.MaximumOffset, dataRequest.SpecifyTimeForEachDay, dataRequest.EarliestTime, dataRequest.LatestTime, dateConversionType); } else if (dataRequest.OutputSequentialDates) { randDateIncrementType = GetRandomDateIncrementType(dataRequest); dt = CreateDateSequenceDataTable(randDateIncrementType, numRows, dataRequest.IncrementSize, dataRequest.StartSequentialDate, dataRequest.EndSequentialDate, dataRequest.SpecifyTimeForEachDay, dataRequest.EarliestTime, dataRequest.LatestTime, dataRequest.MinNumDatesPerIncrement, dataRequest.MaxNumDatesPerIncrement, dataRequest.InitStartSequentialDate, dateConversionType); } else { dt = new DataTable(); } return(dt); }
private enRandomIncrementType GetRandomDateIncrementType(RandomDateTimeDataRequest dataRequest) { enRandomIncrementType incrementType = enRandomIncrementType.enUnknown; if (dataRequest.YearsIncrement) { incrementType = enRandomIncrementType.enYears; } else if (dataRequest.MonthsIncrement) { incrementType = enRandomIncrementType.enMonths; } else { incrementType = enRandomIncrementType.enDays; } return(incrementType); }
/// <summary> /// Creates a set of sequential numbers that start from a user specified start number and are incremented by a user specified number. This is a testing routine. /// </summary> /// <param name="incrementType">Time slice to increment with (seconds, minutes, hours, days, years).</param> /// <param name="numRows">Number of random values to generate.</param> /// <param name="incrementSize">Number to increment by.</param> /// <param name="startDateForSequence">First date in the sequence.</param> /// <param name="endDateForSequence">Maximum date in the sequence.</param> /// <param name="generateRandomTime">If true, a randomized time value will be generated for each sequential date.</param> /// <param name="fromTime">Minimum time value to generate.</param> /// <param name="toTime">Maximum time value to generate.</param> /// <param name="minNumDatesPerIncrement">For each date increment, the minimum number of dates that can be generated with same date into the sequence.</param> /// <param name="maxNumDatesPerIncrement">For each date increment, the maximum number of dates that can be generated with same date into the sequence.</param> /// <param name="initStartDateForSequence">If maximum date is passed during sequence generation, the sequence will be restarted with this date and new values will be appended to the existing sequence.</param> /// <param name="dateConversionType">Determines whether or not to convert the DateTime value to an integer. Useful for data warehousing scenarios.</param> /// <returns>ADO.NET DataTable containing the set of random values.</returns> /// <remarks>This routine is used for testing.</remarks> public DataTable CreateDateSequencePreviewDataTable(enRandomIncrementType incrementType, int numRows, string incrementSize, string startDateForSequence, string endDateForSequence, bool generateRandomTime, string fromTime, string toTime, string minNumDatesPerIncrement, string maxNumDatesPerIncrement, string initStartDateForSequence, enDateConversionType dateConversionType) { DataTable dt = new DataTable(); int sizeOfIncrement = 1; DateTime startDate = DateTime.MinValue; DateTime endDate = DateTime.MaxValue; DateTime currDate = DateTime.Now; DateTime initDate = DateTime.MinValue; int minIncrementDates = 1; int maxIncrementDates = 1; TimeSpan fromTimeTs = Convert.ToDateTime(fromTime).TimeOfDay; TimeSpan toTimeTs = Convert.ToDateTime(toTime).TimeOfDay; double fromSeconds = fromTimeTs.TotalSeconds; double toSeconds = toTimeTs.TotalSeconds; double randNum = 0.0; try { sizeOfIncrement = AppTextGlobals.ConvertStringToInt(incrementSize, 1); startDate = AppTextGlobals.ConvertStringToDateTime(startDateForSequence, new DateTime(1000, 1, 1)); endDate = AppTextGlobals.ConvertStringToDateTime(endDateForSequence, new DateTime(5999, 12, 31)); initDate = AppTextGlobals.ConvertStringToDateTime(initStartDateForSequence, new DateTime(1000, 1, 1)); currDate = IncrementDateTime(incrementType, -sizeOfIncrement, startDate, startDate); minIncrementDates = AppTextGlobals.ConvertStringToInt(minNumDatesPerIncrement, 1); maxIncrementDates = AppTextGlobals.ConvertStringToInt(maxNumDatesPerIncrement, 1); DataColumn dc = new DataColumn("RandomValue"); //dc.DataType = Type.GetType("System.DateTime"); switch (dateConversionType) { case enDateConversionType.DoNotConvert: dc.DataType = Type.GetType("System.DateTime"); break; case enDateConversionType.ConvertDateTo32bitInt: dc.DataType = Type.GetType("System.Int32"); break; case enDateConversionType.ConvertTimeTo32bitInt: dc.DataType = Type.GetType("System.Int32"); break; case enDateConversionType.ConvertDateTimeTo64bitInt: dc.DataType = Type.GetType("System.Int64"); break; default: dc.DataType = Type.GetType("System.DateTime"); break; } dt.Columns.Add(dc); for (int i = 0; i < numRows; i++) { currDate = IncrementDateTime(incrementType, sizeOfIncrement, currDate, startDate); if (currDate > endDate) { currDate = initDate; } int numDatesToGenerate = _rn.GenerateRandomInt(minIncrementDates, maxIncrementDates); DateTime dtm = currDate; for (int n = 0; n < numDatesToGenerate; n++) { DataRow dr = dt.NewRow(); if (generateRandomTime) { randNum = _rn.GenerateRandomNumber(fromSeconds, toSeconds); TimeSpan ts2 = new TimeSpan(0, 0, 0, (int)randNum, 0); try { dtm = currDate.AddSeconds(ts2.TotalSeconds); } catch (System.ArgumentOutOfRangeException) { dtm = startDate; currDate = startDate; } } //dr[0] = dtm; switch (dateConversionType) { case enDateConversionType.DoNotConvert: dr[0] = dtm; break; case enDateConversionType.ConvertDateTo32bitInt: dr[0] = Convert.ToInt32(dtm.ToString("yyyyMMdd")); break; case enDateConversionType.ConvertTimeTo32bitInt: dr[0] = Convert.ToInt32(dtm.ToString("HHmmss")); break; case enDateConversionType.ConvertDateTimeTo64bitInt: dr[0] = Convert.ToInt64(dtm.ToString("yyyyMMddHHmmss")); break; default: dr[0] = dtm; break; } dt.Rows.Add(dr); } } } catch (System.Exception ex) { _msg.Length = 0; _msg.Append("Error in CreateSequencePreviewDataTable routine.\r\n"); _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex)); throw new System.Exception(_msg.ToString()); } finally { ; } return(dt); }