int TimeIndex::GetIndex(const DTime& time, int *dist) const { int i, d; if (!m_time.IsValid() || !time.IsValid()) { i = -1, d = 0; } else { i = d = m_time.GetDistance(time); if (d < 0 || d >= (int)m_number_of_values) i = -1; } if (dist) *dist = d; return i; }
int DTime::GetDistance(const DTime &t) const { assert(IsValid()); assert(t.IsValid()); if (GetPeriod() != t.GetPeriod()) { wxLogError(_T("our period: %d, his period: %d"), GetPeriod(), t.GetPeriod()); assert(false); } const wxDateTime& _t0 = GetTime(); const wxDateTime& _t = t.GetTime(); int ret = -1; switch (GetPeriod()) { case PERIOD_T_DECADE: ret = _t.GetYear() - _t0.GetYear(); break; case PERIOD_T_YEAR: ret = _t.GetYear() * 12 + _t.GetMonth() - _t0.GetYear() * 12 - _t0.GetMonth(); break; case PERIOD_T_MONTH: ret = (_t - _t0).GetHours(); //acount for daylight saving time switch (ret % 24) { case 1: ret -= 1; break; case 23: ret += 1; break; case -1: ret += 1; break; case -23: ret -= 1; break; } ret /= 24; break; case PERIOD_T_DAY: ret = (_t - _t0).GetMinutes() / 10; break; case PERIOD_T_30MINUTE: ret = ((_t - _t0).GetSeconds() / 10).ToLong(); break; case PERIOD_T_5MINUTE: ret = ((_t - _t0).GetSeconds()).ToLong(); break; case PERIOD_T_MINUTE: ret = ((_t - _t0).GetMilliseconds() / 500).ToLong(); break; case PERIOD_T_30SEC: ret = ((_t - _t0).GetMilliseconds() / 100).ToLong(); break; case PERIOD_T_WEEK: ret = (_t - _t0).GetHours() / 8; if (_t0.IsDST() != _t.IsDST()) { if (_t0 < _t && _t.IsDST()) ret += 1; else if (_t < _t0 && _t0.IsDST()) ret -= 1; } break; case PERIOD_T_SEASON: ret = (_t - _t0).GetDays() / 7; if (_t0.IsDST() != _t.IsDST()) { if (_t0 < _t && _t.IsDST()) ret += 1; else if (_t < _t0 && _t0.IsDST()) ret -= 1; } break; default: assert(false); } return ret; }
bool DTime::IsBetween(const DTime& t1, const DTime& t2) const { if (t1.IsValid() && t2.IsValid()) return t1 <= *this && t2 > *this; else return false; }