简体   繁体   中英

How do I retain the hours, minutes, seconds in a long variable?

I'm having trouble describing my question clearly, so excuse me if my question's title seems odd.

I am making a time class.

I am working with these variables: private, with _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;

With that in mind, I am making functions to set the time with hours, minutes, seconds, and milliseconds. Setting the time with all of those variables was simple enough:

void MyTime::SetTime(int newHrs, int newMins, int newSecs, int newMilisecs)
{
    this->_ticks = (newHrs * _ticksPerHour) + (newMins * _ticksPerMinute) 
            + (newSecs * _ticksPerSecond) + (newMilisecs);
}

Next, I need to set the time for only hours, minutes, seconds while maintaining the milliseconds. The math of how to do this eludes me, and this is as much as I have been able to do. As you can see, not much:

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

It might be worth writing methods to extract each part:

int MyTime::hours() {
  return _ticks / _ticksPerHour;
}
int MyTime::minutes() {
  return (_ticks % _ticksPerHour) / _ticksPerMinute;
}
int MyTime::seconds() {
  return (_ticks % _ticksPerMinute) / _ticksPerSecond;
}
int MyTime::millis() {
  return _ticks % _ticksPerSecond;
}

These use integer arithmetic, where / and % give the quotient and remainder, respectively.

To perform an isolated update on one of these, you can calculate the differential relative to the current value, and just add it in. For example:

void MyTime::setMinutes(int newMinutes) {
  _ticks += (newMinutes - minutes())*_ticksPerMinute;
}

Similar code will work for the other parts.

If your unit of measurement is 1/100 s, then simply store and reinstate n % 100 , ie the fractional-seconds part.

(If you're actually storing milliseconds, then it's n % 1000 , of course.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM