forked from Giannoudis/TimePeriodLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quarter.cs
157 lines (135 loc) · 4.89 KB
/
Quarter.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
// -- FILE ------------------------------------------------------------------
// name : Quarter.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 sealed class Quarter : QuarterTimeRange
{
// ----------------------------------------------------------------------
public Quarter() :
this( new TimeCalendar() )
{
} // Quarter
// ----------------------------------------------------------------------
public Quarter( ITimeCalendar calendar ) :
this( ClockProxy.Clock.Now, calendar )
{
} // Quarter
// ----------------------------------------------------------------------
public Quarter( DateTime moment ) :
this( moment, new TimeCalendar() )
{
} // Quarter
// ----------------------------------------------------------------------
public Quarter( DateTime moment, ITimeCalendar calendar ) :
this( TimeTool.GetYearOf( calendar.YearBaseMonth, calendar.GetYear( moment ), calendar.GetMonth( moment ) ),
TimeTool.GetQuarterOfMonth( calendar.YearBaseMonth, (YearMonth)moment.Month ), calendar )
{
} // Quarter
// ----------------------------------------------------------------------
public Quarter( int baseYear, YearQuarter yearQuarter ) :
this( baseYear, yearQuarter, new TimeCalendar() )
{
} // Quarter
// ----------------------------------------------------------------------
public Quarter( int baseYear, YearQuarter yearQuarter, ITimeCalendar calendar ) :
base( baseYear, yearQuarter, 1, calendar )
{
} // Quarter
// ----------------------------------------------------------------------
public int Year
{
get
{
int year;
YearMonth month;
int monthCount = ( (int)StartQuarter - 1 ) * TimeSpec.MonthsPerQuarter;
TimeTool.AddMonth( BaseYear, Calendar.YearBaseMonth, monthCount, out year, out month );
return Calendar.GetYear( year, (int)month );
}
} // Year
// ----------------------------------------------------------------------
public YearMonth StartMonth
{
get
{
int year;
YearMonth month;
int monthCount = ( (int)StartQuarter - 1 ) * TimeSpec.MonthsPerQuarter;
TimeTool.AddMonth( BaseYear, Calendar.YearBaseMonth, monthCount, out year, out month );
return month;
}
} // YearHalfyear
// ----------------------------------------------------------------------
public YearQuarter YearQuarter
{
get { return StartQuarter; }
} // YearQuarter
// ----------------------------------------------------------------------
public string QuarterName
{
get { return StartQuarterName; }
} // QuarterName
// ----------------------------------------------------------------------
public string QuarterOfYearName
{
get { return StartQuarterOfYearName; }
} // QuarterOfYearName
// ----------------------------------------------------------------------
public bool IsCalendarQuarter
{
get { return ( (int)YearBaseMonth - 1 ) % TimeSpec.MonthsPerQuarter == 0; }
} // IsCalendarQuarter
// ----------------------------------------------------------------------
public bool MultipleCalendarYears
{
get
{
if ( IsCalendarQuarter )
{
return false;
}
int startYear;
YearMonth month;
int monthCount = ( (int)StartQuarter - 1 ) * TimeSpec.MonthsPerQuarter;
TimeTool.AddMonth( BaseYear, YearBaseMonth, monthCount, out startYear, out month );
int endYear;
monthCount += TimeSpec.MonthsPerQuarter;
TimeTool.AddMonth( BaseYear, YearBaseMonth, monthCount, out endYear, out month );
return startYear != endYear;
}
} // IsCalendarHalfyear
// ----------------------------------------------------------------------
public Quarter GetPreviousQuarter()
{
return AddQuarters( -1 );
} // GetPreviousQuarter
// ----------------------------------------------------------------------
public Quarter GetNextQuarter()
{
return AddQuarters( 1 );
} // GetNextQuarter
// ----------------------------------------------------------------------
public Quarter AddQuarters( int count )
{
int year;
YearQuarter quarter;
TimeTool.AddQuarter( BaseYear, StartQuarter, count, out year, out quarter );
return new Quarter( year, quarter, Calendar );
} // AddQuarters
// ----------------------------------------------------------------------
protected override string Format( ITimeFormatter formatter )
{
return formatter.GetCalendarPeriod( QuarterOfYearName,
formatter.GetShortDate( Start ), formatter.GetShortDate( End ), Duration );
} // Format
} // class Quarter
} // namespace Itenso.TimePeriod
// -- EOF -------------------------------------------------------------------