-
-
Notifications
You must be signed in to change notification settings - Fork 30
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 LocalDateRange::toDuration, YearMonthRange::toDuration methods #48
Conversation
Also we could add this method to YearMonthRange (looks reasonable), and Year, YearMonth, YearWeek (not so sure). |
Apart from my comment, LGTM 👍 Could you please consider adding
As for |
Why not? YearMonthRange is inclusive. It include full first month, everything in between and full last month. |
Year is not really a range, but also could represent a whole year from first day to last. |
I see. Let's start with this one and discuss the others in another PR. Also, Java's threeten-extra has a toPeriod method, that we may replicate here; maybe we should name this one |
I am not sure we should fight for consistency with Java here. Can you describe main logic of choosing between |
I have no idea, but this project started as port of Java's ThreeTen API, so whenever we can keep the same API, I think we should do it for two reasons:
As for the logic behind this, what about:
Though I'm sure you can find a counter-example in the codebase. |
I will look for it, maybe it good point to do some rename for consistency then. |
I finally chose
Thank you, that'd be great. |
4aade0f
to
ef0ec58
Compare
@BenMorel I renamed method, and also added YearMonthRange::toDuration |
*/ | ||
public function toDuration(): Duration | ||
{ | ||
return Duration::ofDays($this->count()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be Duration::ofDays($this->count() - 1)
?
As it stands, toPeriod()
returns a 0-day Period if the start date is equal to the end date (this is in line with its Java counterpart).
So wouldn't it be weird if toDuration()
returned a 1-day Duration for, say, 2022-06-05/2022-06-05
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This object is iterable and countable: the iterator returns all the LocalDate objects contained
in the range, whilecount()
returns the total number of dates contained in the range.
The end date, inclusive.
I think duration of one day range is One day, not zero)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As it stands, toPeriod() returns a 0-day Period if the start date is equal to the end date (this is in line with its Java counterpart).
Quite weird. Is LocalDateRange in java inclusive too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it makes sense in terms of Period: there is 1 day
between 2022-06-06
an 2022-06-07
.
That is: LocalDate::parse('2022-06-06')->plusPeriod(Period::ofDays(1))
is 2022-06-07
, so this approach is consistent.
In terms of Duration, maybe the approach is different. But this can be confusing.
I think about time zones and Local Range duration. In some time zones with Daylight Saving Time duration between day start and day end is not so obvious. For example Europe/London:
So Duration of range 27 March 2022 to 28 March 2022 in London is 47h. |
I agree that you need the timezone if you want to introduce a Maybe that's the reason why threeten-extra doesn't provide this method at all. What's your use case for this method exactly? |
Actually I don't remember it anymore :) |
No description provided.