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;
}