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

FLIP 251: Cadence Date and Time #245

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

darkdrag00nv2
Copy link
Contributor

@darkdrag00nv2 darkdrag00nv2 commented Jan 29, 2024

#251

Work towards onflow/cadence#843

@darkdrag00nv2 darkdrag00nv2 changed the title FLIP 245: Cadence Date and time FLIP 245: Cadence Date and Time Jan 29, 2024
@darkdrag00nv2 darkdrag00nv2 marked this pull request as ready for review February 4, 2024 13:33
@darkdrag00nv2
Copy link
Contributor Author

cc: @turbolent @SupunS @dsainati1

@bluesign
Copy link
Collaborator

It is assumed that there are 3600*24 seconds in every day and there are no leap years.

I think ignoring leap years is not a good idea.

@turbolent
Copy link
Member

turbolent commented Feb 19, 2024

It is assumed that there are 3600*24 seconds in every day and there are no leap years.

I think ignoring leap years is not a good idea.

Agreed, we can probably at least support leap years, like e.g. Java's java.time.LocalDate does.

In general, we should not reinvent the wheel here and base the API and implementation of an established solution, like e.g. Java's JSR310

@darkdrag00nv2
Copy link
Contributor Author

In general, we should not reinvent the wheel here and base the API and implementation of an established solution, like e.g. Java's JSR310

Yep, the current proposal is more based on the python datetime module.

I wrongly made the assumption that they don't support leap years 🙁. I was just giving the wrong input with the year 2011 🤦

>>> datetime.datetime(2011, 2, 29)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: day is out of range for month

Trying with an actual leap year works fine.

>>> datetime.datetime(2024, 2, 29)

@KshitijChaudhary666
Copy link
Contributor

Hi @darkdrag00nv2 - this FLIP is not reflected on FLIP project tracker. Did you follow the process outlined in https://github.com/onflow/flips? Specifically please remember to do the following without which the FLIP won't get visibility on the project tracker-

Create an issue by using one of the FLIP issue templates based on the type of the FLIP - application, governance, cadence or protocol. The title of the issue should be the title of your FLIP, e.g., "Dynamic Inclusion fees". Submit the issue. Note the issue number that gets assigned.
Then, create your FLIP as a pull request to this repository (onflow/flips). Use the issue number generated in step 2 as the FLIP number. And mention the FLIP issue by copying the GitHub URL or the issue in the comment section.

Thank you!

@darkdrag00nv2 darkdrag00nv2 changed the title FLIP 245: Cadence Date and Time FLIP 251: Cadence Date and Time Mar 3, 2024
status: draft
flip: 245
authors: darkdrag00n ([email protected])
sponsor: Bastian Müller ([email protected])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
sponsor: Bastian Müller (bastian@dapperlabs.com)
sponsor: Bastian Müller (bastian[email protected])

Copy link
Member

@turbolent turbolent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

As we're finishing work on Cadence 1.0, we can finally come back to proposals like this. Thank you for your patience!

I left some comments and suggestions, and am happy to apply suggested edits and add remaining details. Does that sound good to you @darkdrag00nv2 ?

Once we addressed the outstanding issues, we can schedule to vote on it in the next WG call


- `LocalDateTime.fromTimestamp(timestamp : UFix64)`: Creates a `LocalDateTime` with value based on the passed timestamp argument.

- `LocalDateTime.of(year: UInt64, month: Month, day: UInt8, hour: UInt8, minute: UInt8, second: UInt8)`: Creates a `LocalDateTime` with value based on the passed arguments assuming that there are 3600*24 seconds in every day.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can maybe just be a constructor function. It might be good to improve the naming of day and make it explicit that it is the day of the month, e.g. dayOfMonth:

Suggested change
- `LocalDateTime.of(year: UInt64, month: Month, day: UInt8, hour: UInt8, minute: UInt8, second: UInt8)`: Creates a `LocalDateTime` with value based on the passed arguments assuming that there are 3600*24 seconds in every day.
- `LocalDateTime(year: UInt64, month: Month, dayOfMonth: UInt8, hour: UInt8, minute: UInt8, second: UInt8)`: Creates a `LocalDateTime` with value based on the passed arguments assuming that there are 3600*24 seconds in every day.

Examples below also need to be updated


1. `getYear(): UInt32`: Get the year of the date-time.
2. `getMonth(): Month`: Get the month of the date-time.
3. `getDate(): UInt8`: Get the date of the date-time.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe improve the naming and clarify it is the day of the month, i.e.

Suggested change
3. `getDate(): UInt8`: Get the date of the date-time.
3. `getDayOfMonth(): UInt8`: Get the day-of-month of the date-time.

Examples below also need to be updated

The `LocalDateTime` type will be defined as follows:

```cadence
pub struct LocalDateTime {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The proposal needs to be updated to Cadence 1.0. For example:

  • pub -> access(all)
  • Add view function modifier to getter functions


## Design Proposal

New types `LocalDateTime` & `Duration` will be added to Cadence. Both of these data types assume that there are 3600*24 seconds in every day and there are no leap years.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should add this functionality as a standard library contract, i.e. namespace it and not add them to the global namespace.

- `getDays(): Int32`: Get the days of the `Duration`.
- `getSeconds(): UInt32`: Get the second of the `Duration`.
- `getMicroseconds(): UInt32`: Get the microseconds of the `Duration`.
- `addYears(years : UInt32): Duration`: Return a new `Duration` after adding the provided number of years to it.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and for the other functions below: The function name already clarifies what the parameter is, so the argument label can be removed:

Suggested change
- `addYears(years : UInt32): Duration`: Return a new `Duration` after adding the provided number of years to it.
- `addYears(_ years: UInt32): Duration`: Return a new `Duration` after adding the provided number of years to it.

- `getDays(): Int32`: Get the days of the `Duration`.
- `getSeconds(): UInt32`: Get the second of the `Duration`.
- `getMicroseconds(): UInt32`: Get the microseconds of the `Duration`.
- `addYears(years : UInt32): Duration`: Return a new `Duration` after adding the provided number of years to it.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The add name sounds like it is mutating, but it seems like that isn't the case right?

Maybe we can use the plus and minus naming like in Java?

dateTimeFromTs.getSecond() // 12
dateTimeFromTs.getDayOfWeek() // wednesday

let dateTimeFromComponents = LocalDateTime.of(2019, april, 29, 19, 49, 31)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to add argument labels, as required by the function declaration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants