I guess my brain just threw an out of memory exception and crashed,
My problem is I have a class member array of SYSTEMTIME
size 3 which is user defined (read from a .lua)
SYSTEMTIME m_MatchTime[3];
Then it's read this way from the file:
m_MatchTime[0].wDayOfWeek = static_cast<WORD>( m_Lua.GetGlobalNumber( "FirstDay" ) );
m_MatchTime[0].wHour = static_cast<WORD>( m_Lua.GetGlobalNumber( "FirstHour" ) );
m_MatchTime[0].wMinute = static_cast<WORD>( m_Lua.GetGlobalNumber( "FirstMinute" ) );
m_MatchTime[1].wDayOfWeek = static_cast<WORD>( m_Lua.GetGlobalNumber( "SecondDay" ) );
m_MatchTime[1].wHour = static_cast<WORD>( m_Lua.GetGlobalNumber( "SecondHour" ) );
m_MatchTime[1].wMinute = static_cast<WORD>( m_Lua.GetGlobalNumber( "SecondMinute" ) );
m_MatchTime[2].wDayOfWeek = static_cast<WORD>( m_Lua.GetGlobalNumber( "ThirdDay" ) );
m_MatchTime[2].wHour = static_cast<WORD>( m_Lua.GetGlobalNumber( "ThirdHour" ) );
m_MatchTime[2].wMinute = static_cast<WORD>( m_Lua.GetGlobalNumber( "ThirdMinute" ) );
now I have a method:
SYSTEMTIME cTime;
GetLocalTime( &cTime );
I must calculate which from the three user defined times is BEFORE and closer to the current time, then calculate the remaining time to it, (please notice that Sunday = 0, Saturday = 6, also note that only wDayOfWeek, wHour and wMinute must be compared to get to the closest )
Edit: Now I'm awarding 500bounty for a solution, please note the example of what I want,
Today: Day 4, Hour 3, Minute 0,
Date: Day 5, Hour 5, Minute 30
The remaining time until date is: 1 days, 2 hour and 30 minutes.
I have came with an algorithm for the solution, I know its far from the most professional way to do it, but its flawless so far.
int main()
{
SYSTEMTIME m_MatchTime[3];
// Monday: 00:00
m_MatchTime[0].wDayOfWeek = 1;
m_MatchTime[0].wHour = 22;
m_MatchTime[0].wMinute = 4;
// Sunday: 01:00
m_MatchTime[1].wDayOfWeek = 4;
m_MatchTime[1].wHour = 1;
m_MatchTime[1].wMinute = 0;
// Wednesday: 15:30
m_MatchTime[2].wDayOfWeek = 6;
m_MatchTime[2].wHour = 15;
m_MatchTime[2].wMinute = 30;
// Sunday 23:00
SYSTEMTIME cTime;
cTime.wDayOfWeek = 3;
cTime.wHour = 14;
cTime.wMinute = 5;
/* std::cout << timediff_2(cTime, m_MatchTime[0]) << "\n";
std::cout << timediff_2(cTime, m_MatchTime[1]) << "\n";
std::cout << timediff_2(cTime, m_MatchTime[2]) << "\n";*/
vector<size_t>m_Time;
if( cTime.wDayOfWeek == 0 )
{
for( int i =0; i<3; i++ )
{
if( cTime.wDayOfWeek >= m_MatchTime[i].wDayOfWeek )
m_Time.push_back( timediff_2(cTime, m_MatchTime[i]) );
}
if( m_Time.size() == 0 ) //trim right
{
for( int i =0; i<3; i++ )
{
if( cTime.wDayOfWeek <= m_MatchTime[i].wDayOfWeek )
m_Time.push_back( timediff_2(cTime, m_MatchTime[i]) );
}
}
}
else
{
for( int i =0; i<3; i++ )
{
if( cTime.wDayOfWeek <= m_MatchTime[i].wDayOfWeek )
m_Time.push_back( timediff_2(cTime, m_MatchTime[i]) );
}
if( m_Time.size() == 0 ) //trim right
{
for( int i =0; i<3; i++ )
{
if( cTime.wDayOfWeek >= m_MatchTime[i].wDayOfWeek )
m_Time.push_back( timediff_2(cTime, m_MatchTime[i]) );
}
}
}
std::sort( m_Time.begin(), m_Time.end() );
SYSTEMTIME nearest;
if( m_Time.size() > 0 )
{
for( int l=0; l<3; l++ )
{
if( timediff_2( cTime, m_MatchTime[l] ) == m_Time.at(0) )
{
nearest = m_MatchTime[l];
break;
}
}
}
unsigned int manydaysleft = howmanydaysuntil( nearest.wDayOfWeek , cTime.wDayOfWeek );
unsigned int manyhoursleft = howmanyhoursuntil( nearest.wHour, cTime.wHour );
if( nearest.wHour < cTime.wHour ) //manydaysleft will always be > 0
manydaysleft--;
unsigned int manyminutesleft = howmanyminutesuntil( nearest.wMinute, cTime.wMinute );
if( nearest.wMinute < cTime.wMinute )
manyhoursleft--;
/*cout
<< manydaysleft << endl
<< manyhoursleft << endl
<< manyminutesleft << endl;*/
cout << "CurrentTime\n"
<< "Day:" << cTime.wDayOfWeek
<< "Hour:" << cTime.wHour
<< "Min:" << cTime.wMinute
<< "\nDay:" << nearest.wDayOfWeek
<< "Hour:" << nearest.wHour
<< "Min:" << nearest.wMinute
<< "\nDay:" << manydaysleft
<< "Hour:" << manyhoursleft
<< "Min:" << manyminutesleft;
return 0;
}