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 LocalDateRange::toDuration, YearMonthRange::toDuration methods #48

Closed
wants to merge 4 commits into from

Conversation

solodkiy
Copy link
Contributor

No description provided.

@solodkiy
Copy link
Contributor Author

Also we could add this method to YearMonthRange (looks reasonable), and Year, YearMonth, YearWeek (not so sure).

@BenMorel
Copy link
Member

BenMorel commented May 29, 2022

Apart from my comment, LGTM 👍

Could you please consider adding LocalDateRange::getPeriod() as well in another PR? Although that one is less straightforward, as we have 2 ways to convert it to a period: calculating x years, y months & z days, or just 0 years, 0 months, and n days. Maybe we should even offer both ways? Let's start the discussion in #51.

YearMonthRange::getPeriod() would make sense, but not getDuration() as you cannot convert it to a number of days.

As for Year, YearMonth, YearWeek, they're not ranges, so I don't see how this could translate to a Duration or Period?

@solodkiy
Copy link
Contributor Author

YearMonthRange::getPeriod() would make sense, but not getDuration() as you cannot convert it to a number of days.

Why not? YearMonthRange is inclusive. It include full first month, everything in between and full last month.
So I think YearMonthRange include all days between first day of first range month and last day of last range month (include corner days).
This approach allow us to convert YearMonthRange to LocalDateRange and to Duration.

@solodkiy
Copy link
Contributor Author

Year is not really a range, but also could represent a whole year from first day to last.

@BenMorel
Copy link
Member

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 toDuration() for consistency then?

@solodkiy
Copy link
Contributor Author

solodkiy commented May 30, 2022

maybe we should name this one toDuration() for consistency then?

I am not sure we should fight for consistency with Java here.

Can you describe main logic of choosing between to and get methods?

@BenMorel
Copy link
Member

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:

  • there are many people behind this initiative, so they may have had this discussion before, and may have come to this consensus for a reason; to quote another contributor's comment:

    (...) we can't possibly replicate the collective brainpower and empirical evidence that went into designing JSR 310 and so, where practical, I would suggest simply copying JSR 310's API

  • this can make this project less confusing for people coming from a Java background

As for the logic behind this, what about:

  • get*() for fields that are part of the object (getYear(), getMonth(), ...)
  • to*() for objects that are not part of it, but can be derived from it? (toDuration())

Though I'm sure you can find a counter-example in the codebase.

@solodkiy
Copy link
Contributor Author

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.

@BenMorel BenMorel added this to the 0.4.1 milestone May 30, 2022
@BenMorel
Copy link
Member

BenMorel commented Jun 5, 2022

I finally chose toPeriod() in #51 for consistency with threeten-extra, so please let's use toDuration() here for consistency.

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.

Thank you, that'd be great.

@solodkiy solodkiy force-pushed the LocalDateRange_getDuration branch from 4aade0f to ef0ec58 Compare June 5, 2022 16:31
@solodkiy
Copy link
Contributor Author

solodkiy commented Jun 5, 2022

@BenMorel I renamed method, and also added YearMonthRange::toDuration

@solodkiy solodkiy changed the title Add LocalDateRange::getDuration method Add LocalDateRange::toDuration, YearMonthRange::toDuration methods Jun 5, 2022
*/
public function toDuration(): Duration
{
return Duration::ofDays($this->count());
Copy link
Member

@BenMorel BenMorel Jun 5, 2022

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?

Copy link
Contributor Author

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, while count() 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)

Copy link
Contributor Author

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?

Copy link
Member

@BenMorel BenMorel Jun 6, 2022

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.

@solodkiy
Copy link
Contributor Author

solodkiy commented Jun 6, 2022

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:

Sunday, 27 March 2022, 01:00:00 clocks were turned forward 1 hour to
Sunday, 27 March 2022, 02:00:00 local daylight time instead.

So Duration of range 27 March 2022 to 28 March 2022 in London is 47h.
Should we allow pass TimeZone param to toDuration() method?

@BenMorel
Copy link
Member

BenMorel commented Jun 6, 2022

I agree that you need the timezone if you want to introduce a toDuration() method, as 24 hours per day would be an oversimplification here.

Maybe that's the reason why threeten-extra doesn't provide this method at all.

What's your use case for this method exactly?

@solodkiy
Copy link
Contributor Author

What's your use case for this method exactly?

Actually I don't remember it anymore :)
I will return when I do.

@BenMorel BenMorel modified the milestones: 0.4.1, 0.4.2 Jun 18, 2022
@solodkiy solodkiy closed this Jul 4, 2023
@solodkiy solodkiy deleted the LocalDateRange_getDuration branch August 14, 2023 13:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants