示例#1
0
DTime TimeIndex::AdjustToPeriodSpan(const DTime &time) const {
	wxDateTime dt = time.GetTime();

	if (m_number_of_values == default_units_count[time.GetPeriod()] * PeriodMult[time.GetPeriod()]) {
		switch (time.GetPeriod()) {
			case PERIOD_T_DECADE :
			case PERIOD_T_YEAR :
				dt.SetMonth(wxDateTime::Jan);
			case PERIOD_T_MONTH :
				dt.SetDay(1);
				dt.SetHour(0);
				break;
			case PERIOD_T_WEEK:
			case PERIOD_T_SEASON :
				dt.SetToWeekDayInSameWeek(wxDateTime::Mon);
			case PERIOD_T_DAY :
				dt.SetHour(0);
				dt.SetMinute(0);
				break;
			case PERIOD_T_30MINUTE :
				dt.SetMinute(dt.GetMinute() / 30 * 30);
				dt.SetSecond(0);
				break;
			case PERIOD_T_5MINUTE :
				dt.SetMinute(dt.GetMinute() / 3 * 3);
				dt.SetSecond(0);
				break;
			case PERIOD_T_MINUTE :
			case PERIOD_T_30SEC :
				dt.SetSecond(dt.GetSecond() / 30 * 30);
				break;
			case PERIOD_T_OTHER:
			case PERIOD_T_LAST:
				assert(false);
		}
		return DTime(time.GetPeriod(), dt);
	} else {
		DTime ret(time);
		return ret.AdjustToPeriodStart();
	}
}
示例#2
0
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;
}