コード例 #1
0
ファイル: DateTimeExtensions.cs プロジェクト: cairabbit/daf
 public static DateTime Interval(this DateTime date, DateTimePart? intervalType, int? intervalVal)
 {
     if (intervalType.HasValue && intervalVal.HasValue)
     {
         switch (intervalType.Value)
         {
             case DateTimePart.Year:
                 return date.AddYears(intervalVal.Value);
             case DateTimePart.Month:
                 return date.AddMonths(intervalVal.Value);
             case DateTimePart.Day:
                 return date.AddDays((double)intervalVal.Value);
             case DateTimePart.Hour:
                 return date.AddHours((double)intervalVal.Value);
             case DateTimePart.Munite:
                 return date.AddMinutes((double)intervalVal.Value);
             case DateTimePart.Second:
                 return date.AddSeconds((double)intervalVal.Value);
             case DateTimePart.Week:
                 return date.AddDays((double)intervalVal.Value * 7);
             case DateTimePart.Quarter:
                 return date.AddMonths(intervalVal.Value * 3);
         }
     }
     return date;
 }
コード例 #2
0
	    public static DateTime RemoveMilliseconds(this DateTime dateTime)
	    {
	        if (dateTime.Millisecond > 0)
	        {
	            dateTime = dateTime.AddSeconds(1);
	            dateTime = dateTime.AddMilliseconds(-dateTime.Millisecond);
	        }

	        return dateTime;
	    }
コード例 #3
0
ファイル: Extensions.cs プロジェクト: kLeZ/Gecko
 /// <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;
 }
コード例 #4
0
        public static DateTime RoundMinutes(this DateTime dateTime)
        {
            dateTime.AddSeconds(-dateTime.Second);
            dateTime.AddMilliseconds(-dateTime.Millisecond);

            var remainder = dateTime.Minute % 15;

            dateTime = dateTime.AddMinutes(remainder * -1);

            if (remainder > 7)
            {
                dateTime = dateTime.AddMinutes(15);
            }

            return dateTime;
        }
コード例 #5
0
 public static DateTime SubstractTimeStepUnit(this DateTime dt, TimeStepUnit tstep)
 {
   switch (tstep)
   {
     case TimeStepUnit.Year:
       return dt.AddYears(-1);
     case TimeStepUnit.Month:
       return dt.AddMonths(-1);
     case TimeStepUnit.Day:
       return dt.AddDays(-1);
     case TimeStepUnit.Hour:
       return dt.AddHours(-1);
     case TimeStepUnit.Minute:
       return dt.AddMinutes(-1);
     case TimeStepUnit.Second:
       return dt.AddSeconds(-1);
   }
   return dt;
 }
コード例 #6
0
        public static bool MatchesCronExpression(this DateTime theDateTime, string cronExpression)
        {
            CrontabSchedule schedule;
            ExceptionProvider ex;
            var valueOrException = CrontabSchedule.TryParse(cronExpression,
                input =>
                {
                    schedule = input;
                    return true;
                },
                exception =>
                {
                    ex = exception;
                    ex.Invoke();
                    return false;
                });
            

            var cronSchedule = CrontabSchedule.Parse(cronExpression);
            var nextOccurrence = cronSchedule.GetNextOccurrence(theDateTime.AddMinutes(-1).AddSeconds(-1 * theDateTime.Second));
            var matches = Math.Abs((theDateTime.AddSeconds(-1*theDateTime.Second) - nextOccurrence).TotalMinutes) < 1 ;
            return matches;
        }
コード例 #7
0
ファイル: GeneralHelpers.cs プロジェクト: techbuzzz/FlixSharp
 //public static DateTime FromUnixTime(this DateTime source, Int32 seconds)
 //{
 //    if (source == null)
 //        return new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(seconds);
 //    else
 //        return source.AddSeconds(seconds);
 //}
 public static DateTime FromUnixTime(this DateTime source, Int64 seconds)
 {
     if (source == null)
         return new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(seconds);
     else
         return source.AddSeconds(seconds);
 }
コード例 #8
0
 /// <summary>
 /// Trims the seconds from the <see cref="DateTime"></see> object
 /// </summary>
 /// <param name="source"></param>
 /// <returns></returns>
 public static DateTime TrimSeconds(this DateTime source)
 {
     return source.AddSeconds(-source.Second).AddMilliseconds(-source.Millisecond);
 }
