/// <summary> /// Returns all the days of a datetime range. /// </summary> /// <param name="dateRange">The date range.</param> /// <returns></returns> public static IEnumerable <DateTime> DaysInRange(this IDateTimeRange dateRange) { return(Enumerable.Range(0, (dateRange.TimeSpan).Days) .Select(offset => new DateTime( dateRange.StartTime.AddDays(offset).Year, dateRange.StartTime.AddDays(offset).Month, dateRange.StartTime.AddDays(offset).Day))); }
public static bool IsIn(this IDateTimeRange dateTimeRange, DateTime dateTime) { var isBeforeTo = dateTimeRange.To == DateTime.MaxValue || dateTimeRange.To > dateTime || (dateTimeRange.To.Date == dateTimeRange.To && dateTimeRange.To == dateTime); var isAfterFrom = dateTimeRange.From == DateTime.MinValue || dateTimeRange.From <= dateTime; return(isBeforeTo && isAfterFrom); }
public void UpdateDateTimes(IDateTimeRange dateTimeRange) { var(startTime, endTime) = GetNewDateTimeRange(dateTimeRange); bool isAdjusted = false; StartTime = AdjustDateTime(startTime, ref isAdjusted); EndTime = AdjustDateTime(endTime, ref isAdjusted); if (isAdjusted) { string format = "ddd dd'/'MM'/'yyyy HH:mm:ss"; throw new Exception("Datetimes have been adjusted between " + DateTimeMin.ToString(format, CultureInfo.CreateSpecificCulture("en-US")) + " and " + DateTimeMax.ToString(format, CultureInfo.CreateSpecificCulture("en-US"))); } }
/// <summary> /// Creates a datetime conditional expression for MS SQL server. /// </summary> /// <param name="dateTimeRange">DateTime range.</param> /// <param name="column">Column or datetime expression.</param> /// <returns>Conditional expression for MS SQL server.</returns> public static string GetMsSqlCondition(this IDateTimeRange dateTimeRange, string column) { var isFromDefined = dateTimeRange.From != DateTime.MinValue; var isToDefined = dateTimeRange.To != DateTime.MaxValue; var isInclusiveTo = isToDefined && dateTimeRange.To.Date != dateTimeRange.To; var fromMsSqlLiteral = dateTimeRange.From.ToMsSqlLiteral(); var toMsSqlLiteral = dateTimeRange.To.ToMsSqlLiteral(); var isFromToInclusive = isFromDefined && isToDefined && isInclusiveTo; if (isFromToInclusive) { return($"{column} BETWEEN {fromMsSqlLiteral} AND {toMsSqlLiteral}"); } var isFromToExclusive = isFromDefined && isToDefined; if (isFromToExclusive) { return($"{column} >= {fromMsSqlLiteral} AND {column} < {toMsSqlLiteral}"); } var isOnlyFrom = isFromDefined; if (isOnlyFrom) { return($"{column} >= {fromMsSqlLiteral}"); } var isOnlyTo = isToDefined; if (isOnlyTo) { return($"{column} {(isInclusiveTo ? "<=" : "<")} {toMsSqlLiteral}"); } return($"{column} IS NOT NULL"); }
/// <summary> /// Sets date range from given source. /// </summary> public static void SetFrom(this IDateTimeRange target, IDateTimeRange source) { target.StartTime = source.StartTime; target.EndTime = source.EndTime; }
/// <summary> /// Sets date range to given target. /// </summary> /// <param name="source"></param> /// <param name="target"></param> public static void SetTo(this IDateTimeRange source, IDateTimeRange target) { target.StartTime = source.StartTime; target.EndTime = source.EndTime; }
/// <summary> /// Creates a new <see cref="DateTimeRange"/> object from given <paramref name="dateTimeRange"/> object. /// 根据给定的<paramref name="dateTimeRange"/>日期时间对象,创建一个<see cref="DateTimeRange"/>对象 /// </summary> /// <param name="dateTimeRange">IDateTimeRange object 日期时间对象</param> public DateTimeRange(IDateTimeRange dateTimeRange) { StartTime = dateTimeRange.StartTime; EndTime = dateTimeRange.EndTime; }
/// <summary> /// Creates a new <see cref="DateTimeRange"/> object from given <paramref name="dateTimeRange"/> object. /// </summary> /// <param name="dateTimeRange">IDateTimeRange object</param> public DateTimeRange(IDateTimeRange dateTimeRange) { StartTime = dateTimeRange.StartTime; EndTime = dateTimeRange.EndTime; }
/// <summary> /// 初始化一个<see cref="DateTimeRange"/>类型的实例 /// </summary> /// <param name="dateTimeRange">事件范围</param> public DateTimeRange(IDateTimeRange dateTimeRange) : this(dateTimeRange.StartTime, dateTimeRange.EndTime) { }
/// <summary> /// Creates a new <see cref="DateTimeRange"/> object from given <paramref name="dateTimeRange"/> object. /// </summary> /// <param name="dateTimeRange">IDateTimeRange object</param> public DateTimeRange(IDateTimeRange dateTimeRange) { this.StartTime = dateTimeRange.StartTime; this.EndTime = dateTimeRange.EndTime; }
private Tuple <DateTime, DateTime> GetNewDateTimeRange(IDateTimeRange dateTimeRange) { string dateTimeRangeId = dateTimeRange.Id; DateTime now = DateTime.Now; DateTime today = DateTime.Now.Date; DateTime startTime = now; DateTime endTime = now; if (dateTimeRangeId == "pyear") { startTime = today.AddDays(-365); endTime = today.AddTicks(-1); } else if (dateTimeRangeId == "pmonth") { startTime = today.AddDays(-30); endTime = today.AddTicks(-1); } else if (dateTimeRangeId == "pweek") { startTime = today.AddDays(-7); endTime = today.AddTicks(-1); } else if (dateTimeRangeId == "p24h") { startTime = today.AddHours(now.Hour - 24); endTime = today.AddHours(now.Hour).AddTicks(-1); } else if (dateTimeRangeId == "n24h") { startTime = today.AddHours(now.Hour + 1); endTime = startTime.AddHours(24).AddTicks(-1); } else if (dateTimeRangeId == "n36h") { startTime = today.AddHours(now.Hour + 1); endTime = startTime.AddHours(36).AddTicks(-1); } else if (dateTimeRangeId == "n7d") { startTime = today.AddDays(1); endTime = startTime.AddDays(7).AddTicks(-1); } else if (dateTimeRangeId == "n30d") { startTime = today.AddDays(1); endTime = startTime.AddDays(30).AddTicks(-1); } else if (dateTimeRangeId == "lyear") { startTime = new DateTime(today.Year - 1, 1, 1); endTime = startTime.AddYears(1).AddTicks(-1); } else if (dateTimeRangeId == "lmonth") { DateTime lastMonth = today.AddMonths(-1); startTime = new DateTime(lastMonth.Year, lastMonth.Month, 1); endTime = startTime.AddMonths(1).AddTicks(-1); } else if (dateTimeRangeId == "yesterday") { startTime = today.AddDays(-1); endTime = startTime.AddDays(1).AddTicks(-1); } else if (dateTimeRangeId == "today") { startTime = today; endTime = startTime.AddDays(1).AddTicks(-1); } else if (dateTimeRangeId == "tomorrow") { startTime = today.AddDays(1); endTime = startTime.AddDays(1).AddTicks(-1); } else if (dateTimeRangeId == "tmonth") { startTime = new DateTime(today.Year, today.Month, 1); endTime = startTime.AddMonths(1).AddTicks(-1); } else if (dateTimeRangeId == "tyear") { startTime = new DateTime(today.Year, 1, 1); endTime = startTime.AddYears(1).AddTicks(-1); } return(new Tuple <DateTime, DateTime>(startTime, endTime)); }
public ZonedDateTimeRange(IDateTimeRange dateTimeRange, string timeZoneId) : base(dateTimeRange) { Timezone = timeZoneId; }
public static bool Overlaps <TSource>(this TSource dateTimeRange, IDateTimeRange another, OverlapsInclude overlapsInclude = OverlapsInclude.None) where TSource : IDateTimeRange { return(dateTimeRange.Overlaps(another.Begin, another.End, overlapsInclude)); }
/// <summary> /// 复制; /// </summary> /// <param name="dateRange"></param> /// <param name="dest"></param> public static void Copy(this IDateTimeRange dateRange, IDateTimeRange dest) { dest.BeginDateTime = dateRange.BeginDateTime; dest.EndDateTime = dateRange.EndDateTime; }