2023-01-10 13:16:32 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
pragma solidity ^0.8.8;
|
|
|
|
|
|
|
|
contract Periods {
|
|
|
|
type Period is uint256;
|
|
|
|
|
2023-01-23 10:57:10 +00:00
|
|
|
uint256 internal immutable _secondsPerPeriod;
|
2023-01-10 13:16:32 +00:00
|
|
|
|
2023-01-23 10:57:10 +00:00
|
|
|
constructor(uint256 secondsPerPeriod) {
|
|
|
|
_secondsPerPeriod = secondsPerPeriod;
|
2023-01-10 13:16:32 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 15:47:29 +00:00
|
|
|
function _periodOf(uint256 timestamp) internal view returns (Period) {
|
2023-01-23 10:57:10 +00:00
|
|
|
return Period.wrap(timestamp / _secondsPerPeriod);
|
2023-01-10 13:16:32 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 15:47:29 +00:00
|
|
|
function _blockPeriod() internal view returns (Period) {
|
|
|
|
return _periodOf(block.timestamp);
|
2023-01-10 13:16:32 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 15:47:29 +00:00
|
|
|
function _nextPeriod(Period period) internal pure returns (Period) {
|
2023-01-10 13:16:32 +00:00
|
|
|
return Period.wrap(Period.unwrap(period) + 1);
|
|
|
|
}
|
|
|
|
|
2023-01-19 15:47:29 +00:00
|
|
|
function _periodStart(Period period) internal view returns (uint256) {
|
2023-01-23 10:57:10 +00:00
|
|
|
return Period.unwrap(period) * _secondsPerPeriod;
|
2023-01-10 13:16:32 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 15:47:29 +00:00
|
|
|
function _periodEnd(Period period) internal view returns (uint256) {
|
|
|
|
return _periodStart(_nextPeriod(period));
|
2023-01-10 13:16:32 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 15:47:29 +00:00
|
|
|
function _isBefore(Period a, Period b) internal pure returns (bool) {
|
2023-01-10 13:16:32 +00:00
|
|
|
return Period.unwrap(a) < Period.unwrap(b);
|
|
|
|
}
|
|
|
|
|
2023-01-19 15:47:29 +00:00
|
|
|
function _isAfter(Period a, Period b) internal pure returns (bool) {
|
|
|
|
return _isBefore(b, a);
|
2023-01-10 13:16:32 +00:00
|
|
|
}
|
|
|
|
}
|