Menu

[r320]: / GraphWeather / version-3.0.0 / Core / MonthStats.cpp  Maximize  Restore  History

Download this file

689 lines (607 with data), 21.5 kB

  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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
/*
###################################################################################
# This file is part of GraphWeather.
# Latest version is available on : http://guilmard.free.fr
# Copyright (C) 2006 Antoine Guilmard - guilmard@free.fr
#
# GraphWeather is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# GraphWeather is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with GraphWeather; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
###################################################################################
*/
#include "MonthStats.h"
#include "DayStats.h"
#include "GraphicContext.h"
//////////////////////////////////////////////////////////////////////////
// Constructors
//////////////////////////////////////////////////////////////////////////
CMonthStats::CMonthStats(CStatsContext* pStatsContext, int Year, int Month, CBaseStats* pParent/*=NULL*/)
:CBaseStats(pStatsContext, pParent)
{
m_StatsType = MONTH_STATS;
m_XMLDateFormat[0] = "%a %d - %H:%M";
m_XMLDateFormat[1] = "%a %d";
m_Year = Year;
m_Month = Month;
DaysOfRain002 = 0;
DaysOfRain020 = 0;
DaysOfRain200 = 0;
MaxPos32 = 0;
Max0 = 0;
Min0 = 0;
MinNeg18 = 0;
m_StartPeriod = GetStartPeriod(m_Year, m_Month);
m_EndPeriod = GetEndPeriod(m_Year, m_Month);
ResetStats();
}
//////////////////////////////////////////////////////////////////////////
// Destructor
//////////////////////////////////////////////////////////////////////////
CMonthStats::~CMonthStats(void)
{
map<int,CBaseStats*>::const_iterator it=m_SubTimeStats.begin();
for (/*empty*/; it!=m_SubTimeStats.end(); it++)
delete it->second;
}
//////////////////////////////////////////////////////////////////////////
// Method: GetStatsPath
// FullName: CMonthStats::GetStatsPath
// Access: public
//
//! \param string& StatsPath
//
//! \return void
//
//! \brief
//
//////////////////////////////////////////////////////////////////////////
void CMonthStats::GetStatsPath(string& StatsPath)
{
stringstream ss;
ss << m_Year << "\\" ;
ss << setw(2) << setfill('0') << m_Month << "\\" ;
ss << m_Year << "_" ;
ss << setw(2) << setfill('0') << m_Month;
StatsPath = ss.str();
}
//////////////////////////////////////////////////////////////////////////
// Method: UpdateStatsTree
// FullName: CMonthStats::UpdateStatsTree
// Access: virtual public
//
//! \param int Year
//! \param int Month
//! \param int Day
//
//! \return void
//
//! \brief
//
//////////////////////////////////////////////////////////////////////////
void CMonthStats::UpdateStatsTree(int Year, int Month, int Day)
{
ASSERT(m_Month == Month);
if( m_SubTimeStats.find(Day) == m_SubTimeStats.end())
m_SubTimeStats.insert(pair<int,CBaseStats*>(Day, new CDayStats(m_pStatsContext, m_Year, m_Month, Day, this)));
m_SubTimeStats[Day]->UpdateStatsTree(Year, Month, Day);
m_DatabaseUpdateNeeded = TRUE;
}
//////////////////////////////////////////////////////////////////////////
// Method: UpdateDatabase
// FullName: CMonthStats::UpdateDatabase
// Access: public
//
//
//! \return void
//
//! \brief
//
//////////////////////////////////////////////////////////////////////////
void CMonthStats::UpdateDatabase()
{
if (m_DatabaseUpdateNeeded)
{
CBaseStats::UpdateDatabase();
// Override TMinMean and TMaxMean actually calculated for days not decade
int NumOfDays = 0;
TMaxMean = 0.0f;
TMinMean = 0.0f;
map<int,CBaseStats*>::iterator itday = m_SubTimeStats.begin();
for (; itday != m_SubTimeStats.end(); itday++)
{
CBaseStats* pDaySats = itday->second;
TMaxMean += pDaySats->ProbesStats[outdoor_temperature].Max;
TMinMean += pDaySats->ProbesStats[outdoor_temperature].Min;
NumOfDays++;
}
if (NumOfDays > 0)
{
TMaxMean /= (float)NumOfDays;
TMinMean /= (float)NumOfDays;
}
}
}
//////////////////////////////////////////////////////////////////////////
// Method: UpdateContent
// FullName: CMonthStats::UpdateContent
// Access: public
//
//
//! \return void
//
//! \brief
//
//////////////////////////////////////////////////////////////////////////
void CMonthStats::UpdateContent()
{
if (m_DatabaseUpdateNeeded)
{
CBaseStats::UpdateContent();
// Update XML if required
if (m_pStatsContext->m_GenerateXML)
{
UpdateFiles();
try
{
UpdateGraphics();
}
catch (...)
{
}
}
// Update NOAA if required
if (m_pStatsContext->m_GenerateNOAA)
{
UpdateNOAA();
}
m_DatabaseUpdateNeeded = FALSE;
}
}
//////////////////////////////////////////////////////////////////////////
// Method: UpdateFiles
// FullName: CMonthStats::UpdateFiles
// Access: public
//
//! \param DWORD Type
//
//! \return void
//
//! \brief
//
//////////////////////////////////////////////////////////////////////////
void CMonthStats::UpdateFiles()
{
string XslPath = "./../../month.xsl";
string Extension;
stringstream ss;
Extension=".xml";
ss << "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>" << endl;
ss << "<?xml-stylesheet href=\""<< XslPath << "\" type=\"text/xsl\"?>" << endl;
ss << "<statistics year=\"" << m_Year ;
ss << "\" month=\"" << setw(2) << setfill('0') << m_Month ;
tm Date;
memset(&Date, 0, sizeof(tm));
Date.tm_isdst = -1;
Date.tm_mday = 1;
Date.tm_mon = m_Month - 1;
Date.tm_year = m_Year - 1900;
mktime(&Date);
char StrMonth[32];
strftime(StrMonth, 32, "%B", &Date);
ss << "\" str-month=\"" << StrMonth << "\">" << endl;
// Number of days in month
int DaysInMonth = 31;
if (m_Month == 4 || m_Month == 6 || m_Month == 9 || m_Month == 11)
{
DaysInMonth = 30;
}
else if (m_Month == 2)
{
DaysInMonth = 28;
if ((m_Year % 4) == 0) // Valid from 1901 to 2099
DaysInMonth = 29;
}
// Map of available days in month
ss << "\t<map";
for (int i = 1; i <= DaysInMonth ; i++)
{
if (m_SubTimeStats.find(i) != m_SubTimeStats.end())
ss << " day-" << setw(2) << setfill('0') << i << "=\"1\"";
else
ss << " day-" << setw(2) << setfill('0') << i << "=\"0\"";
}
ss << "/>" << endl;
// Probes
AppendStatsString(ss, 1);
ss << "</statistics>" << endl;
// Write the stats file
string StatsString = ss.str();
string StatsPath;
GetStatsPath(StatsPath);
StatsPath += Extension;
WriteStatsFile(StatsPath, StatsString);
}
//////////////////////////////////////////////////////////////////////////
// Method: UpdateNOAA
// FullName: CMonthStats::UpdateNOAA
// Access: protected
//
//
//! \return void
//
//! \brief
//
//////////////////////////////////////////////////////////////////////////
void CMonthStats::UpdateNOAA()
{
SetLocalEn();
string BasePath;
GetContextOutputPath(BasePath);
if (!BasePath.empty())
{
string StatsPath;
GetStatsPath(StatsPath);
string FullPathName = BasePath;
FullPathName += StatsPath;
FullPathName += "_NOAA.txt";
string::size_type Index=FullPathName.rfind("\\");
string Directory(FullPathName.begin(), FullPathName.begin() + Index + 1);
CreateDir(Directory.c_str());
ofstream Output;
Output.open(FullPathName.c_str());
if(!Output.fail())
{
time_t Time = GetStartPeriod(m_Year, m_Month);
tm TimeInfo = *localtime(&Time);
char Buffer[64];
char Line[1024];
strftime(Buffer, 64, "%B %y", &TimeInfo);
// Number of days in month
int DaysInMonth = 31;
if (m_Month == 4 || m_Month == 6 || m_Month == 9 || m_Month == 11)
{
DaysInMonth = 30;
}
else if (m_Month == 2)
{
DaysInMonth = 28;
if ((m_Year % 4) == 0) // Valid from 1901 to 2099
DaysInMonth = 29;
}
///////////////////////////////////////////////////////////////////////////////////////
// Title
Output << " MONTHLY CLIMATOLOGICAL SUMMARY for " << Buffer << endl;
Output << endl;
///////////////////////////////////////////////////////////////////////////////////////
// Location
sprintf(Line, "NAME: %s CITY: %s STATE: %s",
m_pStatsContext->m_Station.c_str(),
m_pStatsContext->m_Country.c_str(),
m_pStatsContext->m_State.c_str());
Output << Line << endl;
// Latitude
float LatDegrees = fabs(m_pStatsContext->m_Latitude);
float LatMinutes = (LatDegrees - floorf(LatDegrees)) * 60.0f;
float LatSeconds = (LatMinutes - floorf(LatMinutes)) * 60.0f;
char LatDirection = 'N';
if (m_pStatsContext->m_Latitude < 0)
LatDirection = 'S';
// Longitude
float LonDegrees = fabs(m_pStatsContext->m_Longitude);
float LonMinutes = (LonDegrees - floorf(LonDegrees)) * 60.0f;
float LonSeconds = (LonMinutes - floorf(LonMinutes)) * 60.0f;
char LonDirection = 'W';
if (m_pStatsContext->m_Longitude < 0)
LonDirection = 'E';
// Complete line
sprintf(Line, "ELEV: %5d m LAT: %3d° %02d' %02d\" %c LONG: %3d° %02d' %02d\" %c",
(int)m_pStatsContext->m_Altitude,
(int)floor(LatDegrees),
(int)floor(LatMinutes),
(int)floor(LatSeconds),
LatDirection,
(int)floor(LonDegrees),
(int)floor(LonMinutes),
(int)floor(LonSeconds),
LonDirection);
Output << Line << endl;
///////////////////////////////////////////////////////////////////////////////////////
// Data header
string Legend = " TEMPERATURE ($Unit[Temperature]), RAIN ($Unit[Rainfall]), WIND SPEED ($Unit[Speed])";
m_StringManager.Parse(m_pStatsContext, m_pStatsContext, &Legend);
Output << endl << Legend.c_str() << endl << endl;
Output << " HEAT COOL AVG" << endl;
Output << " MEAN DEG DEG WIND DOM" << endl;
Output << "DAY TEMP HIGH TIME LOW TIME DAYS DAYS RAIN SPEED HIGH TIME DIR" << endl;
Output << "------------------------------------------------------------------------------------" << endl;
///////////////////////////////////////////////////////////////////////////////////////
// Data table
float TemperaturePos32 = (m_pStatsContext->m_Units & C_TEMPERATURE_MASK) == C_UNIT_CELSIUS ? 32.0f : 89.6f;
float Temperature0 = (m_pStatsContext->m_Units & C_TEMPERATURE_MASK) == C_UNIT_CELSIUS ? 0.0f : 32.0f;
float TemperatureNeg18 = (m_pStatsContext->m_Units & C_TEMPERATURE_MASK) == C_UNIT_CELSIUS ? -18.0f : -0.4f;
float TBaseHeatDegreeDays = (m_pStatsContext->m_Units & C_TEMPERATURE_MASK) == C_UNIT_CELSIUS ? m_pStatsContext->m_TBaseHeatDegreeDays : Units::C2F(m_pStatsContext->m_TBaseHeatDegreeDays);
float TBaseCoolDegreeDays = (m_pStatsContext->m_Units & C_TEMPERATURE_MASK) == C_UNIT_CELSIUS ? m_pStatsContext->m_TBaseCoolDegreeDays : Units::C2F(m_pStatsContext->m_TBaseCoolDegreeDays);
float Rainfall002 = (m_pStatsContext->m_Units & C_RAINFALL_MASK) == C_UNIT_MM ? 0.2f : 0.0787f;
unsigned long MaxRainfallDate = 0;
float MaxRainfall = 0;
string WindDirection;
float WindSpeedMax;
// Reset counters
DaysOfRain002 = 0;
DaysOfRain020 = 0;
DaysOfRain200 = 0;
MaxPos32 = 0;
Max0 = 0;
Min0 = 0;
MinNeg18 = 0;
// Loop days
for (int i=1; i <= DaysInMonth; i++)
{
// Print day
sprintf(Line, "%02d", i);
Output << Line;
// Find if the day data exists
map<int,CBaseStats*>::iterator itday = m_SubTimeStats.find(i);
if (itday == m_SubTimeStats.end())
{
Output << endl;
continue;
}
// Format data line
CDayStats* pDayStats = reinterpret_cast<CDayStats*>(itday->second);
char MaxTempTime[6];
Time = (time_t)pDayStats->ProbesStats[outdoor_temperature].MaxDate;
tm TimeInfo = *localtime(&Time);
strftime(MaxTempTime, 6, "%H:%M", &TimeInfo);
char MinTempTime[6];
Time = (time_t)pDayStats->ProbesStats[outdoor_temperature].MinDate;
TimeInfo = *localtime(&Time);
strftime(MinTempTime, 6, "%H:%M", &TimeInfo);
char MaxWindTime[6];
if (pDayStats->ProbesStats[wind_gust].Max != -FLT_MAX)
{
WindSpeedMax = pDayStats->ProbesStats[wind_gust].Max;
Time = (time_t)pDayStats->ProbesStats[wind_gust].MaxDate;
}
else
{
WindSpeedMax = pDayStats->ProbesStats[wind_speed].Max;
Time = (time_t)pDayStats->ProbesStats[wind_speed].MaxDate;
}
TimeInfo = *localtime(&Time);
strftime(MaxWindTime, 6, "%H:%M", &TimeInfo);
WindDirection = FormatWindDirection(pDayStats->ProbesStats[wind_direction].TrueMean, TRUE);
sprintf(Line, " %5.1f %5.1f %s %5.1f %s %5.1f %5.1f %5.1f %5.1f %5.1f %s %s",
pDayStats->ProbesStats[outdoor_temperature].TrueMean,
pDayStats->ProbesStats[outdoor_temperature].Max,
MaxTempTime,
pDayStats->ProbesStats[outdoor_temperature].Min,
MinTempTime,
pDayStats->HeatDegreeDays,
pDayStats->CoolDegreeDays,
pDayStats->TotalRainfall,
pDayStats->ProbesStats[wind_speed].TrueMean,
WindSpeedMax,
MaxWindTime,
WindDirection.c_str());
Output << Line << endl;
if (pDayStats->ProbesStats[outdoor_temperature].Max >= TemperaturePos32)
MaxPos32++;
if (pDayStats->ProbesStats[outdoor_temperature].Max <= Temperature0)
Max0++;
if (pDayStats->ProbesStats[outdoor_temperature].Min <= Temperature0)
Min0++;
if (pDayStats->ProbesStats[outdoor_temperature].Min <= TemperatureNeg18)
MinNeg18++;
if (pDayStats->TotalRainfall > Rainfall002)
DaysOfRain002++;
if (pDayStats->TotalRainfall > (10 * Rainfall002))
DaysOfRain020++;
if (pDayStats->TotalRainfall > (100 * Rainfall002))
DaysOfRain200++;
}
///////////////////////////////////////////////////////////////////////////////////////
// Data footer
Output << "-------------------------------------------------------------------------------------" << endl;
Time = (time_t)ProbesStats[outdoor_temperature].MaxDate;
TimeInfo = *localtime(&Time);
int MaxTempDay = TimeInfo.tm_mday;
Time = (time_t)ProbesStats[outdoor_temperature].MinDate;
TimeInfo = *localtime(&Time);
int MinTempDay = TimeInfo.tm_mday;
if (ProbesStats[wind_gust].Max != -FLT_MAX)
{
WindSpeedMax = ProbesStats[wind_gust].Max;
Time = (time_t)ProbesStats[wind_gust].MaxDate;
}
else
{
WindSpeedMax = ProbesStats[wind_speed].Max;
Time = (time_t)ProbesStats[wind_speed].MaxDate;
}
TimeInfo = *localtime(&Time);
int MaxWindDay = TimeInfo.tm_mday;
WindDirection = FormatWindDirection(ProbesStats[wind_direction].TrueMean, TRUE);
sprintf(Line, " %5.1f %5.1f %2d %5.1f %2d %5.1f %5.1f %5.1f %5.1f %5.1f %2d %s",
ProbesStats[outdoor_temperature].TrueMean,
ProbesStats[outdoor_temperature].Max,
MaxTempDay,
ProbesStats[outdoor_temperature].Min,
MinTempDay,
HeatDegreeDays,
CoolDegreeDays,
TotalRainfall,
ProbesStats[wind_speed].TrueMean,
WindSpeedMax,
MaxWindDay,
WindDirection.c_str());
Output << Line << endl << endl;
///////////////////////////////////////////////////////////////////////////////////////
// Supplemental data
sprintf(Line, "Max >= %5.1f: %2d", TemperaturePos32, MaxPos32);
Output << Line << endl;
sprintf(Line, "Max <= %5.1f: %2d", Temperature0, Max0);
Output << Line << endl;
sprintf(Line, "Min <= %5.1f: %2d", Temperature0, Min0);
Output << Line << endl;
sprintf(Line, "Min <= %5.1f: %2d", TemperatureNeg18, MinNeg18);
Output << Line << endl;
char MaxRainfallDayTime[9];
Time = (time_t)MaxRainfallDayDate;
TimeInfo = *localtime(&Time);
strftime(MaxRainfallDayTime, 9, "%d/%m/%y", &TimeInfo);
if (MaxRainfallDay > 0.0f)
sprintf(Line, "Max Rain: %.2f ON %s", MaxRainfallDay, MaxRainfallDayTime);
else
sprintf(Line, "Max Rain: 0.0");
Output << Line << endl;
sprintf(Line, "Days of Rain: %d (> .2 mm) %d (> 2 mm) %d (> 20 mm)", DaysOfRain002, DaysOfRain020, DaysOfRain200);
Output << Line << endl;
sprintf(Line, "Heat Base: %.1f Cool Base: %.1f Method: Integration", TBaseHeatDegreeDays, TBaseCoolDegreeDays);
Output << Line << endl;
Output.close();
}
}
RestoreLocal();
}
//////////////////////////////////////////////////////////////////////////
// Method: UpdateGraphics
// FullName: CMonthStats::UpdateGraphics
// Access: protected
//
//
//! \return void
//
//! \brief
//
//////////////////////////////////////////////////////////////////////////
void CMonthStats::UpdateGraphics()
{
CGraphicContext* pGraphicContext = m_pStatsContext->GetGraphicContext();
if (pGraphicContext != NULL)
{
// Set period in context
pGraphicContext->Control(C_PERIOD, C_START_PERIOD, &m_StartPeriod);
pGraphicContext->Control(C_PERIOD, C_END_PERIOD, &m_EndPeriod);
// Draw graphs
for (int k=1; k<100; k++)
{
stringstream ss;
ss << "graph-month-" << k << ".xml";
string Stylesheet = m_pStatsContext->GetOutputPath();
Stylesheet += ss.str();
ifstream File;
File.open(Stylesheet.c_str());
if(File.is_open())
{
File.close();
pGraphicContext->Control(C_STYLESHEET, C_NULL, (void*)Stylesheet.c_str());
pGraphicContext->Process(C_SAVE_GRAPHIC_IN_MEMORY|C_JPEG);
string BasePath;
GetContextOutputPath(BasePath);
stringstream GraphicPathName;
GraphicPathName << BasePath << m_Year << "\\" << setw(2) << setfill('0') ;
GraphicPathName << m_Month << "\\graph-month-" << setw(1) << k << ".jpg";
pGraphicContext->GetResult(C_SAVE_GRAPHIC, C_DEFAULT, (void*)GraphicPathName.str().c_str());
}
}
}
}
//////////////////////////////////////////////////////////////////////////
// Read stream
//////////////////////////////////////////////////////////////////////////
BOOL CMonthStats::ReadStream(ifstream& DBStream)
{
m_DatabaseUpdateNeeded = FALSE;
m_StatsPos=DBStream.tellg();
short Header;
DBStream.read((char*)&Header,sizeof(short));
if(Header != C_STATS_DB_MONTH_HEADER)
return C_STATS_DB_CORRUPTED;
short Month=0;
DBStream.read((char*)&Month,sizeof(short));
m_Month=Month;
m_StartPeriod = GetStartPeriod(m_Year, m_Month);
m_EndPeriod = GetEndPeriod(m_Year, m_Month);
short NbSubTimeStats;
DBStream.read((char*)&NbSubTimeStats,sizeof(short));
BOOL Success = CBaseStats::ReadStream(DBStream);
for (short i=0; i<NbSubTimeStats && Success==C_OK; i++)
{
CDayStats* pDayStats=new CDayStats(m_pStatsContext, m_Year, m_Month, 0, this);
Success = pDayStats->ReadStream(DBStream);
if (Success != C_OK)
delete pDayStats;
else
m_SubTimeStats.insert(pair<int,CBaseStats*>(pDayStats->GetDay(),pDayStats));
}
return Success;
}
//////////////////////////////////////////////////////////////////////////
// Write stream
//////////////////////////////////////////////////////////////////////////
BOOL CMonthStats::WriteStream(ofstream& DBStream)
{
short Header=C_STATS_DB_MONTH_HEADER;
DBStream.write((char*)&Header,sizeof(short));
short Month=(short)m_Month;
DBStream.write((char*)&Month,sizeof(short));
short NbSubTimeStats=(short)m_SubTimeStats.size();
DBStream.write((char*)&NbSubTimeStats,sizeof(short));
CBaseStats::WriteStream(DBStream);
map<int,CBaseStats*>::const_iterator it=m_SubTimeStats.begin();
for (/*empty*/; it!=m_SubTimeStats.end(); it++)
(it->second)->WriteStream(DBStream);
return TRUE;
}
//////////////////////////////////////////////////////////////////////////
// Method: GetDay
// FullName: CMonthStats::GetDay
// Access: public
//
//! \param int Day
//
//! \return CDayStats*
//
//! \brief
//
//////////////////////////////////////////////////////////////////////////
CDayStats* CMonthStats::GetDay(int Day)
{
SubTimeMap::iterator DayIt = m_SubTimeStats.find(Day);
if (DayIt != m_SubTimeStats.end())
{
ASSERT(DayIt->second->GetStatsType() == DAY_STATS);
return static_cast<CDayStats*>(DayIt->second);
}
return NULL;
}
//////////////////////////////////////////////////////////////////////////
// Method: GetTimestamp
// FullName: CMonthStats::GetTimestamp
// Access: public const
//
//
//! \return ULONG
//
//! \brief
//
//////////////////////////////////////////////////////////////////////////
ULONG CMonthStats::GetTimestamp() const
{
struct tm TmStruct;
memset(&TmStruct,0,sizeof(tm));
TmStruct.tm_mon = m_Month - 1;
TmStruct.tm_year = m_Year - 1900;
TmStruct.tm_isdst = -1;
return (ULONG)(mktime(&TmStruct));
}