找到最接近当前时间的时间

时间:2012-11-05 21:17:44

标签: c++ time systemtime

我想我的大脑只是抛出一个内存异常并且崩溃了, 我的问题是我有一个SYSTEMTIME大小为3的类成员数组,它是用户定义的(从.lua中读取)

SYSTEMTIME m_MatchTime[3];

然后从文件中以这种方式阅读:

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" ) );

现在我有一个方法:

SYSTEMTIME cTime;
GetLocalTime( &cTime );

我必须计算从三个用户定义的时间开始到更接近当前时间,然后计算剩余时间, (请注意星期日= 0,星期六= 6,还要注意只有wDayOfWeek,wHour和wMinute必须进行比较才能到达最近的地方)

编辑:现在我正在为 500bounty 寻找解决方案,请注意我想要的示例,

今天:第4天,第3小时,分钟0,
日期:第5天,第5小时,30分钟 到目前为止的剩余时间是:1天,2小时30分钟

5 个答案:

答案 0 :(得分:5)

鉴于问题域,似乎没有必要(甚至可取)强制执行严格的时间排序,您只想找到最接近给定标记值的一组时间。这将需要线性复杂性,但很容易实现。

我建议计算一个已知世纪的时差,在这种情况下,周日00:00:00(秒),然后比较每个时间点与该点之间的差异,看看哪个最接近。

#include <Windows.h>
#include <algorithm>
#include <iostream>

long seconds_from_sunday_epoch(const SYSTEMTIME& t)
{
   size_t seconds = t.wDayOfWeek * 86400;
   seconds += t.wHour * 3600;
   seconds += t.wMinute * 60;
   return seconds;
}

size_t timediff_2(const SYSTEMTIME& t0, const SYSTEMTIME& t1)
{
   size_t seconds_diff = std::abs(
      seconds_from_sunday_epoch(t0) -
      seconds_from_sunday_epoch(t1));

   return seconds_diff;
}

int main()
{
   SYSTEMTIME m_MatchTime[3];


   // Monday: 00:00
   m_MatchTime[0].wDayOfWeek = 1;
   m_MatchTime[0].wHour = 0;
   m_MatchTime[0].wMinute = 0;

   // Sunday: 01:00
   m_MatchTime[1].wDayOfWeek = 0;
   m_MatchTime[1].wHour = 1;
   m_MatchTime[1].wMinute = 0;

   // Wednesday: 15:30
   m_MatchTime[2].wDayOfWeek = 3;
   m_MatchTime[2].wHour = 15;
   m_MatchTime[2].wMinute = 30;

   // Sunday 23:00
   SYSTEMTIME cTime;
   cTime.wDayOfWeek = 0;
   cTime.wHour = 23;
   cTime.wMinute = 0;

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

答案 1 :(得分:3)

所以问题是你坐在一个圆圈上想要知道从d1到d2的右边(保持在同一周)或左边(一个值到下一个星期天)的距离更短。

首先,您应该将日期转换为公式分钟+小时* 60 +工作日* 60 * 24的值。这将给你一周的分钟。

#include <stdlib.h>
int minOfWeek (int d, int h, int m) {
  return d*60*24+h*60+m;
}

接下来找到最小距离:

const int minutesInWeek=60*24*7;
int bestDistance (int minutes1, int minutes2) {
  int d=abs (minutes1-minutes2);
  int dNext=minutesInWeek-d;
  return d<dNext?d:dNext;
}

因此,根据你的实际时间计算minOfWeek,用你所有的3次喂它到最佳距离并取最小的数字......

答案 2 :(得分:2)

标准C ++库让您可以通过将比较日期的“魔力”移动到仿函数,并使用带有自定义比较器的std::sort重载来优雅地解决这个问题。

以下是使用极少行代码(link to a quick test on ideone)执行此操作的方法:

class ClosestTo {
    int minute_now;
    int abs_minute(const SYSTEMTIME& t) const {
        return 60 * (24 * t.wDayOfWeek + t.wHour) + t.wMinute;
    }
    int diff_to_now(const SYSTEMTIME& t) const {
        int res = abs_minute(t) - minute_now;
        // Has the date passed this week?
       if (res < 0) {
            // Yes, the date has passed - move to next week:
            res += 7*24*60;
       }
        return res;
    }
public:
    ClosestTo(const SYSTEMTIME& now)
    :   minute_now(abs_minute(now)) {
    }
    // This is the operator the std::sort is going to call to determine ordering
    bool operator() (const SYSTEMTIME& lhs, const SYSTEMTIME& rhs) const {
        // Pick the date implying the shortest difference to minute_now
        return diff_to_now(lhs) < diff_to_now(rhs);
    }
};

就是这样,真的!有了这个比较器,您可以按如下方式对三个日期进行排序:

ClosestTo cmp(cTime);
sort(m_MatchTime, m_MatchTime+3, cmp);

现在最近的日期是零指数:

SYSTEMTIME &nearest = m_MatchTime[0];

答案 3 :(得分:0)

我已经找到了解决方案的算法,我知道它远非最专业的方式,但到目前为止它完美无瑕。

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

答案 4 :(得分:0)

const unsigned n=3; //replace with actual array size

auto packtime = [](const SYSTEMTIME& t)->unsigned
{
    return t.wDayOfWeek*24*60 + t.wHour*60 + t.wMinute;
};
auto unpacktime = [](unsigned total)->SYSTEMTIME
{
    SYSTEMTIME ret;

    ret.wDayOfWeek = total/(60*24);
    total %= (60*24);
    ret.wHour = total/60;
    ret.wMinute = total%60;

    return ret;
};

unsigned const wraptime = 7*24*60;
unsigned targettime = packtime(cTime);

unsigned mintimedif = wraptime + 1;
unsigned mindifidx;
unsigned timedif;

for(unsigned i=0; i<n; ++i)
{
    timedif = packtime(m_MatchTime[i]);

    if(timedif < targettime)
        timedif = targettime - timedif;
    else
        timedif = wraptime - timedif + targettime;

    if(timedif < mintimedif)
    {
        mintimedif = timedif;
        mindifidx = i;
    }
}

SYSTEMTIME dif = unpacktime(mintimedif);

std::cout<<"Today: Day "<<cTime.wDayOfWeek<<" Hour "<<cTime.wHour<<" Minute "<<cTime.wMinute<<std::endl;
std::cout<<"Nearest day: Day "<<m_MatchTime[mindifidx].wDayOfWeek<<" Hour "<<m_MatchTime[mindifidx].wHour<<" Minute "<<m_MatchTime[mindifidx].wMinute<<std::endl;
std::cout<<"Difference: "<<dif.wDayOfWeek<<" days "<<dif.wHour<<" hours "<<dif.wMinute<<" minutes"<<std::endl;</code>