forked from Dromaeosaur/MerchandiseCalendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MerchCalendarDates.cs
118 lines (103 loc) · 4.25 KB
/
MerchCalendarDates.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
namespace MerchandiseCalendar
{
/// <summary>
/// The date range for the merchandise calendar year, consisting of a start date and an end date.
/// </summary>
public class DateRange
{
/// <summary>
/// The start date for the date range.
/// </summary>
public DateTime StartDate { get; set; }
/// <summary>
/// The end date for the date range.
/// </summary>
public DateTime EndDate { get; set; }
}
/// <summary>
/// Retrieves information about the merchandise year.
/// </summary>
public class MerchYear
{
/* Set up so that the only thing that needs a public setter is the year. All other attributes
* are calculated from the year that's set when object is instantiated. */
private int _year;
/// <summary>
/// Retrieves information about the merchandise year.
/// </summary>
/// <param name="year">
/// The year you wish to get the information for.
/// </param>
public MerchYear(int year)
{
_year = year;
SetDateRange(_year);
}
/// <summary>
/// Retrieves information about the merchandise year.
/// </summary>
public MerchYear() { }
/* The Merchandise calendar year is a 52 week year starting in February. It's calculated by
* taking the first full Sunday to Saturday week in February with less than four days from
* January in it. If, after the 52 weeks have been calculated, there are still more than three
* days from January of the next calenday year left, an extra, 53rd week is added to the
* merchandise calendar. This happens every five or six years to account for the one day
* discrepancy between the merchandise calendar year and the normal calendar year. */
private void SetDateRange(int year)
{
// Merchandise calendar starts in February.
var firstDayOfFeb = new DateTime(year, 2, 1);
// Integer representing the day of the week for the first of February.
var firstFebDayOfWeek = (int)firstDayOfFeb.DayOfWeek;
var nextYear = year + 1;
/* Subtract days until you get to Sunday, which is the start of a merchandise calendar week.
* This is your start date for the year, except when... */
var startDate = firstDayOfFeb.AddDays((0 - firstFebDayOfWeek));
/* ...February 1st falls on Thu - Sat, meaning there are more than three days from January in the
* first week, also meaning the year before was a 53 week year. In that case, add a week
* to the start date. */
if (firstFebDayOfWeek > 3)
startDate = startDate.AddDays(7);
/* Add 52 weeks' worth of days (364) to the start date to get the end date, taking into
* account the start day constitutes one of them (-1). */
var endDate = startDate.AddDays(363);
/* If the end date is January 27th of next year or earlier, there will be more than 3 January
* days at the beginning of the next year and another week needs to be added to account for this. */
if (endDate <= new DateTime(nextYear, 1, 27))
{
endDate = endDate.AddDays(7);
ExtraWeek = true;
}
else
{
ExtraWeek = false;
}
DateRange = new DateRange
{
StartDate = startDate,
EndDate = endDate.Add(new TimeSpan(0, 23, 59, 59, 999))
};
}
/// <summary>
/// The merchandise year.
/// </summary>
public int Year
{
get { return _year; }
set
{
SetDateRange(value);
_year = value;
}
}
/// <summary>
/// The date range for the merchandise year.
/// </summary>
public DateRange DateRange { get; private set; }
/// <summary>
/// Indicates whether or not the merchandise year contains 52 or 53 weeks.
/// </summary>
public bool ExtraWeek { get; private set; }
}
}