forked from Giannoudis/TimePeriodLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MonthTimeRange.cs
179 lines (156 loc) · 5.98 KB
/
MonthTimeRange.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// -- FILE ------------------------------------------------------------------
// name : MonthTimeRange.cs
// project : Itenso Time Period
// created : Jani Giannoudis - 2011.02.18
// language : C# 4.0
// environment: .NET 2.0
// copyright : (c) 2011-2012 by Itenso GmbH, Switzerland
// --------------------------------------------------------------------------
using System;
namespace Itenso.TimePeriod
{
// ------------------------------------------------------------------------
public abstract class MonthTimeRange : CalendarTimeRange
{
// ----------------------------------------------------------------------
protected MonthTimeRange( int startYear, YearMonth startMonth, int monthCounth ) :
this( startYear, startMonth, monthCounth, new TimeCalendar() )
{
} // MonthTimeRange
// ----------------------------------------------------------------------
protected MonthTimeRange( int startYear, YearMonth startMonth, int monthCount, ITimeCalendar calendar ) :
base( GetPeriodOf( calendar, startYear, startMonth, monthCount ), calendar )
{
this.startYear = startYear;
this.startMonth = startMonth;
this.monthCount = monthCount;
TimeTool.AddMonth( startYear, startMonth, monthCount - 1, out endYear, out endMonth );
} // MonthTimeRange
// ----------------------------------------------------------------------
public int StartYear
{
get { return Calendar.GetYear( startYear, (int)startMonth ); }
} // StartYear
// ----------------------------------------------------------------------
public int EndYear
{
get { return Calendar.GetYear( endYear, (int)endMonth ); }
} // EndYear
// ----------------------------------------------------------------------
public YearMonth StartMonth
{
get { return startMonth; }
} // StartMonth
// ----------------------------------------------------------------------
public YearMonth EndMonth
{
get { return endMonth; }
} // EndMonth
// ----------------------------------------------------------------------
public int MonthCount
{
get { return monthCount; }
} // MonthCount
// ----------------------------------------------------------------------
public string StartMonthName
{
get { return Calendar.GetMonthName( (int)StartMonth ); }
} // StartMonthName
// ----------------------------------------------------------------------
public string StartMonthOfYearName
{
get { return Calendar.GetMonthOfYearName( StartYear, (int)StartMonth ); }
} // StartMonthOfYearName
// ----------------------------------------------------------------------
public string EndMonthName
{
get { return Calendar.GetMonthName( (int)EndMonth ); }
} // EndMonthName
// ----------------------------------------------------------------------
public string EndMonthOfYearName
{
get { return Calendar.GetMonthOfYearName( EndYear, (int)EndMonth ); }
} // EndMonthOfYearName
// ----------------------------------------------------------------------
public ITimePeriodCollection GetDays()
{
TimePeriodCollection days = new TimePeriodCollection();
DateTime startDate = GetStartOfMonth( Calendar, startYear, startMonth );
for ( int month = 0; month < monthCount; month++ )
{
DateTime monthStart = startDate.AddMonths( month );
int daysOfMonth = TimeTool.GetDaysInMonth( monthStart.Year, monthStart.Month );
for ( int day = 0; day < daysOfMonth; day++ )
{
days.Add( new Day( monthStart.AddDays( day ), Calendar ) );
}
}
return days;
} // GetDays
// ----------------------------------------------------------------------
protected override bool IsEqual( object obj )
{
return base.IsEqual( obj ) && HasSameData( obj as MonthTimeRange );
} // IsEqual
// ----------------------------------------------------------------------
private bool HasSameData( MonthTimeRange comp )
{
return
startYear == comp.startYear &&
startMonth == comp.startMonth &&
monthCount == comp.monthCount &&
endYear == comp.endYear &&
endMonth == comp.endMonth;
} // HasSameData
// ----------------------------------------------------------------------
protected override int ComputeHashCode()
{
return HashTool.ComputeHashCode( base.ComputeHashCode(), startYear, startMonth, monthCount, endYear, endMonth );
} // ComputeHashCode
// ----------------------------------------------------------------------
private static DateTime GetStartOfMonth( ITimeCalendar calendar, int year, YearMonth month )
{
DateTime startOfMonth;
if ( calendar.YearType == YearType.FiscalYear )
{
startOfMonth = FiscalCalendarTool.GetStartOfMonth(
year, month, calendar.YearBaseMonth, calendar.FiscalFirstDayOfYear, calendar.FiscalYearAlignment, calendar.FiscalQuarterGrouping );
}
else
{
startOfMonth = new DateTime( year, (int)month, 1 );
}
return startOfMonth;
} // GetStartOfMonth
// ----------------------------------------------------------------------
private static TimeRange GetPeriodOf( ITimeCalendar calendar, int startYear, YearMonth startMonth, int monthCount )
{
if ( monthCount < 1 )
{
throw new ArgumentOutOfRangeException( "monthCount" );
}
DateTime start = GetStartOfMonth( calendar, startYear, startMonth );
DateTime end;
if ( calendar.YearType == YearType.FiscalYear )
{
int endYear;
YearMonth endMonth;
TimeTool.AddMonth( startYear, startMonth, monthCount, out endYear, out endMonth );
end = GetStartOfMonth( calendar, endYear, endMonth );
}
else
{
end = start.AddMonths( monthCount );
}
return new TimeRange( start, end );
} // GetPeriodOf
// ----------------------------------------------------------------------
// members
private readonly int startYear;
private readonly YearMonth startMonth;
private readonly int monthCount;
private readonly int endYear; // cache
private readonly YearMonth endMonth; // cache
} // class MonthTimeRange
} // namespace Itenso.TimePeriod
// -- EOF -------------------------------------------------------------------