我无法清楚地描述我的问题,如果我的问题的标题看起来很奇怪,请原谅。
我正在做一个时间课。
我正在使用这些变量:私有的,带有 _ticks :
// 1 _ticks = 1/100 of a second
// 0 _ticks = 00:00:00.00 i.e. 12:00am
// a time is stored as a number of ticks since midnight
// for example 1234567 ticks would be 3:25:45.67am
long _ticks;
// the following static fields might come in handy
// 8,643,999 _ticks = 23:59:59.99 i.e. 11:59:59.99pm
static const long _lastTickOfTheDay = 8639999;
// 4,320,000 _ticks = 12:00:00.00 i.e 12pm i.e. noon
static const long _noon = 4320000;
// _ticks per second;
static const long _ticksPerSecond = 100;
// _ticks per minute;
static const long _ticksPerMinute = 6000;
// _ticks per hour;
static const long _ticksPerHour = 360000;
// _ticks per day
static const long _ticksPerDay = 8640000;
考虑到这一点,我正在制作用小时、分钟、秒和毫秒来设置时间的函数。使用所有这些变量设置时间非常简单:
void MyTime::SetTime(int newHrs, int newMins, int newSecs, int newMilisecs)
{
this->_ticks = (newHrs * _ticksPerHour) + (newMins * _ticksPerMinute)
+ (newSecs * _ticksPerSecond) + (newMilisecs);
}
接下来,我需要将时间设置为小时、分钟、秒,同时保持毫秒。如何做到这一点的数学让我难以捉摸,这已经是我所能做到的了。如您所见,不多:
// Hours, Minutes, Seconds
void MyTime::SetTime(int newHours, int newMinutes, int newSeconds)
{
// Take the ticks apart and put them back together
int oldTime = _ticks;
int newTime = (newHours * _ticksPerHour) + (newMinutes * _ticksPerMinute)
+ (newSeconds * _ticksPerSecond);
}