Skip to content

Latest commit

 

History

History
1309 lines (1062 loc) · 32.9 KB

BokkyPooBahsDateTimeLibrary.md

File metadata and controls

1309 lines (1062 loc) · 32.9 KB

BokkyPooBahsDateTimeLibrary.sol

View Source: contracts/libraries/BokkyPooBahsDateTimeLibrary.sol

BokkyPooBahsDateTimeLibrary

Contract Members

Constants & Variables

uint256 internal constant SECONDS_PER_DAY;
uint256 internal constant SECONDS_PER_HOUR;
uint256 internal constant SECONDS_PER_MINUTE;
int256 internal constant OFFSET19700101;
uint256 internal constant DOW_MON;
uint256 internal constant DOW_TUE;
uint256 internal constant DOW_WED;
uint256 internal constant DOW_THU;
uint256 internal constant DOW_FRI;
uint256 internal constant DOW_SAT;
uint256 internal constant DOW_SUN;

Functions

_daysFromDate

function _daysFromDate(uint256 year, uint256 month, uint256 day) internal pure
returns(_days uint256)

Arguments

Name Type Description
year uint256
month uint256
day uint256
Source Code
function _daysFromDate(
    uint256 year,
    uint256 month,
    uint256 day
  ) internal pure returns (uint256 _days) {
    require(year >= 1970);
    int256 _year = int256(year);
    int256 _month = int256(month);
    int256 _day = int256(day);

    int256 __days = _day -
      32075 +
      (1461 * (_year + 4800 + (_month - 14) / 12)) /
      4 +
      (367 * (_month - 2 - ((_month - 14) / 12) * 12)) /
      12 -
      (3 * ((_year + 4900 + (_month - 14) / 12) / 100)) /
      4 -
      OFFSET19700101;

    _days = uint256(__days);
  }

_daysToDate

function _daysToDate(uint256 _days) internal pure
returns(year uint256, month uint256, day uint256)

Arguments

Name Type Description
_days uint256
Source Code
function _daysToDate(uint256 _days)
    internal
    pure
    returns (
      uint256 year,
      uint256 month,
      uint256 day
    )
  {
    int256 __days = int256(_days);

    int256 L = __days + 68569 + OFFSET19700101;
    int256 N = (4 * L) / 146097;
    L = L - (146097 * N + 3) / 4;
    int256 _year = (4000 * (L + 1)) / 1461001;
    L = L - (1461 * _year) / 4 + 31;
    int256 _month = (80 * L) / 2447;
    int256 _day = L - (2447 * _month) / 80;
    L = _month / 11;
    _month = _month + 2 - 12 * L;
    _year = 100 * (N - 49) + _year + L;

    year = uint256(_year);
    month = uint256(_month);
    day = uint256(_day);
  }

timestampFromDate

function timestampFromDate(uint256 year, uint256 month, uint256 day) internal pure
returns(timestamp uint256)

Arguments

Name Type Description
year uint256
month uint256
day uint256
Source Code
function timestampFromDate(
    uint256 year,
    uint256 month,
    uint256 day
  ) internal pure returns (uint256 timestamp) {
    timestamp = _daysFromDate(year, month, day) * SECONDS_PER_DAY;
  }

timestampFromDateTime

function timestampFromDateTime(uint256 year, uint256 month, uint256 day, uint256 hour, uint256 minute, uint256 second) internal pure
returns(timestamp uint256)

Arguments

Name Type Description
year uint256
month uint256
day uint256
hour uint256
minute uint256
second uint256
Source Code
function timestampFromDateTime(
    uint256 year,
    uint256 month,
    uint256 day,
    uint256 hour,
    uint256 minute,
    uint256 second
  ) internal pure returns (uint256 timestamp) {
    timestamp = _daysFromDate(year, month, day) * SECONDS_PER_DAY + hour * SECONDS_PER_HOUR + minute * SECONDS_PER_MINUTE + second;
  }

timestampToDate

function timestampToDate(uint256 timestamp) internal pure
returns(year uint256, month uint256, day uint256)

Arguments

Name Type Description
timestamp uint256
Source Code
function timestampToDate(uint256 timestamp)
    internal
    pure
    returns (
      uint256 year,
      uint256 month,
      uint256 day
    )
  {
    (year, month, day) = _daysToDate(timestamp / SECONDS_PER_DAY);
  }

timestampToDateTime

function timestampToDateTime(uint256 timestamp) internal pure
returns(year uint256, month uint256, day uint256, hour uint256, minute uint256, second uint256)

Arguments

Name Type Description
timestamp uint256
Source Code
function timestampToDateTime(uint256 timestamp)
    internal
    pure
    returns (
      uint256 year,
      uint256 month,
      uint256 day,
      uint256 hour,
      uint256 minute,
      uint256 second
    )
  {
    (year, month, day) = _daysToDate(timestamp / SECONDS_PER_DAY);
    uint256 secs = timestamp % SECONDS_PER_DAY;
    hour = secs / SECONDS_PER_HOUR;
    secs = secs % SECONDS_PER_HOUR;
    minute = secs / SECONDS_PER_MINUTE;
    second = secs % SECONDS_PER_MINUTE;
  }

isValidDate

function isValidDate(uint256 year, uint256 month, uint256 day) internal pure
returns(valid bool)

Arguments

Name Type Description
year uint256
month uint256
day uint256
Source Code
function isValidDate(
    uint256 year,
    uint256 month,
    uint256 day
  ) internal pure returns (bool valid) {
    if (year >= 1970 && month > 0 && month <= 12) {
      uint256 daysInMonth = _getDaysInMonth(year, month);
      if (day > 0 && day <= daysInMonth) {
        valid = true;
      }
    }
  }

isValidDateTime

function isValidDateTime(uint256 year, uint256 month, uint256 day, uint256 hour, uint256 minute, uint256 second) internal pure
returns(valid bool)

Arguments

Name Type Description
year uint256
month uint256
day uint256
hour uint256
minute uint256
second uint256
Source Code
function isValidDateTime(
    uint256 year,
    uint256 month,
    uint256 day,
    uint256 hour,
    uint256 minute,
    uint256 second
  ) internal pure returns (bool valid) {
    if (isValidDate(year, month, day)) {
      if (hour < 24 && minute < 60 && second < 60) {
        valid = true;
      }
    }
  }

isLeapYear

function isLeapYear(uint256 timestamp) internal pure
returns(leapYear bool)

Arguments

Name Type Description
timestamp uint256
Source Code
function isLeapYear(uint256 timestamp) internal pure returns (bool leapYear) {
    (uint256 year, , ) = _daysToDate(timestamp / SECONDS_PER_DAY);
    leapYear = _isLeapYear(year);
  }

_isLeapYear

function _isLeapYear(uint256 year) internal pure
returns(leapYear bool)

Arguments

Name Type Description
year uint256
Source Code
function _isLeapYear(uint256 year) internal pure returns (bool leapYear) {
    leapYear = ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
  }

isWeekDay

function isWeekDay(uint256 timestamp) internal pure
returns(weekDay bool)

Arguments

Name Type Description
timestamp uint256
Source Code
function isWeekDay(uint256 timestamp) internal pure returns (bool weekDay) {
    weekDay = getDayOfWeek(timestamp) <= DOW_FRI;
  }

isWeekEnd

function isWeekEnd(uint256 timestamp) internal pure
returns(weekEnd bool)

Arguments

Name Type Description
timestamp uint256
Source Code
function isWeekEnd(uint256 timestamp) internal pure returns (bool weekEnd) {
    weekEnd = getDayOfWeek(timestamp) >= DOW_SAT;
  }

getDaysInMonth

function getDaysInMonth(uint256 timestamp) internal pure
returns(daysInMonth uint256)

Arguments

Name Type Description
timestamp uint256
Source Code
function getDaysInMonth(uint256 timestamp) internal pure returns (uint256 daysInMonth) {
    (uint256 year, uint256 month, ) = _daysToDate(timestamp / SECONDS_PER_DAY);
    daysInMonth = _getDaysInMonth(year, month);
  }

_getDaysInMonth

function _getDaysInMonth(uint256 year, uint256 month) internal pure
returns(daysInMonth uint256)

Arguments

