Is this the best way to introduce decades with TimeSpan?

I make a schedule that covers "1930-1940", "1940-1950", "1950-1960", "1960-1970", ...

I want to present this using DateTime and Timespan , but I'm not sure how to make a Timespan , and it is difficult for me to verify the correctness of the time intervals.

Is this how I should use Timespan , or is it overlapping? If it overlaps, how can I fix it?

 List<DateTime> list1 = new List<DateTime>(); List<TimeSpan> list2 = new List<TimeSpan>(); int startYearInt = 1930; int times = 0; const int intervalSize = 10; for (int i = startYearInt; i < 2020; i += intervalSize) { DateTime sYear = new DateTime(startYearInt + (intervalSize * times++), 1, 1); TimeSpan period = (sYear.AddYears(intervalSize)) - sYear; list1.Add(sYear); list2.Add(period); // <<-- Don't know if if this is correct? } 

EDIT: I have this too. And if my time period is too short or for the latter it can give some problems.

 public bool IsInsidePeriod(DateTime dt) { return dt >= FromYearDateTime && dt < FromYearDateTime.Add(periodTimeSpan); } 
+6
source share
3 answers

You are better off creating a DateRange value type than using DateTime and TimeSpan . Look here for an example. Then you can use the factory method, which gives you a range for a decade: DateRange.DecadeStartingOn(1930); . Thus, you raise the level of abstraction and understand the concepts that you have in mind in the code itself.

Your IsInsidePeriod is a simple operation for DateRange :

 public bool Includes(DateTime date) { return start <= date && date <= end; } 

(assuming both start and end are included)

Now, if you only need to deal with decades, you really don't need a full-fledged DateRange class, just this:

 class Decade { public int StartYear { get; private set; } public int EndYear { get { return StartYear + 9; } } public Decade(int startYear) { StartYear = startYear; } public bool Includes(DateTime date) { return StartYear <= date.Year && date.Year <= EndYear; } public override string ToString() { return string.Format("{0}-{1}", StartYear, EndYear + 1); } } 

Or maybe a more general YearRange .

+7
source

If all you are asking to do is solve your current problems, then the code below works, hey, I'm bored, but I would consider doing some research on DateTime ranges, comparisons (especially how to work with different TimeZones, etc. and intervals.

DateTime on MSDN

 class Program { static void Main(string[] args) { int interval = 10; DateTime isInRangeDate = DateTime.UtcNow; for (int i = 1930; i < 2020; ) { DateRange range = new DateRange(1, 1, i, interval); Console.WriteLine(string.Format("{0}: Is in range - {1}", range.ToString(), range.IsInsidePeriod(isInRangeDate))); i = range.EndDate.Year; } Console.ReadLine(); } } public class DateRange { public DateTime StartDate { get; private set; } public DateTime EndDate { get; private set; } public override string ToString() { return string.Format("{0}-{1}", this.StartDate.Year, this.EndDate.Year); } public DateRange(int day, int month, int year, int addYears) { StartDate = new DateTime(year, month, day, 0, 0, 0); EndDate = StartDate.AddYears(addYears); } public bool IsInsidePeriod(DateTime dt) { return ((dt.Date >= StartDate) && (dt.Date < EndDate)); } } 
+1
source

You can simplify the IsInPeriod method like this:

 public bool IsInsidePeriod(DateTime dateToCompare, DateTime startDate, DateTime endDate) { return startDate <= dateToCompare && dateToCompare < endDate; } 

As others have noted, TimeSpan does not buy you anything and causes the excessive complexity of the problem you described. Pay particular attention to comparison operators. You may want either end to be exclusive, not inclusive, or vice versa.

0
source

Source: https://habr.com/ru/post/910519/


All Articles