示例#1
0
 public void ShiftBySeconds(double value)
 {
     if (value > 0.0d)
     {
         /*
          * steps is positive
          * increase min, max by the amount of steps only
          * if they will be less than or equal bounds max
          */
         if (Minimum.TryAddSeconds(value.NotAbove((Bounds.Maximum - Minimum).Seconds()), out DateTime dtn))
         {
             Minimum = dtn;
         }
         if (Maximum.TryAddSeconds(value.NotAbove((Bounds.Maximum - Maximum).Seconds()), out DateTime dtx))
         {
             Maximum = dtx;
         }
     }
     else if (value < 0.0d)
     {
         /*
          * steps is negative
          * decrease min, max by the amount of steps only
          * if they will be greater than or equal bounds min
          */
         // This is not a mistake, I'll actually add a negative to a negative
         if (Minimum.TryAddSeconds(value.NotBelow((Bounds.Minimum - Minimum).Seconds()), out DateTime dtn))
         {
             Minimum = dtn;
         }
         if (Maximum.TryAddSeconds(value.NotBelow((Bounds.Minimum - Maximum).Seconds()), out DateTime dtx))
         {
             Maximum = dtx;
         }
     }
 }
示例#2
0
        public DateRangeLister(DateTime minimum, DateTime maximum, DateTimeUnit unit)
            : base(minimum, maximum)
        {
            switch (unit)
            {
            case DateTimeUnit.Year:
                _count        = (int)Maximum.Years(Minimum);
                _getNextValue = () =>
                {
                    if (Index < 0)
                    {
                        return(Minimum);
                    }
                    if (Index >= _count - 1)
                    {
                        return(null);
                    }
                    if (Minimum.TryAddYears(Index + 1, out DateTime result))
                    {
                        return(result);
                    }
                    return(null);
                };
                break;

            case DateTimeUnit.Month:
                _count        = (int)Maximum.Months(Minimum).NotAbove(int.MaxValue);
                _getNextValue = () =>
                {
                    if (Index < 0)
                    {
                        return(Minimum);
                    }
                    if (Index >= _count - 1)
                    {
                        return(null);
                    }
                    if (Minimum.TryAddMonths(Index + 1, out DateTime result))
                    {
                        return(result);
                    }
                    return(null);
                };
                break;

            case DateTimeUnit.Hour:
                _count        = (int)Maximum.Hours(Minimum).NotAbove(int.MaxValue);
                _getNextValue = () =>
                {
                    if (Index < 0)
                    {
                        return(Minimum);
                    }
                    if (Index >= _count - 1)
                    {
                        return(null);
                    }
                    if (Minimum.TryAddHours(Index + 1, out DateTime result))
                    {
                        return(result);
                    }
                    return(null);
                };
                break;

            case DateTimeUnit.Minute:
                _count        = (int)Maximum.Minutes(Minimum).NotAbove(int.MaxValue);
                _getNextValue = () =>
                {
                    if (Index < 0)
                    {
                        return(Minimum);
                    }
                    if (Index >= _count - 1)
                    {
                        return(null);
                    }
                    if (Minimum.TryAddMinutes(Index + 1, out DateTime result))
                    {
                        return(result);
                    }
                    return(null);
                };
                break;

            case DateTimeUnit.Second:
                _count        = (int)Maximum.Seconds(Minimum).NotAbove(int.MaxValue);
                _getNextValue = () =>
                {
                    if (Index < 0)
                    {
                        return(Minimum);
                    }
                    if (Index >= _count - 1)
                    {
                        return(null);
                    }
                    if (Minimum.TryAddSeconds(Index + 1, out DateTime result))
                    {
                        return(result);
                    }
                    return(null);
                };
                break;

            case DateTimeUnit.Millisecond:
                _count        = (int)Maximum.Milliseconds(Minimum).NotAbove(int.MaxValue);
                _getNextValue = () =>
                {
                    if (Index < 0)
                    {
                        return(Minimum);
                    }
                    if (Index >= _count - 1)
                    {
                        return(null);
                    }
                    if (Minimum.TryAddMilliseconds(Index + 1, out DateTime result))
                    {
                        return(result);
                    }
                    return(null);
                };
                break;

            default:                     // default to add day
                _count        = (int)Maximum.Days(Minimum).NotAbove(int.MaxValue);
                _getNextValue = () =>
                {
                    if (Index < 0)
                    {
                        return(Minimum);
                    }
                    if (Index >= _count - 1)
                    {
                        return(null);
                    }
                    if (Minimum.TryAddDays(Index + 1, out DateTime result))
                    {
                        return(result);
                    }
                    return(null);
                };
                break;
            }
        }