protected DateTimeRange FromRelationAmountTime(string relation, int amount, string size, DateTime now) { relation = relation.ToLower(); size = size.ToLower(); if (amount < 1) throw new ArgumentException("Time amount can't be 0."); TimeSpan intervalSpan = Helper.GetTimeSpanFromName(size); if (intervalSpan != TimeSpan.Zero) { var totalSpan = TimeSpan.FromTicks(intervalSpan.Ticks * amount); switch (relation) { case "last": case "past": case "previous": return new DateTimeRange(now.Floor(intervalSpan).SafeSubtract(totalSpan), now); case "this": case "next": return new DateTimeRange(now, now.SafeAdd(totalSpan).Ceiling(intervalSpan).SubtractMilliseconds(1)); } } else if (size == "week" || size == "weeks") { switch (relation) { case "last": case "past": case "previous": return new DateTimeRange(now.SubtractWeeks(amount).StartOfDay(), now); case "this": case "next": return new DateTimeRange(now, now.AddWeeks(amount).EndOfDay()); } } else if (size == "month" || size == "months") { switch (relation) { case "last": case "past": case "previous": return new DateTimeRange(now.SubtractMonths(amount).StartOfDay(), now); case "this": case "next": return new DateTimeRange(now, now.AddMonths(amount).EndOfDay()); } } else if (size == "year" || size == "years") { switch (relation) { case "last": case "past": case "previous": return new DateTimeRange(now.SubtractYears(amount).StartOfDay(), now); case "this": case "next": return new DateTimeRange(now, now.AddYears(amount).EndOfDay()); } } return null; }
protected DateTime? FromRelationAmountTime(string relation, int amount, string size, DateTime now, bool isUpperLimit) { relation = relation.ToLower(); size = size.ToLower(); if (amount < 1) throw new ArgumentException("Time amount can't be 0."); TimeSpan intervalSpan = Helper.GetTimeSpanFromName(size); if (intervalSpan != TimeSpan.Zero) { var totalSpan = TimeSpan.FromTicks(intervalSpan.Ticks * amount); switch (relation) { case "ago": return isUpperLimit ? now.SafeSubtract(totalSpan).Ceiling(intervalSpan).SubtractMilliseconds(1) : now.SafeSubtract(totalSpan).Floor(intervalSpan); case "from now": return isUpperLimit ? now.SafeAdd(totalSpan).Ceiling(intervalSpan).SubtractMilliseconds(1) : now.SafeAdd(totalSpan).Floor(intervalSpan); } } else if (size == "week" || size == "weeks") { switch (relation) { case "ago": return isUpperLimit ? now.SubtractWeeks(amount).EndOfDay() : now.SubtractWeeks(amount).StartOfDay(); case "from now": return isUpperLimit ? now.AddWeeks(amount).EndOfDay() : now.AddWeeks(amount).StartOfDay(); } } else if (size == "month" || size == "months") { switch (relation) { case "ago": return isUpperLimit ? now.SubtractMonths(amount).EndOfDay() : now.SubtractMonths(amount).StartOfDay(); case "from now": return isUpperLimit ? now.AddMonths(amount).EndOfDay() : now.AddMonths(amount).StartOfDay(); } } else if (size == "year" || size == "years") { switch (relation) { case "ago": return isUpperLimit ? now.SubtractYears(amount).EndOfDay() : now.SubtractYears(amount).StartOfDay(); case "from now": return isUpperLimit ? now.AddYears(amount).EndOfDay() : now.AddYears(amount).StartOfDay(); } } return null; }