contract Scheduller {
	using SafeMath for uint256;
	//last period recorded
    uint256 public period;
    //period duration in sec
    uint256 public periodTime;
    //last period start time
    uint256 public periodStartTime;

	/**
     * @dev Scheduller constructor, saves periodTime in seconds
     * @param _periodDays period length in Days
     */
    constructor (
    	uint256 _periodDays
    )
    	public
    {
    	periodTime = _periodDays.mul(1 days);
    }

    /**
     * @dev Updates last period to current
     * @param _startNow set periodStartTime = now if true
     */
    function _updatePeriod (bool _startNow)
        internal
    {
        uint256 _currentPeriod = currentPeriod();
        if(_currentPeriod != period){
        	period = _currentPeriod;
            if(_startNow)
                // solhint-disable-next-line not-rely-on-time
                periodStartTime = block.timestamp;
            else
                periodStartTime = 0;
        }else{
            if(_startNow && periodStartTime == 0)
                // solhint-disable-next-line not-rely-on-time
                periodStartTime = block.timestamp;
        }
    }

    /**
     * @dev Returns current period
     */
    function currentPeriod () 
        public 
        view 
        returns (uint256 _currentPeriod)
    {
    	_currentPeriod = period;
    	if(periodStartTime == 0 && _currentPeriod != 0)
    		return _currentPeriod;
    	// solhint-disable-next-line not-rely-on-time
    	if( block.timestamp >= periodStartTime.add(periodTime) )
    		_currentPeriod = _currentPeriod.add(1); 
		return _currentPeriod;
    }
}
function _updatePeriod (bool _startNow)
    internal
{
    uint256 _currentPeriod = currentPeriod();
    if(_currentPeriod != period){
    	period = _currentPeriod;
        if(_startNow)
            // solhint-disable-next-line not-rely-on-time
            periodStartTime = block.timestamp;
        else
            periodStartTime = 0;
    }else{
        if(_startNow && periodStartTime == 0)
            // solhint-disable-next-line not-rely-on-time
            periodStartTime = block.timestamp;
    }
}

/**
 * @dev Updates last period to current
 * @param _startNow set periodStartTime = now if true
 */