コード例 #9
0
 /// <summary>
 /// Returns a DateTime a number of seconds in the past
 /// </summary>
 /// <param name="self">The DateTime to move</param>
 /// <param name="seconds">Number of seconds</param>
 /// <example>
 /// var now = DateTime.UtcNow;
 /// var nowMinus10 = now.SecondsAgo(10);
 /// Debug.Assert((now - nowMinus10).TotalSeconds == 10);
 /// </example>
 public static DateTime SecondsAgo(this DateTime self, double seconds)
 {
     return self.AddSeconds(-seconds);
 }
コード例 #10
0
 /// <summary>
 /// Adds one second to the given DateTime.
 /// </summary>
 /// <param name="date">The given DateTime.</param>
 /// <returns>The DateTime after one second is added.</returns>
 public static DateTime AddASecond(this DateTime date)
 {
     return date.AddSeconds(1);
 }
コード例 #11
0
        public static DateTime SubtractSeconds(this DateTime date, double value) {
            if (value < 0)
                throw new ArgumentException("Value cannot be less than 0.", "value");

            return date.AddSeconds(value * -1);
        }
コード例 #12
0
 public static bool HasExceeded(this DateTimeOffset creationTime, int seconds)
 {
     return (DateTimeOffsetHelper.UtcNow > creationTime.AddSeconds(seconds));
 }
コード例 #13
0
ファイル: DatePickerBackend.cs プロジェクト: m13253/xwt
		public static DateTime AddComponent(this DateTime dateTime, DateTimeComponent component, int value)
		{
			try {
				switch (component) {
					case DateTimeComponent.Second:
						return dateTime.AddSeconds (value);
					case DateTimeComponent.Minute:
						return dateTime.AddMinutes (value);
					case DateTimeComponent.Hour:
						return dateTime.AddHours (value);
					case DateTimeComponent.Day:
						return dateTime.AddDays (value);
					case DateTimeComponent.Month:
						return dateTime.AddMonths (value);
					case DateTimeComponent.Year:
						return dateTime.AddYears (value);
					default:
						return dateTime.AddSeconds (value);
				}
			} catch (ArgumentOutOfRangeException) {
				return dateTime;
			}
		}
コード例 #14
0
 /// <summary>
 /// Return the date with seconds and milliseconds stripped
 /// </summary>
 public static DateTime StripSeconds(this DateTime @this)
 {
     return @this.AddSeconds([email protected]).StripMilliseconds();
 }
コード例 #15
0
 public static DateTime RemoveSeconds(this DateTime seconds)
 {
     return seconds.AddSeconds(-seconds.Second).AddMilliseconds(-seconds.Millisecond);
 }
コード例 #16
0
        /// <summary>
        /// Warning: bug found. dt.Offset may be incorrect as offsets could be different if range crosses daylight saving switch, i.e. October - December, or month of November in the EST USA.
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="count"></param>
        /// <param name="unit"></param>
        /// <returns></returns>
        public static DateTimeOffset Add(this DateTimeOffset dt, int count, TimeUnits unit)
        {
            switch(unit)
            {
                case TimeUnits.Century:
                    return dt.AddYears(count*100);
                case TimeUnits.Day:
                    return dt.AddDays(count);
                case TimeUnits.Decade:
                    return dt.AddYears(count*10);
                case TimeUnits.Hour:
                    return dt.AddHours(count);
                case TimeUnits.Minute:
                    return dt.AddMinutes(count);
                case TimeUnits.Month:
                    return dt.AddMonths(count);
                case TimeUnits.Quarter:
                    return dt.AddMonths(count*3);
                case TimeUnits.Second:
                    return dt.AddSeconds(count);
                case TimeUnits.Week:
                    return dt.AddDays(count*7);
                case TimeUnits.Year:
                    return dt.AddYears(count);
            }

            throw new Exception("Adding \"{0}\" is not implemented.".SmartFormat(unit));
        }
コード例 #17
0
ファイル: DateTimeFu.cs プロジェクト: jheddings/flynn
        ///////////////////////////////////////////////////////////////////////
        public static DateTime SetSecond(this DateTime dt, int second)
        {
            if ((second < 0) || (second > 59)) {
                throw new ArgumentOutOfRangeException("second");
            }

            int diff = second - dt.Second;
            return dt.AddSeconds(diff);
        }