Name Type Description
year uint256
month uint256
Source Code
function _getDaysInMonth(uint256 year, uint256 month) internal pure returns (uint256 daysInMonth) {
    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
      daysInMonth = 31;
    } else if (month != 2) {
      daysInMonth = 30;
    } else {
      daysInMonth = _isLeapYear(year) ? 29 : 28;
    }
  }

getDayOfWeek

function getDayOfWeek(uint256 timestamp) internal pure
returns(dayOfWeek uint256)

Arguments

Name Type Description
timestamp uint256
Source Code
function getDayOfWeek(uint256 timestamp) internal pure returns (uint256 dayOfWeek) {
    uint256 _days = timestamp / SECONDS_PER_DAY;
    dayOfWeek = ((_days + 3) % 7) + 1;
  }

getYear

function getYear(uint256 timestamp) internal pure
returns(year uint256)

Arguments

Name Type Description
timestamp uint256
Source Code
function getYear(uint256 timestamp) internal pure returns (uint256 year) {
    (year, , ) = _daysToDate(timestamp / SECONDS_PER_DAY);
  }

getMonth

function getMonth(uint256 timestamp) internal pure
returns(month uint256)

Arguments

Name Type Description
timestamp uint256
Source Code
function getMonth(uint256 timestamp) internal pure returns (uint256 month) {
    (, month, ) = _daysToDate(timestamp / SECONDS_PER_DAY);
  }

getDay

function getDay(uint256 timestamp) internal pure
returns(day uint256)

Arguments

Name Type Description
timestamp uint256
Source Code
function getDay(uint256 timestamp) internal pure returns (uint256 day) {
    (, , day) = _daysToDate(timestamp / SECONDS_PER_DAY);
  }

getHour

function getHour(uint256 timestamp) internal pure
returns(hour uint256)

Arguments

Name Type Description
timestamp uint256
Source Code
function getHour(uint256 timestamp) internal pure returns (uint256 hour) {
    uint256 secs = timestamp % SECONDS_PER_DAY;
    hour = secs / SECONDS_PER_HOUR;
  }

getMinute

function getMinute(uint256 timestamp) internal pure
returns(minute uint256)

Arguments

Name Type Description
timestamp uint256
Source Code
function getMinute(uint256 timestamp) internal pure returns (uint256 minute) {
    uint256 secs = timestamp % SECONDS_PER_HOUR;
    minute = secs / SECONDS_PER_MINUTE;
  }

getSecond

function getSecond(uint256 timestamp) internal pure
returns(second uint256)

Arguments

Name Type Description
timestamp uint256
Source Code
function getSecond(uint256 timestamp) internal pure returns (uint256 second) {
    second = timestamp % SECONDS_PER_MINUTE;
  }

addYears

function addYears(uint256 timestamp, uint256 _years) internal pure
returns(newTimestamp uint256)

Arguments

Name Type Description
timestamp uint256
_years uint256
Source Code
function addYears(uint256 timestamp, uint256 _years) internal pure returns (uint256 newTimestamp) {
    (uint256 year, uint256 month, uint256 day) = _daysToDate(timestamp / SECONDS_PER_DAY);
    year += _years;
    uint256 daysInMonth = _getDaysInMonth(year, month);
    if (day > daysInMonth) {
      day = daysInMonth;
    }
    newTimestamp = _daysFromDate(year, month, day) * SECONDS_PER_DAY + (timestamp % SECONDS_PER_DAY);
    require(newTimestamp >= timestamp);
  }

addMonths

function addMonths(uint256 timestamp, uint256 _months) internal pure
returns(newTimestamp uint256)

Arguments

Name Type Description
timestamp uint256
_months uint256
Source Code
function addMonths(uint256 timestamp, uint256 _months) internal pure returns (uint256 newTimestamp) {
    (uint256 year, uint256 month, uint256 day) = _daysToDate(timestamp / SECONDS_PER_DAY);
    month += _months;
    year += (month - 1) / 12;
    month = ((month - 1) % 12) + 1;
    uint256 daysInMonth = _getDaysInMonth(year, month);
    if (day > daysInMonth) {
      day = daysInMonth;
    }
    newTimestamp = _daysFromDate(year, month, day) * SECONDS_PER_DAY + (timestamp % SECONDS_PER_DAY);
    require(newTimestamp >= timestamp);
  }

addDays

