-
Notifications
You must be signed in to change notification settings - Fork 137
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
Add basic types for manipulating dates and times #843
Comments
Agreed, it would be nice to add date/time functionality. This is currently not a priority, but contributions are very welcome (happy to assist) |
A small MVP proposal to kickstart the discussion. We can discuss here and then I'll write up a FLIP later. pub struct DateTime {
/// The timestamp representing the DateTime
let timestamp : UFix64
/// Get the year of the date based on the timestamp.
pub fun getYear(): UInt64
/// Get the month of the date based on the timestamp.
pub fun getMonth(): UInt64
/// Get the day of the date based on the timestamp.
pub fun getDay(): UInt64
/// Get the hour of the date based on the timestamp.
pub fun getHour(): UInt64
/// Get the minute of the date based on the timestamp.
pub fun getMinute(): UInt64
/// Get the second of the date based on the timestamp.
pub fun getSecond(): UInt64
/// Mutate the DateTime by adding years to the timestamp.
pub fun addYears(years: UInt64)
/// Mutate the DateTime by adding months to the timestamp.
pub fun addMonths(months: UInt64)
/// Mutate the DateTime by adding days to the timestamp.
pub fun addDays(days: UInt64)
/// Mutate the DateTime by adding hours to the timestamp.
pub fun addHours(hours: UInt64)
/// Mutate the DateTime by adding minutes to the timestamp.
pub fun addMinutes(minutes: UInt64)
/// Mutate the DateTime by adding seconds to the timestamp.
pub fun addSeconds(seconds: UInt64)
}
/// Returns the date time based on the current block timestamp.
DateTime.now()
/// Returns the date time based on the given timestamp.
DateTime.fromTimestamp(timestamp : UFix64)
|
Couple minor suggestions:
|
Good start @darkdrag00nv2! In general, we do not have to reinvent the wheel here and can probably get a lot of inspiration from existing APIs in other languages. For example:
I'm not saying this issue should result in all the functionality of those libraries being made available, but rather just that we can start with some simple data structures, that are inspired by proven solutions. |
Thanks for the suggestions @dsainati1 and @turbolent.
Agreed. I believe we can start with for an MVP:
In future we could add the following depending on requests/need.
Ack on your suggestion 1, 2 (enums and UInt8) and 4 (day of week).
We could support microseconds as well. Python datetime supports it. Yes, seems like block timestamps are measured in seconds based on this snippet. cadence/runtime/stdlib/block.go Line 156 in 32ddd35
I originally didn't plan on supporting time zones in the first phase but can do if needed. |
There's no point IMO in supporting microseconds if the minimum granularity of block timestamps is seconds; we should support the granularity with which blocks are measured and no smaller. |
AFAIK the block time granularity is undefined. Supporting sub-second granularity is maybe unnecessary today, but future-proof |
Possibly I am missing something, but it seems like in order to support any kind of |
This choice can be made by the application/developer, it does not necessarily have to be part of the type itself. Providing types without timezone information and related operations is a good starting point. It will put the burden of handling timezone details (if needed) on developers at first, but we can later add timezone-aware types/functionality. For example, in Java, this is what the |
This is a great overview of pitfalls to avoid when designing the library: https://dev.arie.bovenberg.net/blog/python-datetime-pitfalls/ |
Issue To Be Solved
I would like to include calculations based on elapsed time in my contract. Cadence doesn't currently have a Date/Time type, so for now I'm attempting to roll my own using UFix64 timestamps.
Date manipulation is both difficult and common and seems like a good problem to solve at the language level.
Context
I'm trying to calculate the amount due for a payment based on the difference between a timestamp on the resource and the current time.
For current time, I'm allowing the transaction signer to pass a timestamp and I validate that it is later than the timestamp on the latest block. Ideally I would be able to get the current time from Cadence (for example, by initializing a new Date type).
The text was updated successfully, but these errors were encountered: