Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DateTimeUtils - Helper logic to handle time #1531

Open
rmeissner opened this issue Nov 30, 2018 · 13 comments · May be fixed by #3255
Open

DateTimeUtils - Helper logic to handle time #1531

rmeissner opened this issue Nov 30, 2018 · 13 comments · May be fixed by #3255
Labels
contracts Smart contract code. feature New contracts, functions, or helpers.

Comments

@rmeissner
Copy link

🧐 Motivation
Time is important and off-chain there are often cases where time related checks need to be performed. To handle this on chain it would be nice to have some utils that would encapsulate these checks.

There are many use cases for this:

  • Assuming we have a multisignature wallet that should only require the approval of all owners if more than a specified amount should be transferred (see https://github.com/gnosis/MultiSigWallet/blob/master/contracts/MultiSigWalletWithDailyLimit.sol). In the linked contract this limit is always the "in the last 24h" but it would be interesting to map this to a calendar day.
  • Assuming we want to perform recurring transfer (see https://groundhog.network/) it makes sense to limit these to calendar dates (On the 1st of November I pay my monthly zeppelin subscription, or on the 13th of November between 1pm and 2pm the company pays me my salary).

📝 Details
There is a library that is already implementing this logic (https://github.com/pipermerriam/ethereum-datetime), but this is not really up to date, nor audited.

Also it would be interesting to consider edge cases like leap seconds (at least mention why they are not implemented).

An api should include method to check if what year/ month/ day/ hour/ minute a certain timestamp is.

@nventuro nventuro added feature New contracts, functions, or helpers. kind:discussion contracts Smart contract code. labels Nov 30, 2018
@nventuro
Copy link
Contributor

A point we'll have to address if attempting to cater to a 'calendar day' usage will be that of timezones, which may be a major issue due to daylight savings, etc.

@rmeissner
Copy link
Author

Yes that is true. I would probably assume UTC for simplicity and it would be possible to shift the time (e.g UTC+1 or UTC-10)

@frangio
Copy link
Contributor

frangio commented Dec 1, 2018

Leap seconds are dealt with at the EVM level. The unix timestamp that is obtained from block.timestamp doesn't count real seconds, it counts "civil" seconds. Notice in this table how the same timestamp ending in 800 actually spans two real seconds.

@nventuro nventuro added this to the v2.2 milestone Dec 10, 2018
@frangio frangio removed this from the v2.2 milestone Jan 9, 2019
@nventuro nventuro mentioned this issue Mar 8, 2019
@nventuro
Copy link
Contributor

nventuro commented Mar 8, 2019

In #533, the case was made for extending this to also include support for a cleaner Date type. According to Solidity developers here, this should be done by us, not the language.

@alcuadrado
Copy link

alcuadrado commented Apr 9, 2019

#533 is quite old, and there's some discussion in that issue related to the security/correctness of using block.timestamp. I think that discussion is not relevant anymore, but the original proposal by @martriay is still relevant.

There's some ambiguity in #533, as it is not clear if it proposes Date to be a function or a type because of the capital D. I think it was intended to be a function, which IMO would be a great starting point.

@alcuadrado
Copy link

The reason I commented on this issue is that while auditing a smart contract I found errors in the way dates were computed.

The contract I was auditing had to lock some tokens until a certain date. It computed dates with now + offset. This has two problems: (1) it depends on the deployment date. (2) the offset is easy to get wrong (spoiler: it was wrong).

I'd love to recommend the authors to use a human-readable representation of the dates, but I couldn't find a maintained function/lib for doing so. Having something like dateToTimestamp(year, month, day) would be enough for this use-case.

@frangio
Copy link
Contributor

frangio commented Apr 9, 2019

So, functions to create timestamps from human dates: date(2019, 06, 12), datetime(2019, 06, 12, 00, 00). Sounds quite reasonable and useful.

Would these functions return the uint256 timestamp directly, or a struct with operations?

Also, how should we deal with timezones?


(2) the offset is easy to get wrong (spoiler: it was wrong).

@alcuadrado Mind sharing how it was wrong exactly?

@nventuro
Copy link
Contributor

nventuro commented Apr 9, 2019

Timezones are a mess, and DST only complicates matters further. I'd make it so that times specified in either UTC, or at a fixed offset (e.g. UTC+8 for Hong Kong, UTC-4 for Chile, etc.).

@alcuadrado
Copy link

Would these functions return the uint256 timestamp directly, or a struct with operations?

I think functions would be more efficient.

Btw, some of the functions in this library are also super useful.

Also, how should we deal with timezones?

I'd only do UTC. I don't see a way of managing timezones trustlessly, as any government can change their country's timezone whenever they want.

@alcuadrado Mind sharing how it was wrong exactly?

The offset was expressed as <base> * <units multiplier>, and the <unit multiplier> was wrong. Still, if it weren't wrong, reading it is way harder than a normal date (i.e. year, month & day).

@frangio frangio added the needs milestone Interesting features or improvements that are not yet assigned to a milestone. label Apr 11, 2019
@princesinha19
Copy link
Contributor

@nventuro @frangio Guys, Any progress on this issue?

@frangio frangio removed the needs milestone Interesting features or improvements that are not yet assigned to a milestone. label May 27, 2020
@frangio
Copy link
Contributor

frangio commented May 27, 2020

We're interested in adding this to the library. No one has started working on it yet.

This looks like good information for whoever wants to work on this: Low-Level Date Algorithms

@pasevin
Copy link

pasevin commented Sep 8, 2021

Is this even under consideration?

@frangio
Copy link
Contributor

frangio commented Sep 16, 2021

The team is not proactively working on this feature but we would be happy to receive a PR implementing the interface I described above:

functions to create timestamps from human dates: date(2019, 06, 12), datetime(2019, 06, 12, 00, 00)

lesserhatch added a commit to lesserhatch/openzeppelin-contracts that referenced this issue Mar 10, 2022
Add date and datetime utility functions to convert from civil date
time to Unix time.
lesserhatch added a commit to lesserhatch/openzeppelin-contracts that referenced this issue Mar 10, 2022
Add date and datetime utility functions to convert from civil date
time to Unix time.
lesserhatch added a commit to lesserhatch/openzeppelin-contracts that referenced this issue Mar 10, 2022
Add date and datetime utility functions to convert from civil date
time to Unix time.
lesserhatch added a commit to lesserhatch/openzeppelin-contracts that referenced this issue Mar 10, 2022
Add date and datetime utility functions to convert from civil date
time to Unix time.
@lesserhatch lesserhatch linked a pull request Mar 10, 2022 that will close this issue
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contracts Smart contract code. feature New contracts, functions, or helpers.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants