better encapsulation of DateTime object
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
#ifndef Bookkeeper_H
|
||||
#define Bookkeeper_H
|
||||
|
||||
#include "TimeConstants.h"
|
||||
#include "DateTime.h"
|
||||
|
||||
|
||||
class Bookkeeper
|
||||
|
||||
@@ -1,7 +1,85 @@
|
||||
#include "global.h"
|
||||
#include "TimeConstants.h"
|
||||
#include "DateTime.h"
|
||||
#include "RageUtil.h"
|
||||
|
||||
|
||||
DateTime DateTime::GetNowDateTime()
|
||||
{
|
||||
time_t now = time(NULL);
|
||||
DateTime tNow;
|
||||
localtime_r( &now, &tNow );
|
||||
return tNow;
|
||||
}
|
||||
|
||||
DateTime DateTime::GetNowDate()
|
||||
{
|
||||
DateTime tNow = GetNowDateTime();
|
||||
tNow.StripTime();
|
||||
return tNow;
|
||||
}
|
||||
|
||||
void DateTime::StripTime()
|
||||
{
|
||||
tm_hour = 0;
|
||||
tm_min = 0;
|
||||
tm_sec = 0;
|
||||
}
|
||||
|
||||
//
|
||||
// Common SQL/XML format: "YYYY-MM-DD HH:MM:SS"
|
||||
//
|
||||
CString DateTime::GetString() const
|
||||
{
|
||||
return ssprintf( "%d-%02d-%02d %02d:%02d:%02d",
|
||||
tm_year+1900,
|
||||
tm_mon+1,
|
||||
tm_mday,
|
||||
tm_hour,
|
||||
tm_min,
|
||||
tm_sec );
|
||||
}
|
||||
|
||||
CString DateTime::GetDateString() const
|
||||
{
|
||||
return ssprintf( "%d-%02d-%02d",
|
||||
tm_year+1900,
|
||||
tm_mon+1,
|
||||
tm_mday );
|
||||
}
|
||||
|
||||
bool DateTime::FromString( const CString sDateTime )
|
||||
{
|
||||
int ret;
|
||||
|
||||
ZERO( *this );
|
||||
|
||||
ret = sscanf( sDateTime, "%d-%d-%d %d:%d:%d",
|
||||
&tm_year,
|
||||
&tm_mon,
|
||||
&tm_mday,
|
||||
&tm_hour,
|
||||
&tm_min,
|
||||
&tm_sec );
|
||||
if( ret == 6 )
|
||||
goto success;
|
||||
|
||||
ret = sscanf( sDateTime, "%d-%d-%d",
|
||||
&tm_year,
|
||||
&tm_mon,
|
||||
&tm_mday );
|
||||
if( ret == 3 )
|
||||
goto success;
|
||||
|
||||
return false;
|
||||
|
||||
success:
|
||||
tm_year -= 1900;
|
||||
tm_mon -= 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
CString DayInYearToString( int iDayInYear )
|
||||
{
|
||||
return ssprintf("DayInYear%03d",iDayInYear);
|
||||
@@ -148,8 +226,9 @@ tm GetDayInYearAndYear( int iDayInYearIndex, int iYear )
|
||||
return when;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (c) 2004 Chris Danford
|
||||
* (c) 2001-2004 Chris Danford
|
||||
* All rights reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
@@ -1,7 +1,5 @@
|
||||
/* Time helpers. */
|
||||
|
||||
#ifndef TimeConstants_H
|
||||
#define TimeConstants_H
|
||||
#ifndef DATE_TIME_H
|
||||
#define DATE_TIME_H
|
||||
|
||||
#include <ctime>
|
||||
|
||||
@@ -26,10 +24,37 @@ tm GetNextSunday( tm start );
|
||||
|
||||
tm GetDayInYearAndYear( int iDayInYearIndex, int iYear );
|
||||
|
||||
|
||||
struct DateTime : public tm
|
||||
{
|
||||
bool operator<( const DateTime& other ) const
|
||||
{
|
||||
#define COMPARE( v ) if(v!=other.v) return v<other.v;
|
||||
COMPARE( tm_year );
|
||||
COMPARE( tm_mon );
|
||||
COMPARE( tm_mday );
|
||||
COMPARE( tm_hour );
|
||||
COMPARE( tm_min );
|
||||
COMPARE( tm_sec );
|
||||
#undef COMPARE
|
||||
// they're equal
|
||||
return true;
|
||||
}
|
||||
static DateTime GetNowDateTime();
|
||||
static DateTime GetNowDate(); // GetNowDateTime() with time chopped off
|
||||
|
||||
void StripTime();
|
||||
|
||||
CString GetString() const;
|
||||
CString GetDateString() const;
|
||||
bool FromString( const CString sDateTime );
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
/*
|
||||
* (c) 2004 Chris Danford
|
||||
* (c) 2001-2004 Chris Danford
|
||||
* All rights reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
@@ -55,7 +55,7 @@ DataStructures = \
|
||||
Attack.cpp Attack.h BannerCache.cpp BannerCache.h CatalogXml.cpp CatalogXml.h \
|
||||
Character.cpp Character.h CharacterHead.cpp CharacterHead.h \
|
||||
CodeDetector.cpp CodeDetector.h Difficulty.cpp Difficulty.h EnumHelper.cpp EnumHelper.h Course.cpp Course.h \
|
||||
CourseUtil.cpp CourseUtil.h Font.cpp Font.h FontCharAliases.cpp FontCharAliases.h \
|
||||
CourseUtil.cpp CourseUtil.h DateTime.cpp DateTime.h Font.cpp Font.h FontCharAliases.cpp FontCharAliases.h \
|
||||
FontCharmaps.cpp FontCharmaps.h Game.h GameConstantsAndTypes.cpp GameConstantsAndTypes.h GameDef.cpp GameDef.h \
|
||||
GameInput.cpp GameInput.h Grade.cpp Grade.h HighScore.cpp HighScore.h Inventory.cpp Inventory.h LuaFunctions.h \
|
||||
LuaHelpers.cpp LuaHelpers.h LyricsLoader.cpp LyricsLoader.h MenuInput.h ModeChoice.cpp ModeChoice.h \
|
||||
@@ -69,7 +69,7 @@ ScoreKeeper.h ScoreKeeperMAX2.cpp ScoreKeeperMAX2.h \
|
||||
ScoreKeeperRave.cpp ScoreKeeperRave.h Song.cpp song.h SongCacheIndex.cpp SongCacheIndex.h \
|
||||
SongOptions.cpp SongOptions.h SongUtil.cpp SongUtil.h StageStats.cpp StageStats.h Steps.cpp Steps.h \
|
||||
StepsUtil.cpp StepsUtil.h Style.cpp Style.h \
|
||||
StyleInput.h TimeConstants.cpp TimeConstants.h TimingData.cpp TimingData.h \
|
||||
StyleInput.h TimingData.cpp TimingData.h \
|
||||
Trail.cpp Trail.h TrailUtil.cpp TrailUtil.h TitleSubstitution.cpp TitleSubstitution.h
|
||||
|
||||
FileTypes = IniFile.cpp IniFile.h \
|
||||
|
||||
+18
-27
@@ -11,7 +11,6 @@
|
||||
#include "SongManager.h"
|
||||
#include "Steps.h"
|
||||
#include "Course.h"
|
||||
#include <ctime>
|
||||
#include "ThemeManager.h"
|
||||
#include "CryptManager.h"
|
||||
#include "ProfileManager.h"
|
||||
@@ -184,11 +183,8 @@ CString Profile::GetDisplayTotalCaloriesBurned() const
|
||||
|
||||
CString Profile::GetDisplayTotalCaloriesBurnedToday() const
|
||||
{
|
||||
time_t now = time(NULL);
|
||||
tm tNow;
|
||||
localtime_r( &now, &tNow );
|
||||
Day today = { tNow.tm_yday, tNow.tm_year+1900 };
|
||||
float fCals = GetCaloriesBurnedForDay(today);
|
||||
DateTime now = DateTime::GetNowDate();
|
||||
float fCals = GetCaloriesBurnedForDay(now);
|
||||
if( m_iWeightPounds == 0 ) // weight not entered
|
||||
return "N/A";
|
||||
else
|
||||
@@ -991,9 +987,8 @@ void Profile::AddStepTotals( int iTotalTapsAndHolds, int iTotalJumps, int iTotal
|
||||
SCALE( m_iWeightPounds, 100.f, 200.f, 0.222f, 0.386f ) * iTotalHands;
|
||||
m_fTotalCaloriesBurned += fCals;
|
||||
|
||||
tm cur_tm = GetLocalTime();
|
||||
Day day = { cur_tm.tm_yday, cur_tm.tm_year+1900 };
|
||||
m_mapDayToCaloriesBurned[day] += fCals;
|
||||
DateTime date = DateTime::GetNowDate();
|
||||
m_mapDayToCaloriesBurned[date] += fCals;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1301,25 +1296,23 @@ void Profile::LoadCalorieDataFromNode( const XNode* pNode )
|
||||
CHECKPOINT;
|
||||
|
||||
ASSERT( pNode->name == "CalorieData" );
|
||||
FOREACH_CONST( XNode*, pNode->childs, pDay )
|
||||
FOREACH_CONST( XNode*, pNode->childs, pCaloriesBurned )
|
||||
{
|
||||
if( (*pDay)->name != "Day" )
|
||||
if( (*pCaloriesBurned)->name != "CaloriesBurned" )
|
||||
WARN_AND_CONTINUE;
|
||||
|
||||
Day day;
|
||||
|
||||
if( !(*pDay)->GetAttrValue("DayInYear",day.iDayInYear) )
|
||||
CString sDate;
|
||||
if( !(*pCaloriesBurned)->GetAttrValue("Date",sDate) )
|
||||
WARN_AND_CONTINUE;
|
||||
|
||||
if( !(*pDay)->GetAttrValue("Year",day.iYear) )
|
||||
DateTime date;
|
||||
if( !date.FromString(sDate) )
|
||||
WARN_AND_CONTINUE;
|
||||
|
||||
float fCaloriesBurned = 0;
|
||||
|
||||
if( !(*pDay)->GetChildValue("CaloriesBurned",fCaloriesBurned) )
|
||||
WARN_AND_CONTINUE;
|
||||
(*pCaloriesBurned)->GetValue(fCaloriesBurned);
|
||||
|
||||
m_mapDayToCaloriesBurned[day] = fCaloriesBurned;
|
||||
m_mapDayToCaloriesBurned[date] = fCaloriesBurned;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1333,24 +1326,22 @@ XNode* Profile::SaveCalorieDataCreateNode() const
|
||||
XNode* pNode = new XNode;
|
||||
pNode->name = "CalorieData";
|
||||
|
||||
for( map<Day,float>::const_iterator i = m_mapDayToCaloriesBurned.begin();
|
||||
for( map<DateTime,float>::const_iterator i = m_mapDayToCaloriesBurned.begin();
|
||||
i != m_mapDayToCaloriesBurned.end();
|
||||
i++ )
|
||||
{
|
||||
XNode* pDay = pNode->AppendChild( "Day" );
|
||||
XNode* pCaloriesBurned = pNode->AppendChild( "CaloriesBurned", i->second );
|
||||
|
||||
pDay->AppendAttr( "DayInYear", i->first.iDayInYear );
|
||||
pDay->AppendAttr( "Year", i->first.iYear );
|
||||
|
||||
pDay->AppendChild( "CaloriesBurned", i->second );
|
||||
pCaloriesBurned->AppendAttr( "Date", i->first.GetDateString() );
|
||||
}
|
||||
|
||||
return pNode;
|
||||
}
|
||||
|
||||
float Profile::GetCaloriesBurnedForDay( Day day ) const
|
||||
float Profile::GetCaloriesBurnedForDay( DateTime day ) const
|
||||
{
|
||||
map<Day,float>::const_iterator i = m_mapDayToCaloriesBurned.find( day );
|
||||
day.StripTime();
|
||||
map<DateTime,float>::const_iterator i = m_mapDayToCaloriesBurned.find( day );
|
||||
if( i == m_mapDayToCaloriesBurned.end() )
|
||||
return 0;
|
||||
else
|
||||
|
||||
+3
-18
@@ -8,7 +8,7 @@
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include "HighScore.h"
|
||||
#include "TimeConstants.h"
|
||||
#include "DateTime.h"
|
||||
#include "SongUtil.h" // for SongID
|
||||
#include "StepsUtil.h" // for StepsID
|
||||
#include "CourseUtil.h" // for CourseID
|
||||
@@ -186,23 +186,8 @@ public:
|
||||
// a mis-set clock could wipe out all your past data. With this scheme,
|
||||
// the worst that could happen is that playing on a mis-set machine will
|
||||
// insert some garbage entries into the map.
|
||||
struct Day
|
||||
{
|
||||
int iDayInYear; // 0-365
|
||||
int iYear; // e.g. 2004
|
||||
bool operator==( const Day& other ) const { return iDayInYear==other.iDayInYear && iYear==other.iYear; }
|
||||
bool operator<( const Day& other ) const
|
||||
{
|
||||
if(iYear<other.iYear)
|
||||
return true;
|
||||
if( iYear>other.iYear)
|
||||
return false;
|
||||
else
|
||||
return iDayInYear<other.iDayInYear;
|
||||
}
|
||||
};
|
||||
map<Day,float> m_mapDayToCaloriesBurned;
|
||||
float GetCaloriesBurnedForDay( Day day ) const;
|
||||
map<DateTime,float> m_mapDayToCaloriesBurned;
|
||||
float GetCaloriesBurnedForDay( DateTime day ) const;
|
||||
|
||||
//
|
||||
// Awards
|
||||
|
||||
@@ -632,6 +632,14 @@ SOURCE=.\CourseUtil.h
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\DateTime.cpp
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\DateTime.h
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\Difficulty.cpp
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
@@ -988,14 +996,6 @@ SOURCE=.\StyleInput.h
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\TimeConstants.cpp
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\TimeConstants.h
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\TimingData.cpp
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
@@ -605,6 +605,12 @@ cl /Zl /nologo /c verstub.cpp /Fo"$(IntDir)"\
|
||||
<File
|
||||
RelativePath="CourseUtil.h">
|
||||
</File>
|
||||
<File
|
||||
RelativePath="DateTime.cpp">
|
||||
</File>
|
||||
<File
|
||||
RelativePath="DateTime.h">
|
||||
</File>
|
||||
<File
|
||||
RelativePath="Difficulty.cpp">
|
||||
</File>
|
||||
|
||||
Reference in New Issue
Block a user