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

Add support for :hour and :minute time units #10185

Merged
merged 7 commits into from
Jul 14, 2020

Conversation

sorentwo
Copy link
Contributor

This enhances time unit aware functions in the Time module so that they can operate on :hour and :minute, along with standard System.time_unit/0 values. This eliminates the need to convert all values to seconds, which allows more expressive time manipulation.

Along with changes to Time.add/3 and Time.diff/3, this adds a new extended time_unit type to Time.

are measured according to `Calendar.ISO`.
If the first time value is earlier than the second, a negative number is
returned. The answer can be returned in any `unit` available from
`t:time_unit/0`.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These two sections seemed contradictory so I removed the second segment. Looking at it now I realize that there isn't any mention of Calendar.ISO units, so I need to put that back.

This enhances time unit aware functions in the `Time` module so that
they can operate on `:hour` and `:minute`, along with standard
`System.time_unit/0` values. This eliminates the need to convert all
values to seconds, which allows more expressive time manipulation.

Along with changes to `Time.add/3` and `Time.diff/3`, this adds
a new extended `time_unit` type to `Time`.
@sorentwo sorentwo force-pushed the feature/minute-hour-time-units branch from 817013c to d84c281 Compare July 13, 2020 20:34
@sorentwo
Copy link
Contributor Author

This is the first of four changes (Time, Date, DateTime, NaiveDateTime). I opened the PR to get feedback on the approach before proceeding on the next few changes. The changes are atomic and stand alone so I opted for separate PRs, but I'm happy to combine them instead if desired.

end

def diff(%{calendar: Calendar.ISO} = time1, %{calendar: Calendar.ISO} = time2, :minute) do
(time1.hour - time2.hour) * 60 + (time1.minute - time2.minute)
Copy link
Member

Choose a reason for hiding this comment

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

This will return the wrong result for this:

Time.diff(~T[00:01:20], ~T[00:00:40])

It should return zero. I am afraid you will have to add all seconds and microseconds together, as in the clause below, and only then extracts minutes and hours.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Excellent point. It felt like something was missing here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've modified it to use the base diff implementation and div/2 the result 👍

sorentwo and others added 4 commits July 14, 2020 09:28
This enhances time unit aware functions in the `Time` module so that
they can operate on `:hour` and `:minute`, along with standard
`System.time_unit/0` values. This eliminates the need to convert all
values to seconds, which allows more expressive time manipulation.

Along with changes to `Time.add/3` and `Time.diff/3`, this adds
a new extended `time_unit` type to `Time`.
@fertapric fertapric merged commit 35f6ff8 into elixir-lang:master Jul 14, 2020
@fertapric
Copy link
Member

Thanks @sorentwo! 💜

josevalim added a commit that referenced this pull request Jul 16, 2020
@entertainyou
Copy link
Contributor

It looks this PR got reverted, @josevalim Sorry to interrupt, why it got reverted?

@sorentwo sorentwo deleted the feature/minute-hour-time-units branch October 29, 2020 13:46
@wojtekmach
Copy link
Member

see this comment and discussion around it for the reason: #10199 (comment)

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

Successfully merging this pull request may close these issues.

5 participants