public static string GetStringValue(this DateMathTimeUnit value) { switch (value) { case DateMathTimeUnit.Second: return("s"); case DateMathTimeUnit.Minute: return("m"); case DateMathTimeUnit.Hour: return("h"); case DateMathTimeUnit.Day: return("d"); case DateMathTimeUnit.Week: return("w"); case DateMathTimeUnit.Month: return("M"); case DateMathTimeUnit.Year: return("y"); default: throw new ArgumentOutOfRangeException(nameof(value), value, null); } }
/// <summary> /// Instantiates a new instance of <see cref="DateMathTime"/> from a factor and interval. /// </summary> public DateMathTime(int factor, DateMathTimeUnit interval) =>
private void SetWholeFactorIntervalAndSeconds(double milliseconds, MidpointRounding rounding) { double fraction; int whole; if (milliseconds >= MillisecondsInAWeek) { fraction = milliseconds / MillisecondsInAWeek; if (TryGetIntegerGreaterThanZero(fraction, out whole)) { this.Factor = whole; this.Interval = DateMathTimeUnit.Week; this._approximateSeconds = Factor * (MillisecondsInAWeek / MillisecondsInASecond); return; } } if (milliseconds >= MillisecondsInADay) { fraction = milliseconds / MillisecondsInADay; if (TryGetIntegerGreaterThanZero(fraction, out whole)) { this.Factor = whole; this.Interval = DateMathTimeUnit.Day; this._approximateSeconds = Factor * (MillisecondsInADay / MillisecondsInASecond); return; } } if (milliseconds >= MillisecondsInAnHour) { fraction = milliseconds / MillisecondsInAnHour; if (TryGetIntegerGreaterThanZero(fraction, out whole)) { this.Factor = whole; this.Interval = DateMathTimeUnit.Hour; this._approximateSeconds = Factor * (MillisecondsInAnHour / MillisecondsInASecond); return; } } if (milliseconds >= MillisecondsInAMinute) { fraction = milliseconds / MillisecondsInAMinute; if (TryGetIntegerGreaterThanZero(fraction, out whole)) { this.Factor = whole; this.Interval = DateMathTimeUnit.Minute; this._approximateSeconds = Factor * (MillisecondsInAMinute / MillisecondsInASecond); return; } } if (milliseconds >= MillisecondsInASecond) { fraction = milliseconds / MillisecondsInASecond; if (TryGetIntegerGreaterThanZero(fraction, out whole)) { this.Factor = whole; this.Interval = DateMathTimeUnit.Second; this._approximateSeconds = Factor; return; } } // round to nearest second, using specified rounding this.Factor = Convert.ToInt32(Math.Round(milliseconds / MillisecondsInASecond, rounding)); this.Interval = DateMathTimeUnit.Second; this._approximateSeconds = Factor; }
private void SetWholeFactorIntervalAndSeconds(double factor, DateMathTimeUnit interval, MidpointRounding rounding) { var fraction = factor; double milliseconds; // if the factor is already a whole number then use it if (TryGetIntegerGreaterThanZero(fraction, out var whole)) { this.Factor = whole; this.Interval = interval; switch (interval) { case DateMathTimeUnit.Second: this._approximateSeconds = whole; break; case DateMathTimeUnit.Minute: this._approximateSeconds = whole * (MillisecondsInAMinute / MillisecondsInASecond); break; case DateMathTimeUnit.Hour: this._approximateSeconds = whole * (MillisecondsInAnHour / MillisecondsInASecond); break; case DateMathTimeUnit.Day: this._approximateSeconds = whole * (MillisecondsInADay / MillisecondsInASecond); break; case DateMathTimeUnit.Week: this._approximateSeconds = whole * (MillisecondsInAWeek / MillisecondsInASecond); break; case DateMathTimeUnit.Month: this._approximateSeconds = whole * (MillisecondsInAMonthApproximate / MillisecondsInASecond); break; case DateMathTimeUnit.Year: this._approximateSeconds = whole * (MillisecondsInAYearApproximate / MillisecondsInASecond); break; default: throw new ArgumentOutOfRangeException(nameof(interval), interval, null); } return; } switch (interval) { case DateMathTimeUnit.Second: milliseconds = factor * MillisecondsInASecond; break; case DateMathTimeUnit.Minute: milliseconds = factor * MillisecondsInAMinute; break; case DateMathTimeUnit.Hour: milliseconds = factor * MillisecondsInAnHour; break; case DateMathTimeUnit.Day: milliseconds = factor * MillisecondsInADay; break; case DateMathTimeUnit.Week: milliseconds = factor * MillisecondsInAWeek; break; case DateMathTimeUnit.Month: if (TryGetIntegerGreaterThanZero(fraction, out whole)) { this.Factor = whole; this.Interval = interval; this._approximateSeconds = whole * (MillisecondsInAMonthApproximate / MillisecondsInASecond); return; } milliseconds = factor * MillisecondsInAMonthApproximate; break; case DateMathTimeUnit.Year: if (TryGetIntegerGreaterThanZero(fraction, out whole)) { this.Factor = whole; this.Interval = interval; this._approximateSeconds = whole * (MillisecondsInAYearApproximate / MillisecondsInASecond); return; } fraction = fraction * MonthsInAYear; if (TryGetIntegerGreaterThanZero(fraction, out whole)) { this.Factor = whole; this.Interval = DateMathTimeUnit.Month; this._approximateSeconds = whole * (MillisecondsInAMonthApproximate / MillisecondsInASecond); return; } milliseconds = factor * MillisecondsInAYearApproximate; break; default: throw new ArgumentOutOfRangeException(nameof(interval), interval, null); } SetWholeFactorIntervalAndSeconds(milliseconds, rounding); }
/// <summary> /// 小于 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <typeparam name="TValue"></typeparam> /// <param name="queries"></param> /// <param name="field">要进行判断的字段</param> /// <param name="value">要进行判断的值</param> /// <returns></returns> public static ElasticQuery <TEntity> LessThan <TEntity>(this ElasticQuery <TEntity> queries, Expression <Func <TEntity, object> > paths, Expression <Func <TEntity, object> > field, DateTime time, string timeZone = "+08:00", DateMathTimeUnit dateMathTimeUnit = DateMathTimeUnit.Day) where TEntity : class { queries.Add(t => t.Nested(n => n.Path(paths).Query(q => q.Bool(b => b.Must(m => m.DateRange(r => r.Field(field).TimeZone(timeZone).LessThan(DateMath.Anchored(time).RoundTo(dateMathTimeUnit)))))))); return(queries); }
/// <summary> /// 小于 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <typeparam name="TValue"></typeparam> /// <param name="queries"></param> /// <param name="field">要进行判断的字段</param> /// <param name="value">要进行判断的值</param> /// <returns></returns> public static ElasticQuery <TEntity> LessThan <TEntity>(this ElasticQuery <TEntity> queries, Expression <Func <TEntity, object> > field, DateTime time, string timeZone = "+08:00", DateMathTimeUnit dateMathTimeUnit = DateMathTimeUnit.Day) where TEntity : class { queries.Add(mu => mu.DateRange(r => r.Field(field).TimeZone(timeZone).LessThan(DateMath.Anchored(time).RoundTo(dateMathTimeUnit)))); return(queries); }
public DateMath RoundTo(DateMathTimeUnit round) { this.Round = round; return(this); }