diff --git a/stepmania/src/Bookkeeper.h b/stepmania/src/Bookkeeper.h index 66774a170d..5942ba6a2c 100644 --- a/stepmania/src/Bookkeeper.h +++ b/stepmania/src/Bookkeeper.h @@ -3,7 +3,7 @@ #ifndef Bookkeeper_H #define Bookkeeper_H -#include "TimeConstants.h" +#include "DateTime.h" class Bookkeeper diff --git a/stepmania/src/TimeConstants.cpp b/stepmania/src/DateTime.cpp similarity index 79% rename from stepmania/src/TimeConstants.cpp rename to stepmania/src/DateTime.cpp index 0ea731d2b0..eebb7d9eb0 100644 --- a/stepmania/src/TimeConstants.cpp +++ b/stepmania/src/DateTime.cpp @@ -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 diff --git a/stepmania/src/TimeConstants.h b/stepmania/src/DateTime.h similarity index 75% rename from stepmania/src/TimeConstants.h rename to stepmania/src/DateTime.h index 3d5408b58a..1a0083d736 100644 --- a/stepmania/src/TimeConstants.h +++ b/stepmania/src/DateTime.h @@ -1,7 +1,5 @@ -/* Time helpers. */ - -#ifndef TimeConstants_H -#define TimeConstants_H +#ifndef DATE_TIME_H +#define DATE_TIME_H #include @@ -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 #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::const_iterator i = m_mapDayToCaloriesBurned.begin(); + for( map::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::const_iterator i = m_mapDayToCaloriesBurned.find( day ); + day.StripTime(); + map::const_iterator i = m_mapDayToCaloriesBurned.find( day ); if( i == m_mapDayToCaloriesBurned.end() ) return 0; else diff --git a/stepmania/src/Profile.h b/stepmania/src/Profile.h index 53e89eab6b..46d6722817 100644 --- a/stepmania/src/Profile.h +++ b/stepmania/src/Profile.h @@ -8,7 +8,7 @@ #include #include #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(iYearother.iYear) - return false; - else - return iDayInYear m_mapDayToCaloriesBurned; - float GetCaloriesBurnedForDay( Day day ) const; + map m_mapDayToCaloriesBurned; + float GetCaloriesBurnedForDay( DateTime day ) const; // // Awards diff --git a/stepmania/src/StepMania.dsp b/stepmania/src/StepMania.dsp index 15cd906a9d..6d5a56ffd2 100644 --- a/stepmania/src/StepMania.dsp +++ b/stepmania/src/StepMania.dsp @@ -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 diff --git a/stepmania/src/StepMania.vcproj b/stepmania/src/StepMania.vcproj index c730354c77..d8f0cb18cf 100644 --- a/stepmania/src/StepMania.vcproj +++ b/stepmania/src/StepMania.vcproj @@ -605,6 +605,12 @@ cl /Zl /nologo /c verstub.cpp /Fo"$(IntDir)"\ + + + +