function addDays(uint256 timestamp, uint256 _days) internal pure
returns(newTimestamp uint256)

Arguments

Name Type Description
timestamp uint256
_days uint256
Source Code
function addDays(uint256 timestamp, uint256 _days) internal pure returns (uint256 newTimestamp) {
    newTimestamp = timestamp + _days * SECONDS_PER_DAY;
    require(newTimestamp >= timestamp);
  }

addHours

function addHours(uint256 timestamp, uint256 _hours) internal pure
returns(newTimestamp uint256)

Arguments

Name Type Description
timestamp uint256
_hours uint256
Source Code
function addHours(uint256 timestamp, uint256 _hours) internal pure returns (uint256 newTimestamp) {
    newTimestamp = timestamp + _hours * SECONDS_PER_HOUR;
    require(newTimestamp >= timestamp);
  }

addMinutes

function addMinutes(uint256 timestamp, uint256 _minutes) internal pure
returns(newTimestamp uint256)

Arguments

Name Type Description
timestamp uint256
_minutes uint256
Source Code
function addMinutes(uint256 timestamp, uint256 _minutes) internal pure returns (uint256 newTimestamp) {
    newTimestamp = timestamp + _minutes * SECONDS_PER_MINUTE;
    require(newTimestamp >= timestamp);
  }

addSeconds

function addSeconds(uint256 timestamp, uint256 _seconds) internal pure
returns(newTimestamp uint256)

Arguments

Name Type Description
timestamp uint256
_seconds uint256
Source Code
function addSeconds(uint256 timestamp, uint256 _seconds) internal pure returns (uint256 newTimestamp) {
    newTimestamp = timestamp + _seconds;
    require(newTimestamp >= timestamp);
  }

subYears

function subYears(uint256 timestamp, uint256 _years) internal pure
returns(newTimestamp uint256)

Arguments

Name Type Description
timestamp uint256
_years uint256
Source Code
function subYears(uint256 timestamp, uint256 _years) internal pure returns (uint256 newTimestamp) {
    (uint256 year, uint256 month, uint256 day) = _daysToDate(timestamp / SECONDS_PER_DAY);
    year -= _years;
    uint256 daysInMonth = _getDaysInMonth(year, month);
    if (day > daysInMonth) {
      day = daysInMonth;
    }
    newTimestamp = _daysFromDate(year, month, day) * SECONDS_PER_DAY + (timestamp % SECONDS_PER_DAY);
    require(newTimestamp <= timestamp);
  }

subMonths

function subMonths(uint256 timestamp, uint256 _months) internal pure
returns(newTimestamp uint256)

Arguments

Name Type Description
timestamp uint256
_months uint256
Source Code
function subMonths(uint256 timestamp, uint256 _months) internal pure returns (uint256 newTimestamp) {
    (uint256 year, uint256 month, uint256 day) = _daysToDate(timestamp / SECONDS_PER_DAY);
    uint256 yearMonth = year * 12 + (month - 1) - _months;
    year = yearMonth / 12;
    month = (yearMonth % 12) + 1;
    uint256 daysInMonth = _getDaysInMonth(year, month);
    if (day > daysInMonth) {
      day = daysInMonth;
    }
    newTimestamp = _daysFromDate(year, month, day) * SECONDS_PER_DAY + (timestamp % SECONDS_PER_DAY);
    require(newTimestamp <= timestamp);
  }

subDays

function subDays(uint256 timestamp, uint256 _days) internal pure
returns(newTimestamp uint256)

Arguments

Name Type Description
timestamp uint256
_days uint256
Source Code
function subDays(uint256 timestamp, uint256 _days) internal pure returns (uint256 newTimestamp) {
    newTimestamp = timestamp - _days * SECONDS_PER_DAY;
    require(newTimestamp <= timestamp);
  }

subHours

function subHours(uint256 timestamp, uint256 _hours) internal pure
returns(newTimestamp uint256)

Arguments

Name Type Description
timestamp uint256
_hours uint256
Source Code
function subHours(uint256 timestamp, uint256 _hours) internal pure returns (uint256 newTimestamp) {
    newTimestamp = timestamp - _hours * SECONDS_PER_HOUR;
    require(newTimestamp <= timestamp);
  }

