public static void ShouldMatch(this DateTime actual, DateTime expected)
		{
			actual = actual.AddTicks(-(actual.Ticks % TimeSpan.TicksPerMillisecond));
			expected = expected.AddTicks(-(expected.Ticks % TimeSpan.TicksPerMillisecond));

			ShouldBeTestExtensions.ShouldBe(actual, expected);
		}
Example #2
0
		public static DateTime TruncateTo(this DateTime dateTime, TimeSpan timeSpan)
		{

			if (timeSpan == TimeSpan.Zero) return dateTime; 

			return dateTime.AddTicks(-(dateTime.Ticks % timeSpan.Ticks));
		}
        public static DateTime Truncate(this DateTime dateTime, TimeSpan timeSpan)
        {
            if (timeSpan == TimeSpan.Zero)
                return dateTime;

            return DateTime.SpecifyKind(dateTime.AddTicks(-(dateTime.Ticks % timeSpan.Ticks)), dateTime.Kind);
        }
Example #4
0
 /// <summary>
 /// Extension method to round a datetime down by a timespan interval.
 /// </summary>
 /// <param name="dateTime">Base DateTime object we're rounding down.</param>
 /// <param name="interval">Timespan interval to round to.</param>
 /// <returns>Rounded datetime</returns>
 public static DateTime RoundDown(this DateTime dateTime, TimeSpan interval)
 {
     if (interval == TimeSpan.Zero)
     {
         // divide by zero exception
         return dateTime;
     }
     return dateTime.AddTicks(-(dateTime.Ticks % interval.Ticks));
 }
Example #5
0
 /// <summary>
 /// Adds a generic AddType to a DateTime object
 /// </summary>
 /// <param name="now"><seealso cref="System.DateTime"/></param>
 /// <param name="adder">Type structure that acts as a switcher for what type of add to perform</param>
 /// <param name="much">How much AddType to add to each element for creating list of data</param>
 /// <returns>A DateTime object with the added AddType amounts</returns>
 public static DateTime Add(this DateTime now, AddType adder, double much)
 {
     DateTime ret = now;
     switch (adder)
     {
         case AddType.Years:
             {
                 ret = now.AddYears((int)much);
                 break;
             }
         case AddType.Months:
             {
                 ret = now.AddMonths((int)much);
                 break;
             }
         case AddType.Days:
             {
                 ret = now.AddDays(much);
                 break;
             }
         case AddType.Hours:
             {
                 ret = now.AddHours(much);
                 break;
             }
         case AddType.Minutes:
             {
                 ret = now.AddMinutes(much);
                 break;
             }
         case AddType.Seconds:
             {
                 ret = now.AddSeconds(much);
                 break;
             }
         case AddType.Milliseconds:
             {
                 ret = now.AddMilliseconds(much);
                 break;
             }
         case AddType.Ticks:
             {
                 ret = now.AddTicks((long)much);
                 break;
             }
     }
     return ret;
 }
Example #6
0
        /// <summary>
        /// Returns a new DateTime that adds the specified number of microseconds to the value of this instance.
        /// </summary>
        /// <param name="dateTime">The DateTime to add microseconds to.</param>
        /// <param name="value">A number of whole and fractional microseconds. The value parameter can be negative or positive. Note that this value is rounded to the nearest integer.</param>
        /// <returns>An object whose value is the sum of the date and time represented by this instance and the number of microseconds represented by value.</returns>
        public static DateTime AddMicroseconds(this DateTime dateTime, double value)
        {
            const long MaxValue = long.MaxValue / TimeSpanExtensions.TicksPerMicrosecond;
            if (value > MaxValue)
            {
                throw new ArgumentOutOfRangeException("value", value,
                                                      string.Format(CultureInfo.InvariantCulture, "Value cannot be bigger than {0}", MaxValue));
            }

            const long MinValue = long.MinValue / TimeSpanExtensions.TicksPerMicrosecond;
            if (value < MinValue)
            {
                throw new ArgumentOutOfRangeException("value", value,
                                                      string.Format(CultureInfo.InvariantCulture, "Value cannot be smaller than {0}", MinValue));
            }

            long roundedValue = (long)Math.Round(value);
            long ticks = roundedValue * TimeSpanExtensions.TicksPerMicrosecond;
            return dateTime.AddTicks(ticks);
        }
        public static DateTime SubtractTicks(this DateTime date, long value) {
            if (value < 0)
                throw new ArgumentException("Value cannot be less than 0.", "value");

            return date.AddTicks(value * -1);
        }
 internal static DateTime AddMicroseconds(this DateTime self, int microseconds)
 {
     return self.AddTicks(microseconds * ticksPerMicrosecond);
 }
 /// <summary>
 /// Retrieves the yesterday date.
 /// </summary>
 /// <param name="today">The today.</param>
 /// <returns>yesterday date</returns>
 public static DateTime RetrieveYesterdayDate(this DateTime today)
 {
     DateTime yesterdayDate = today.AddTicks(-1);
     return yesterdayDate;
 }
 /// <summary>
 /// Round the given DateTime object by the given time interval. i.e. 10:09 rounded by 10 minutes would be 10:10
 /// </summary>
 /// <param name="dt">The given DateTime object</param>
 /// <param name="interval">The time interval to round by</param>
 /// <returns>The new rounded DateTime object</returns>
 public static DateTime Round(this DateTime dt, TimeSpan interval)
 {
     var halfIntervalTicks = ((interval.Ticks + 1) >> 1);
       return dt.AddTicks(halfIntervalTicks - ((dt.Ticks + halfIntervalTicks) % interval.Ticks));
 }
 /// <summary>
 /// Floor the given DateTime object by the given time interval. i.e. 10:09 floored by 10 minutes would be 10:00
 /// </summary>
 /// <param name="dt">The given DateTime object</param>
 /// <param name="interval">The time interval to floor by</param>
 /// <returns>The new floored DateTime object</returns>
 public static DateTime Floor(this DateTime dt, TimeSpan interval)
 {
     return dt.AddTicks(-(dt.Ticks % interval.Ticks));
 }
Example #12
0
		public static DateTime TruncSeconds(this DateTime dateTime)
		{
			return dateTime.AddTicks(-dateTime.Ticks % 600000000);
		}
        public static DateTimeOffset TruncateMilliseconds(this DateTimeOffset dto)
        {
            // From http://stackoverflow.com/a/1005222/335418

            return dto.AddTicks( - (dto.Ticks % TimeSpan.TicksPerSecond));
        }
 public static string ToSaml2DateTimeString(this DateTime dateTime)
 {
     return XmlConvert.ToString(dateTime.AddTicks(-(dateTime.Ticks % TimeSpan.TicksPerSecond)),
         XmlDateTimeSerializationMode.Utc);
 }
 public static DateTime SubtractTicks(this DateTime dateTime, long ticks)
 {
     return dateTime.AddTicks(-ticks);
 }
 public static DateTimeOffset Ceiling(this DateTimeOffset date, TimeSpan interval) {
     return date.AddTicks(interval.Ticks - (date.Ticks % interval.Ticks));
 }
 public static DateTimeOffset Floor(this DateTimeOffset date, TimeSpan interval) {
     return date.AddTicks(-(date.Ticks % interval.Ticks));
 }
 public static DateTimeOffset SubtractTicks(this DateTimeOffset dateTimeOffset, long ticks)
 {
     return dateTimeOffset.AddTicks(-ticks);
 }
Example #19
0
		public static DateTime Add(this DateTime value, TimeUnit timeUnit)
		{
			return value.AddTicks((Int64) timeUnit);
		}
Example #20
0
		public static DateTime Add(this DateTime value, TimeInterval timeInterval)
		{
			return value.AddTicks(timeInterval);
		}
Example #21
0
		/// <summary>
		/// 只保留时间到秒部分,清空毫秒
		/// </summary>
		/// <param name="time">时间</param>
		/// <returns></returns>
		public static DateTime Truncate(this DateTime time) {
			return time.AddTicks(-(time.Ticks % TimeSpan.TicksPerSecond));
		}
Example #22
0
        private static DateTime Truncate(this DateTime dateTime, TimeSpan timeSpan)
        {
            if (timeSpan == TimeSpan.Zero)
            return dateTime; // Or could throw an ArgumentException

             return dateTime.AddTicks(-(dateTime.Ticks % timeSpan.Ticks));
        }
Example #23
0
        public static DateTime ToSpecificUniversalTime(this DateTime value, bool isSummerPeriod)
        {
            if (value.Kind == DateTimeKind.Utc)
                throw new Exception("Value must be of local or unspecified kind");

            if (!TimeZoneInfo.Local.SupportsDaylightSavingTime)
                return value.ToUniversalTime();

            var baseUtcOffset = TimeZoneInfo.Local.BaseUtcOffset;

            var dateTime = new DateTime(value.AddTicks(-baseUtcOffset.Ticks).Ticks, DateTimeKind.Utc);

            if (isSummerPeriod)
            {
                var dayLightDelta = TimeZoneInfo.Local.GetAdjustmentRules()[0].DaylightDelta;

                dateTime = new DateTime(dateTime.AddTicks(-dayLightDelta.Ticks).Ticks, DateTimeKind.Utc);
            }

            return dateTime;
        }
Example #24
0
 public static DateTime Truncate (this DateTime val, long percisionTicks)
 {
     return val.AddTicks (- (val.Ticks % percisionTicks));
 }
 /// <summary>
 /// Subtracts given ticks from the given DateTime.
 /// </summary>
 /// <param name="date">The given DateTime.</param>
 /// <param name="ticks">Number of ticks to be subtracted.</param>
 /// <returns>The DateTime after the given ticks are subtracted.</returns>
 public static DateTime SubtractTicks(this DateTime date, int ticks)
 {
     return date.AddTicks(ticks.Negate());
 }
Example #26
0
 /// <summary>
 /// MongoDB 가 JSON 형식에서 DataTime 을 내부적으로 double이 아닌 long을 변경해서 저장하므로, .NET DateTime과 오차가 생길 수 있다.
 /// MongoDB에 저장된 정보 중 DateTime에 대한 비교는 꼭 ToMongoDateTime() 이용해서 DateTime을 변경한 후 비교해야 합니다.
 /// </summary>
 /// <param name="dateTime"></param>
 /// <returns></returns>
 public static DateTime ToMongoDateTime(this DateTime dateTime) {
     return dateTime.AddTicks(-(dateTime.Ticks % 10000));
 }
 /// <summary>
 /// Ceiling the given DateTime object by the given time interval. i.e. 10:01 ceilinged by 10 minutes would be 10:10
 /// </summary>
 /// <param name="dt">The given DateTime object</param>
 /// <param name="interval">The time interval to ceiling by</param>
 /// <returns>The new ceilinged DateTime object</returns>
 public static DateTime Ceiling(this DateTime dt, TimeSpan interval)
 {
     return dt.AddTicks(interval.Ticks - (dt.Ticks % interval.Ticks));
 }
 public static DateTime Round(this DateTime date, TimeSpan roundingInterval) {
     var halfIntervalTicks = ((roundingInterval.Ticks + 1) >> 1);
     return date.AddTicks(halfIntervalTicks - ((date.Ticks + halfIntervalTicks) % roundingInterval.Ticks));
 }
 public static DateTime Truncate(this DateTime dateTime, TimeSpan timeSpan)
 {
     return dateTime.AddTicks(-(dateTime.Ticks % timeSpan.Ticks));
 }
 public static DateTime WithoutMilliseconds(this DateTime dateTime)
 {
     return dateTime.AddTicks(-(dateTime.Ticks % TimeSpan.TicksPerSecond));
 }