-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDateRangeValues.cpp
229 lines (195 loc) · 5.98 KB
/
DateRangeValues.cpp
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// DateRangeValues.cpp : implementation file
//
#include "stdafx.h"
#include "VarroaPop.h"
#include "DateRangeValues.h"
// CDateRangeValue
/*
The data in this class represent a list of date ranges and an associated value to apply in that range.
The public accessor functions allow retrieval of the value associated with a date range. If
the date ranges from two separate DateRangeValue items overlap, the accessor returns the value of the first one.
*/
CDateRangeValues::CDateRangeValues()
{
pItemList = new CObList();
SetEnabled(true);
}
CDateRangeValues::~CDateRangeValues()
{
ClearAll();
delete pItemList;
}
CDateRangeValues& CDateRangeValues::operator = (CDateRangeValues& DRV)
{
DRV.Copy(this);
return *this;
}
void CDateRangeValues::Copy(CDateRangeValues* pDestination)
{
if (pDestination == NULL) return;
pDestination->ClearAll();
DR_ITEM theItem;
for (int i = 0; i < GetCount(); i++)
{
GetItem(i, theItem);
pDestination->AddItem(theItem);
}
pDestination->SetEnabled(IsEnabled());
}
int CDateRangeValues::GetCount()
{
if (!pItemList) return 0;
else return pItemList->GetCount();
}
bool CDateRangeValues::GetItem(int Index, DR_ITEM& theItem)
{
bool Success = false;
if ((Index >=0) && (Index < GetCount()))
{
DR_ITEM* pItem = (DR_ITEM*)pItemList->GetAt(pItemList->FindIndex(Index));
theItem.EndTime = pItem->EndTime;
theItem.StartTime = pItem->StartTime;
theItem.Value = pItem->Value;
Success = true;
}
return Success;
}
DR_ITEM* CDateRangeValues::GetItemPtr(int Index)
{
if (Index >= pItemList->GetCount()) return NULL;
else return (DR_ITEM*)pItemList->GetAt(pItemList->FindIndex(Index));
}
bool CDateRangeValues::GetActiveItem(COleDateTime theDate, DR_ITEM& theItem)
{
// theItem is filled with the contents of the first list item where theDate is between the DR_ITEM start date and end date,
// and the function return value is TRUE. If theDate does not fall between the date range in any
// list item, the return value is FALSE.
bool Success = false;
DR_ITEM* pListItem;
POSITION pos = pItemList->GetHeadPosition();
while (pos != NULL)
{
pListItem = (DR_ITEM*)pItemList->GetNext(pos);
COleDateTimeSpan TimeSinceStart = (COleDateTimeSpan)(theDate - pListItem->StartTime);
COleDateTimeSpan TimeTillEnd = (COleDateTimeSpan)(pListItem->EndTime - theDate);
int month = theDate.GetMonth();
int day = theDate.GetDay();
int year = theDate.GetYear();
if ((month >= 3) && (day >= 2) && (year == 1999))
{
int i = 0;
}
if ((TimeSinceStart.GetDays() > 0) && (TimeTillEnd.GetDays() > 0))
{
theItem.EndTime = pListItem->EndTime;
theItem.StartTime = pListItem->StartTime;
theItem.Value = pListItem->Value;
Success = true;
break;
}
}
return Success;
}
bool CDateRangeValues::GetActiveValue(COleDateTime theDate, double& theValue)
{
DR_ITEM theItem;
bool Found = GetActiveItem(theDate,theItem);
if (Found) theValue = theItem.Value;
return Found;
}
void CDateRangeValues::AddItem(DR_ITEM& theItem)
{
DR_ITEM* pItem = new DR_ITEM();
pItem->EndTime = theItem.EndTime;
pItem->StartTime = theItem.StartTime;
pItem->Value = theItem.Value;
pItemList->AddTail((CObject*)pItem);
}
void CDateRangeValues::AddItem(COleDateTime theStartTime, COleDateTime theEndTime, double theValue)
{
DR_ITEM Item;
Item.EndTime = theEndTime;
Item.StartTime = theStartTime;
Item.Value = theValue;
AddItem(Item);
}
void CDateRangeValues::AddItem(CString theStartTimeStg, CString theEndTimeStg, double theValue)
{
DR_ITEM Item;
// If the date strings are valid, the item is added.
if ((Item.EndTime.ParseDateTime(theEndTimeStg) && Item.StartTime.ParseDateTime(theStartTimeStg)))
{
Item.Value = theValue;
AddItem(Item);
}
}
void CDateRangeValues::DeleteItem(int Index)
{
if (Index <0) return;
POSITION pos = pItemList->FindIndex(Index);
if (pos != NULL) // Validate that Index is valid
{
delete(DR_ITEM*)(pItemList->GetAt(pos)); // Deletes the item
pItemList->RemoveAt(pos); // Removed item pointer from list
}
}
void CDateRangeValues::ClearAll()
{
DR_ITEM* pItem;
if (pItemList != NULL)
{
while (pItemList->GetCount() > 0)
{
pItem = (DR_ITEM*)pItemList->RemoveTail();
delete(pItem);
}
}
}
void CDateRangeValues::Serialize(CArchive& ar, int Version)
{
FileFormatVersion = Version;
if (ar.IsStoring())
{
DR_ITEM* pItem;
int itemcount = pItemList->GetCount();
ar << itemcount; // Store value, even if 0
if (FileFormatVersion >= 12)
{
int fv = IsEnabled()?1:0;
ar << fv;
}
if (itemcount>0)
{
POSITION pos = pItemList->GetHeadPosition();
while (pos != NULL)
{
pItem = (DR_ITEM*)pItemList->GetNext(pos);
ar << pItem->StartTime;
ar << pItem->EndTime;
ar << pItem->Value;
}
}
}
else
{
ClearAll();
int itemcount;
int iEnabled;
DR_ITEM theItem;
ar >> itemcount;
if (FileFormatVersion >= 12)
{
ar >> iEnabled;
if (iEnabled == 1) SetEnabled(true);
else SetEnabled(false);
}
for (int i = 0; i < itemcount; i++)
{
ar >> theItem.StartTime;
ar >> theItem.EndTime;
ar >> theItem.Value;
AddItem(theItem);
}
}
}
// CDateRangeValue member functions