コード例 #18
0
        public static DateTime ChangeSecond(this DateTime date, int second) {
            if (second < 0 || second > 59)
                throw new ArgumentException("Value must be between 0 and 59.", "second");

            return date.AddSeconds(second - date.Second);
        }
コード例 #19
0
ファイル: DateTimeFu.cs プロジェクト: jheddings/flynn
 ///////////////////////////////////////////////////////////////////////
 public static DateTime NextSecond(this DateTime date)
 {
     DateTime next = date.AddSeconds(1);
     next = next.SetMillisecond(0);
     return next;
 }
コード例 #20
0
 public static DateTime SubtractSeconds(this DateTime dateTime, double seconds)
 {
     return dateTime.AddSeconds(-seconds);
 }
コード例 #21
0
ファイル: DateHelper.cs プロジェクト: rocketeerbkw/DNA
 public static bool IsInLastSeconds(this DateTime dateTime, int interval)
 {
     return (DateTime.Now <= dateTime.AddSeconds(interval));
 }
コード例 #22
0
 /// <summary>
 /// Subtracts given seconds from the given DateTime.
 /// </summary>
 /// <param name="date">The given DateTime.</param>
 /// <param name="seconds">Number of seconds to be subtracted.</param>
 /// <returns>The DateTime after the given seconds are subtracted.</returns>
 public static DateTime SubtractSeconds(this DateTime date, int seconds)
 {
     return date.AddSeconds(seconds.Negate());
 }
コード例 #23
0
 public static bool HasExpired(this DateTime creationTime, int seconds)
 {
     return (DateTime.UtcNow > creationTime.AddSeconds(seconds));
 }
コード例 #24
0
 /// <summary>
 /// Returns a DateTime a number of seconds in the future. 
 /// </summary>
 /// <param name="self">The DateTime to move</param>
 /// <param name="seconds">Number of seconds</param>
 /// <example>
 /// var now = DateTime.UtcNow;
 /// var nowPlus10 = now.SecondsSince(10);
 /// Debug.Assert((nowPlus10 - now).TotalSeconds == 10);
 /// </example>
 public static DateTime SecondsSince(this DateTime self, double seconds)
 {
     return self.AddSeconds(seconds);
 }
コード例 #25
0
 public static DateTime StripSeconds(this DateTime dateTime)
 {
     return dateTime.AddSeconds(-(dateTime.Second));
 }
コード例 #26
0
 /// <summary>
 /// Rounds the second component to the nearest specified time. Defaults to 1 second
 /// </summary>
 /// <param name="source"></param>
 /// <param name="rounding"></param>
 /// <returns></returns>
 public static DateTime RoundSeconds(this DateTime source, SecondRounding rounding = SecondRounding.Second)
 {
     switch (rounding)
     {
         case SecondRounding.Second:
             if (source.Second >= 30)
                 return source.AddMinutes(1).TrimSeconds();
             return source.AddSeconds(-source.Second).AddMilliseconds(-source.Millisecond);
         case SecondRounding.HalfSecond:
             if (source.Second < 15)
                 return source.TrimSeconds();
             if (source.Second >= 15 && source.Minute < 45)
                 return source.TrimSeconds().AddSeconds(30);
             return source.AddMinutes(1).TrimSeconds();
         default:
             throw new ArgumentOutOfRangeException("rounding");
     }
 }
コード例 #27
0
 public static DateTimeOffset SubtractSeconds(this DateTimeOffset dateTimeOffset, double seconds)
 {
     return dateTimeOffset.AddSeconds(-seconds);
 }
コード例 #28
0
 public static DateTime AddTime(this DateTime dateTime, Time timeToAdd)
 {
     return dateTime.AddSeconds(timeToAdd.Second).AddMinutes(timeToAdd.Minute).AddHours(timeToAdd.Hour);
 }
コード例 #29
0
 public static bool SecondTimeSpanHasPass(this DateTime sourceUtcDateTime, int seconds)
 {
     return sourceUtcDateTime.AddSeconds(seconds) < DateTime.UtcNow;
 }