/// <summary> /// Returns the Largest DateTime less then or equal to the specified DateTime object to a specified Precision Value at a specified Precision Level /// </summary> /// <param name="datetime">A DateTime object </param> /// <param name="value">A long precision value which makes a multiple to the precision level</param> /// <param name="precisionLevel">One of DateTimePrecisionLevel values</param> /// <exception cref="ArgumentException"></exception> /// <returns>A new DateTime object Floored or reduced to Specified Precision Levels</returns> public static DateTime Floor(this DateTime datetime, long value, DateTimePrecisionLevel precisionLevel) { if (value < 1) { throw new ArgumentException("value can not be less then 1", "value"); } const long milliSeconds = 10000, seconds = milliSeconds * 1000, minutes = seconds * 60, hours = minutes * 60, days = hours * 24; long precisionvalue; switch (precisionLevel) { case DateTimePrecisionLevel.MilliSeconds: { precisionvalue = milliSeconds; break; } case DateTimePrecisionLevel.Seconds: { precisionvalue = seconds; break; } case DateTimePrecisionLevel.Minutes: { precisionvalue = minutes; break; } case DateTimePrecisionLevel.Hours: { precisionvalue = hours; break; } case DateTimePrecisionLevel.Days: { precisionvalue = days; break; } default: { throw new ArgumentException( String.Format( "Precission Level can only be one of DateTimePrecisionLevels. {0} is not a valid value for this argument", (int)precisionLevel), "precisionLevel"); } } return(new DateTime(datetime.Ticks - (datetime.Ticks % (precisionvalue * value)), datetime.Kind)); }
/// <summary> /// Returns the Largest DateTime less then or equal to the specified DateTime object at a specified Precision Level /// </summary> /// <param name="datetime">A DateTime object</param> /// <param name="precisionLevel">One of DateTimePrecisionLevel values</param> /// <returns>A new DateTime object Floored or reduced to Specified Precision Levels</returns> public static DateTime Floor(this DateTime datetime, DateTimePrecisionLevel precisionLevel) { const long milliSeconds = 10000, seconds = milliSeconds*1000, minutes = seconds*60, hours = minutes*60, days = hours * 24; long precisionvalue; switch (precisionLevel) { case DateTimePrecisionLevel.MilliSeconds: { precisionvalue = milliSeconds; break; } case DateTimePrecisionLevel.Seconds: { precisionvalue = seconds; break; } case DateTimePrecisionLevel.Minutes: { precisionvalue = minutes; break; } case DateTimePrecisionLevel.Hours: { precisionvalue = hours; break; } case DateTimePrecisionLevel.Days: { precisionvalue = days; break; } default: { throw new ArgumentException( String.Format( "Precission Level can only be one of DateTimePrecisionLevel Values. {0} is not a valid value for this argument", (int) precisionLevel), "precisionLevel"); } } return new DateTime(datetime.Ticks - (datetime.Ticks % precisionvalue), datetime.Kind); }