contract IntervalBased is DSMath {
    // the start interval
    uint256 public intervalStartTimestamp;

    // interval duration (e.g. 1 days)
    uint256 public intervalDuration;

    // the max amount of intervals that can be processed for interest claim
    uint256 public intervalMaximum;

    // interval offset
    uint256 public intervalOffset;

    function changeDuration(uint256 duration) internal {
      // protect againt unecessary change of offset and starttimestamp
      if (duration == intervalDuration) { return; }

      // offset all previous intervals
      intervalOffset = intervalNumber(block.timestamp);

      // set new duration
      intervalDuration = duration;

      // restart timestamp to current
      intervalStartTimestamp = block.timestamp;
    }

    // get the interval number from start position
    // every timestamp should have an interval past the start timestamp..
    function intervalNumber(uint256 timestamp) public view returns(uint256 number) {
        return add(intervalOffset, sub(timestamp, intervalStartTimestamp) / intervalDuration);
    }
}
/**
 * @title IntervalBased
 * @author UnityCoin Team
 * @notice Primitive events, methods, properties for a contract which has a
 *        interval system, that can be changed in-flight.
 *
 *        Here we create a system in which any valid unixtimestamp can reduce
 *        down to a specific interval number, based on a start time, duration
 *        and offset.
 *
 *        Interval Derivation
 *        number = offset + ((timestamp - start time) / intervalDuration)
 *
 *        Note, when your changing the interval params in flight, we must
 *        set the offset to the most current interval number, as to not
 *        disrupt previously used interval numbers / mechanics
 */
function intervalNumber(uint256 timestamp) public view returns(uint256 number) {
    return add(intervalOffset, sub(timestamp, intervalStartTimestamp) / intervalDuration);
}

// get the interval number from start position
// every timestamp should have an interval past the start timestamp..