subMinutes

function subMinutes(uint256 timestamp, uint256 _minutes) internal pure
returns(newTimestamp uint256)

Arguments

Name Type Description
timestamp uint256
_minutes uint256
Source Code
function subMinutes(uint256 timestamp, uint256 _minutes) internal pure returns (uint256 newTimestamp) {
    newTimestamp = timestamp - _minutes * SECONDS_PER_MINUTE;
    require(newTimestamp <= timestamp);
  }

subSeconds

function subSeconds(uint256 timestamp, uint256 _seconds) internal pure
returns(newTimestamp uint256)

Arguments

Name Type Description
timestamp uint256
_seconds uint256
Source Code
function subSeconds(uint256 timestamp, uint256 _seconds) internal pure returns (uint256 newTimestamp) {
    newTimestamp = timestamp - _seconds;
    require(newTimestamp <= timestamp);
  }

diffYears

function diffYears(uint256 fromTimestamp, uint256 toTimestamp) internal pure
returns(_years uint256)

Arguments

Name Type Description
fromTimestamp uint256
toTimestamp uint256
Source Code
function diffYears(uint256 fromTimestamp, uint256 toTimestamp) internal pure returns (uint256 _years) {
    require(fromTimestamp <= toTimestamp);
    (uint256 fromYear, , ) = _daysToDate(fromTimestamp / SECONDS_PER_DAY);
    (uint256 toYear, , ) = _daysToDate(toTimestamp / SECONDS_PER_DAY);
    _years = toYear - fromYear;
  }

diffMonths

function diffMonths(uint256 fromTimestamp, uint256 toTimestamp) internal pure
returns(_months uint256)

Arguments

Name Type Description
fromTimestamp uint256
toTimestamp uint256
Source Code
function diffMonths(uint256 fromTimestamp, uint256 toTimestamp) internal pure returns (uint256 _months) {
    require(fromTimestamp <= toTimestamp);
    (uint256 fromYear, uint256 fromMonth, ) = _daysToDate(fromTimestamp / SECONDS_PER_DAY);
    (uint256 toYear, uint256 toMonth, ) = _daysToDate(toTimestamp / SECONDS_PER_DAY);
    _months = toYear * 12 + toMonth - fromYear * 12 - fromMonth;
  }

diffDays

function diffDays(uint256 fromTimestamp, uint256 toTimestamp) internal pure
returns(_days uint256)

Arguments

Name Type Description
fromTimestamp uint256
toTimestamp uint256
Source Code
function diffDays(uint256 fromTimestamp, uint256 toTimestamp) internal pure returns (uint256 _days) {
    require(fromTimestamp <= toTimestamp);
    _days = (toTimestamp - fromTimestamp) / SECONDS_PER_DAY;
  }

diffHours

function diffHours(uint256 fromTimestamp, uint256 toTimestamp) internal pure
returns(_hours uint256)

Arguments

Name Type Description
fromTimestamp uint256
toTimestamp uint256
Source Code
function diffHours(uint256 fromTimestamp, uint256 toTimestamp) internal pure returns (uint256 _hours) {
    require(fromTimestamp <= toTimestamp);
    _hours = (toTimestamp - fromTimestamp) / SECONDS_PER_HOUR;
  }

diffMinutes

function diffMinutes(uint256 fromTimestamp, uint256 toTimestamp) internal pure
returns(_minutes uint256)

Arguments

Name Type Description
fromTimestamp uint256
toTimestamp uint256
Source Code
function diffMinutes(uint256 fromTimestamp, uint256 toTimestamp) internal pure returns (uint256 _minutes) {
    require(fromTimestamp <= toTimestamp);
    _minutes = (toTimestamp - fromTimestamp) / SECONDS_PER_MINUTE;
  }

diffSeconds

function diffSeconds(uint256 fromTimestamp, uint256 toTimestamp) internal pure
returns(_seconds uint256)

Arguments

Name Type Description
fromTimestamp uint256
toTimestamp uint256
Source Code
function diffSeconds(uint256 fromTimestamp, uint256 toTimestamp) internal pure returns (uint256 _seconds) {
    require(fromTimestamp <= toTimestamp);
    _seconds = toTimestamp - fromTimestamp;
  }

Contracts