• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

bokkypoobah/BokkyPooBahsDateTimeLibrary: Gas-Efficient Solidity DateTime Library

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

bokkypoobah/BokkyPooBahsDateTimeLibrary

开源软件地址(OpenSource Url):

https://github.com/bokkypoobah/BokkyPooBahsDateTimeLibrary

开源编程语言(OpenSource Language):

Solidity 60.2%

开源软件介绍(OpenSource Introduction):



BokkyPooBah's DateTime Library

A 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

Version Date Notes
v1.00-pre-release May 25 2018 "Rarefaction" pre-release. I'm currently trying to get this library audited, so don't use in production mode yet.
v1.00-pre-release-a Jun 2 2018 "Rarefaction" pre-release a. Added the contracts/BokkyPooBahsDateTimeContract.sol wrapper for convenience.
Alex Kampa conducted a range of tests on the library.
v1.00-pre-release-b Jun 4 2018 "Rarefaction" pre-release b. Replaced public function with internal for easier EtherScan verification - a83e13b.
Deployed contracts/BokkyPooBahsDateTimeContract.sol with the inlined contracts/BokkyPooBahsDateTimeLibrary.sol to the Ropsten network at address 0x07239bb079094481bfaac91ca842426860021aaa
v1.00-pre-release-c June 8 2018 "Rarefaction" pre-release c. Added require(year >= 1970) to _daysFromDate(...) in 4002b27 as highlighted in James Zaki's audit
v1.00-pre-release-d Sep 1 2018 "Rarefaction" pre-release d. Added isValidDate(...) and isValidDateTime(...) in 380061b as highlighted in Adrian Guerrera's audit
v1.00 Sep 2 2018 "Rarefaction" release
v1.01 Feb 14 2019 "Notoryctes" release. Upgraded contracts to Solidity 0.5.x.
Updated to MIT Licence
v1.01 Feb 17 2019 Bug bounty added


Bug Bounty Scope And Donations

Details 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:



Deployment

v1.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 _dredge

User /u/_dredge asked the following questions:

Some Muslim countries have a Friday/Saturday weekend. Workday(1-7) may be more useful.

I presume leap seconds and such details are taken care of in the Unix timecode.

Some additional ideas for functions below.

Quarter calculations

weekNumber(1-53)

Complete years / months / weeks / days

Nearest years / months / weeks / days

Regarding regions and systems where Friday/Saturday are weekends, please use the function getDayOfWeek(timestamp) that returns 1 (= Monday, ..., 7 (= Sunday) to determine whether you should treat a day as a weekday or weekend.

See the next question regarding the leap seconds.

Regarding the additional ideas, thanks!


What about the leap second?

Asked by Oleksii Matiiasevych and /u/_dredge above.

For example, a leap second was inserted on Jan 01 1999.

From the first answer to Unix time and leap seconds:

The number of seconds per day are fixed with Unix timestamps.

The Unix time number is zero at the Unix epoch, and increases by exactly 86400 per day since the epoch.

So it cannot represent leap seconds. The OS will slow down the clock to accommodate for this. The leap seconds is simply not existent as far a Unix timestamps are concerned.

And from the second answer to Unix time and leap seconds:

Unix time is easy to work with, but some timestamps are not real times, and some timestamps are not unique times.

That is, there are some duplicate timestamps representing two different seconds in time, because in unix time the sixtieth second might have to repeat itself (as there can't be a sixty-first second). Theoretically, they could also be gaps in the future because the sixtieth second doesn't have to exist, although no skipping leap seconds have been issued so far.

Rationale for unix time: it's defined so that it's easy to work with. Adding support for leap seconds to the standard libraries is very tricky. ...

This library aims to replicate the Unix time functionality and assumes that leap seconds are handled by the underlying operating system.


What is the maximum year 2345?

Asked by Adrian Guerrera.

2345 is just an arbitrary number chosen for the year limit to test to. The algorithms should still work beyond this date.


Why are there no input validations to some of the functions?

Asked by Adrian Guerrera. Specifically, the functions _daysFromDate, timestampFromDate and timestampFromDateTime.

The date and time inputs should be validated before the values are passed to these functions. The validation functions isValidDate(...) and isValidDateTime(...) have now been added for this purpose.


Why are all variables 256-bit integers?

This library provides a cheap conversion between the timestamp and year/month/day hour:minute:second formats. There is no requirement for this library to store a packed structure to represent a DateTime as this data can be stored as a uint256 and converted on the fly to year/month/day hour:minute:second.


Why do you call this a gas-efficient library?

The formulae for converting between a timestamp and year/month/day hour:minute:second format uses a mathematically simple algorithm without any loops. The gas cost is relatively constant (as there are no loops) and the mathematical computations are relative cheap (compared to using loops and looking up data from storage).


Can this library be written more efficiently?

Most likely. The aim of this first version is for the conversions to be computed correctly.


How gas-efficient is this library?

From Gas Cost, timestampToDateTime(…) has an execution gas cost of 3,101 gas, and timestampFromDateTime(…) has an execution gas cost of 2,566 gas.



Conventions

All dates, times and Unix timestamps are UTC.

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 _.



Functions

_daysFromDate

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.


_daysToDate

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)

timestampFromDate

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.


timestampFromDateTime

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.


timestampToDate

Calculate year/month/day from timestamp.

function timestampToDate(uint timestamp) public pure returns (uint year, uint month, uint day)

timestampToDateTime

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)

isValidDate

Is the date specified by year/month/day a valid date?

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


isValidDateTime

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)

isLeapYear

Is the year specified by timestamp a leap year?

function isLeapYear(uint timestamp) public pure returns (bool leapYear)

_isLeapYear

Is the specified year (e.g. 2018) a leap year?

function _isLeapYear(uint year) public pure returns (bool leapYear)

isWeekDay

Is the day specified by timestamp a weekday (Monday, ..., Friday)?

function isWeekDay(uint timestamp) public pure returns (bool weekDay)

isWeekEnd

Is the day specified by timestamp a weekend (Saturday, Sunday)?

function isWeekEnd(uint timestamp) public pure returns (bool weekEnd)

getDaysInMonth

Return the day in the month daysInMonth for the month specified by timestamp.

function getDaysInMonth(uint timestamp) public pure returns (uint daysInMonth)

_getDaysInMonth

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)

getDayOfWeek

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)

getYear

Get the year of the date specified by timestamp.

function getYear(uint timestamp) public pure returns (uint year)

getMonth

Get the month of the date specified by timestamp.

function getMonth(uint timestamp) public pure returns (uint month)

getDay

Get the day of the month day (1, ..., 31) of the date specified timestamp.

function getDay(uint timestamp) public pure returns (uint day)

getHour

Get the hour of the date and time specified by timestamp.

function getHour(uint timestamp) public pure returns (uint hour)

getMinute

Get the minute of the date and time specified by timestamp.

function getMinute(uint timestamp) public pure returns (uint minute)

getSecond

Get the second of the date and time specified by timestamp.

function getSecond(uint timestamp) public pure returns (uint second)

addYears

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)

addMonths

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)

addDays

Add _days days to the date and time specified by timestamp.

function addDays(uint timestamp, uint _days) public pure returns (uint newTimestamp)

addHours

Add _hours hours to the date and time specified by timestamp.

function addHours(uint timestamp, uint _hours) public pure returns (uint newTimestamp)

addMinutes

Add _minutes minutes to the date and time specified by timest


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap