brick / date-time

Date and time library for PHP
MIT License
337 stars 30 forks source link

Add parts to Duration #15

Closed MMauro94 closed 5 years ago

MMauro94 commented 5 years ago

Hi! First of all thanks for the great work!

Would it be possible to add the following functions to the Duration class:

They should mimic the behavior of java.time.Duration.

As far as I can see the logic is already implemented in the __toString() function, so it should be quite easy to extract into functions.

BenMorel commented 5 years ago

Sure!

Do you know why Java has duplicate method names here? toDays() vs toDaysPart(), toHours() vs toHoursPart() etc.?

Theirs docs are mostly copy/pastes, so I can just assume these methods are doing the same thing. The only difference being that the methods ending in Part() have been introduced in Java 9, so maybe they plan to deprecated the old ones?

MMauro94 commented 5 years ago

The toX() functions return the total number of units, instead of their "part".

Example:

Duration d = Duration.parse("PT1H30M20S"); //1 hour, 30 minutes, 20 seconds

d.toHours(); // 1
d.toHoursPart(); // 1

d.toMinutes(); // 90
d.toMinutesPart(); // 30

d.toSeconds(); // 5420
d.toSecondsPart(); // 20
BenMorel commented 5 years ago

Oh right, this makes sense. I'll add both versions then!

BenMorel commented 5 years ago

Implemented and released in 0.1.14!