在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):bokkypoobah/BokkyPooBahsDateTimeLibrary开源软件地址(OpenSource Url):https://github.com/bokkypoobah/BokkyPooBahsDateTimeLibrary开源编程语言(OpenSource Language):Solidity 60.2%开源软件介绍(OpenSource Introduction):BokkyPooBah's DateTime LibraryA gas-efficient Solidity date and time library. Instead of using loops and lookup tables, this date conversions library uses formulae to convert year/month/day hour:minute:second to a Unix timestamp and back. See BokkyPooBah’s Gas-Efficient Solidity DateTime Library for more explanations. Thank you to Alex Kampa, James Zaki and Adrian Guerrera for helping to validate this library. Thanks also to Oleksii Matiiasevych for asking about leap seconds. Table Of Contents
History
Bug Bounty Scope And DonationsDetails of the bug bounty program for this project can be found at BokkyPooBah's Hall Of Fame And Bug Bounties. Please consider donating to support the bug bounty, and the development and maintenance of decentralised applications. The scope of the bug bounty for this project follows: Deploymentv1.00 of the source code for BokkyPooBahsDateTimeContract.sol and TestDateTime.sol has been deployed to:
For each of the deployed contracts above, you can click on the Read Contract tab to test out the date/time/timestamp functions. Questions And Answers
Questions by |
Unit | Range | Notes |
---|---|---|
timestamp | >= 0 | Unix timestamp, number of seconds since 1970/01/01 00:00:00 UTC |
year | 1970 ... 2345 | |
month | 1 ... 12 | |
day | 1 ... 31 | |
hour | 0 ... 23 | |
minute | 0 ... 59 | |
second | 0 ... 59 | |
dayOfWeek | 1 ... 7 | 1 = Monday, ..., 7 = Sunday |
year/month/day | 1970/01/01 ... 2345/12/31 |
_days
, _months
and _years
variable names are _
-prefixed as the non-prefixed versions are reserved words in Solidity.
All functions operate on the uint
timestamp data type, except for functions prefixed with _
.
Calculate the number of days _days
from 1970/01/01 to year
/month
/day
.
function _daysFromDate(uint year, uint month, uint day) public pure returns (uint _days)
NOTE This function does not validate the year
/month
/day
input. Use isValidDate(...)
to validate the input if necessary.
Calculate year
/month
/day
from the number of days _days
since 1970/01/01 .
function _daysToDate(uint _days) public pure returns (uint year, uint month, uint day)
Calculate the timestamp
to year
/month
/day
.
function timestampFromDate(uint year, uint month, uint day) public pure returns (uint timestamp)
NOTE This function does not validate the year
/month
/day
input. Use isValidDate(...)
to validate the input if necessary.
Calculate the timestamp
to year
/month
/day
hour
:minute
:second
UTC.
function timestampFromDateTime(uint year, uint month, uint day, uint hour, uint minute, uint second) public pure returns (uint timestamp)
NOTE This function does not validate the year
/month
/day
hour
:minute
:second
input. Use isValidDateTime(...)
to validate the input if necessary.
Calculate year
/month
/day
from timestamp
.
function timestampToDate(uint timestamp) public pure returns (uint year, uint month, uint day)
Calculate year
/month
/day
hour
:minute
:second
from timestamp
.
function timestampToDateTime(uint timestamp) public pure returns (uint year, uint month, uint day, uint hour, uint minute, uint second)
Is the date specified by year
/month
/day
a valid date?
function isValidDate(uint year, uint month, uint day) internal pure returns (bool valid)
Is the date/time specified by year
/month
/day
hour
:minute
:second
a valid date/time?
function isValidDateTime(uint year, uint month, uint day, uint hour, uint minute, uint second) internal pure returns (bool valid)
Is the year specified by timestamp
a leap year?
function isLeapYear(uint timestamp) public pure returns (bool leapYear)
Is the specified year
(e.g. 2018) a leap year?
function _isLeapYear(uint year) public pure returns (bool leapYear)
Is the day specified by timestamp
a weekday (Monday, ..., Friday)?
function isWeekDay(uint timestamp) public pure returns (bool weekDay)
Is the day specified by timestamp
a weekend (Saturday, Sunday)?
function isWeekEnd(uint timestamp) public pure returns (bool weekEnd)
Return the day in the month daysInMonth
for the month specified by timestamp
.
function getDaysInMonth(uint timestamp) public pure returns (uint daysInMonth)
Return the day in the month daysInMonth
(1, ..., 31) for the month specified by the year
/month
.
function _getDaysInMonth(uint year, uint month) public pure returns (uint daysInMonth)
Return the day of the week dayOfWeek
(1 = Monday, ..., 7 = Sunday) for the date specified by timestamp
.
function getDayOfWeek(uint timestamp) public pure returns (uint dayOfWeek)
Get the year
of the date specified by timestamp
.
function getYear(uint timestamp) public pure returns (uint year)
Get the month
of the date specified by timestamp
.
function getMonth(uint timestamp) public pure returns (uint month)
Get the day of the month day
(1, ..., 31) of the date specified timestamp
.
function getDay(uint timestamp) public pure returns (uint day)
Get the hour
of the date and time specified by timestamp
.
function getHour(uint timestamp) public pure returns (uint hour)
Get the minute
of the date and time specified by timestamp
.
function getMinute(uint timestamp) public pure returns (uint minute)
Get the second
of the date and time specified by timestamp
.
function getSecond(uint timestamp) public pure returns (uint second)
Add _years
years to the date and time specified by timestamp
.
Note that the resulting day of the month will be adjusted if it exceeds the valid number of days in the month. For example, if the original date is 2020/02/29 and an additional year is added to this date, the resulting date will be an invalid date of 2021/02/29. The resulting date is then adjusted to 2021/02/28.
function addYears(uint timestamp, uint _years) public pure returns (uint newTimestamp)
Add _months
months to the date and time specified by timestamp
.
Note that the resulting day of the month will be adjusted if it exceeds the valid number of days in the month. For example, if the original date is 2019/01/31 and an additional month is added to this date, the resulting date will be an invalid date of 2019/02/31. The resulting date is then adjusted to 2019/02/28.
function addMonths(uint timestamp, uint _months) public pure returns (uint newTimestamp)
Add _days
days to the date and time specified by timestamp
.
function addDays(uint timestamp, uint _days) public pure returns (uint newTimestamp)
Add _hours
hours to the date and time specified by timestamp
.
function addHours(uint timestamp, uint _hours) public pure returns (uint newTimestamp)
Add _minutes
minutes to the date and time specified by timest
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论